2014-05-25 22:41:28 -07:00
|
|
|
// Copyright 2014 Team 254. All Rights Reserved.
|
|
|
|
|
// Author: pat@patfairbank.com (Patrick Fairbank)
|
|
|
|
|
//
|
|
|
|
|
// Configuration and functions for the event server web interface.
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/gorilla/mux"
|
2014-06-02 23:52:48 -07:00
|
|
|
"html/template"
|
2014-05-25 22:41:28 -07:00
|
|
|
"log"
|
|
|
|
|
"net/http"
|
2014-06-03 14:14:22 -07:00
|
|
|
"os/exec"
|
|
|
|
|
"runtime"
|
|
|
|
|
"strconv"
|
2014-05-25 22:41:28 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const httpPort = 8080
|
|
|
|
|
|
2014-06-02 23:52:48 -07:00
|
|
|
func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
template, err := template.ParseFiles("templates/index.html", "templates/base.html")
|
|
|
|
|
if err != nil {
|
|
|
|
|
handleWebErr(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
err = template.ExecuteTemplate(w, "base", nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
handleWebErr(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-25 22:41:28 -07:00
|
|
|
func ServeWebInterface() {
|
2014-06-02 23:52:48 -07:00
|
|
|
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
|
2014-05-25 22:41:28 -07:00
|
|
|
http.Handle("/", newHandler())
|
|
|
|
|
log.Printf("Serving HTTP requests on port %d", httpPort)
|
2014-06-03 14:14:22 -07:00
|
|
|
|
|
|
|
|
// Open in Default Web Browser
|
|
|
|
|
// Necessary to Authenticate
|
|
|
|
|
url := "http://localhost:"+strconv.Itoa(httpPort)
|
|
|
|
|
var err error
|
|
|
|
|
switch runtime.GOOS {
|
|
|
|
|
case "linux":
|
|
|
|
|
err = exec.Command("xdg-open", url).Start()
|
|
|
|
|
case "darwin":
|
|
|
|
|
err = exec.Command("open", url).Start()
|
|
|
|
|
case "windows":
|
|
|
|
|
err = exec.Command(`rundll32.exe`, "url.dll,FileProtocolHandler", url).Start()
|
|
|
|
|
default:
|
|
|
|
|
err = fmt.Errorf("unsupported platform")
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
println(err.Error())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Start Server
|
2014-05-25 22:41:28 -07:00
|
|
|
http.ListenAndServe(fmt.Sprintf(":%d", httpPort), nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newHandler() http.Handler {
|
|
|
|
|
router := mux.NewRouter()
|
2014-05-31 19:28:32 -07:00
|
|
|
router.HandleFunc("/reports/csv/rankings", RankingsCsvReportHandler)
|
|
|
|
|
router.HandleFunc("/reports/pdf/rankings", RankingsPdfReportHandler)
|
2014-06-03 22:20:10 -07:00
|
|
|
router.HandleFunc("/reports/json/rankings", RankingsJSONReportHandler)
|
2014-05-26 16:39:20 -07:00
|
|
|
router.HandleFunc("/reports/csv/schedule/{type}", ScheduleCsvReportHandler)
|
|
|
|
|
router.HandleFunc("/reports/pdf/schedule/{type}", SchedulePdfReportHandler)
|
2014-05-25 22:41:28 -07:00
|
|
|
router.HandleFunc("/reports/csv/teams", TeamsCsvReportHandler)
|
|
|
|
|
router.HandleFunc("/reports/pdf/teams", TeamsPdfReportHandler)
|
2014-06-02 23:52:48 -07:00
|
|
|
router.HandleFunc("/", IndexHandler)
|
2014-05-25 22:41:28 -07:00
|
|
|
return router
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func handleWebErr(w http.ResponseWriter, err error) {
|
|
|
|
|
http.Error(w, "Internal server error: "+err.Error(), 500)
|
|
|
|
|
}
|