package main import ( "bytes" "fmt" "io" "io/ioutil" "log" "net/http" "os" "path" "strings" "time" "srs.epita.fr/fic-server/dashboard/api" "srs.epita.fr/fic-server/libfic" "srs.epita.fr/fic-server/settings" "github.com/julienschmidt/httprouter" ) var BaseURL = "/" var forwarder *string = nil var fwdPublicJson = false var indexTmpl []byte func getIndexHtml(w io.Writer) { if len(indexTmpl) == 0 { if file, err := os.Open(path.Join(StaticDir, "index.html")); err != nil { log.Println("Unable to open index.html: ", err) } else { defer file.Close() if indexTmpl, err = ioutil.ReadAll(file); err != nil { log.Println("Cannot read whole index.html: ", err) } else { indexTmpl = bytes.Replace(indexTmpl, []byte("{{.urlbase}}"), []byte(path.Clean(path.Join(BaseURL+"/", "nuke"))[:len(path.Clean(path.Join(BaseURL+"/", "nuke")))-4]), -1) } } } w.Write(indexTmpl) } func init() { api.Router().GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { http.Redirect(w, r, "public0.html", http.StatusFound) }) for i := 0; i <= 9; i++ { api.Router().GET(fmt.Sprintf("/public%d.html", i), func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { getIndexHtml(w) }) } api.Router().GET("/css/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { http.ServeFile(w, r, path.Join(StaticDir, r.URL.Path)) }) api.Router().GET("/fonts/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { http.ServeFile(w, r, path.Join(StaticDir, r.URL.Path)) }) api.Router().GET("/img/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { http.ServeFile(w, r, path.Join(StaticDir, r.URL.Path)) }) api.Router().GET("/js/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { http.ServeFile(w, r, path.Join(StaticDir, r.URL.Path)) }) api.Router().GET("/views/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { http.ServeFile(w, r, path.Join(StaticDir, r.URL.Path)) }) api.Router().GET("/files/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { if forwarder != nil { fwd_request(w, r, *forwarder) } else { http.ServeFile(w, r, path.Join(fic.FilesDir, strings.TrimPrefix(r.URL.Path, "/files"))) } }) api.Router().GET("/events.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Header().Set("Cache-Control", "no-cache") if forwarder != nil { fwd_request(w, r, *forwarder) } else { http.ServeFile(w, r, path.Join(TeamsDir, "events.json")) } }) api.Router().GET("/my.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Header().Set("Cache-Control", "no-cache") if forwarder != nil { fwd_request(w, r, *forwarder) } else { http.ServeFile(w, r, path.Join(TeamsDir, "public", "my.json")) } }) api.Router().GET("/api/teams/:tid/score-grid.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { if forwarder != nil { fwd_request(w, r, *forwarder) } else { fwd_request(w, r, "http://127.0.0.1:8081/") } }) api.Router().GET("/api/teams/:tid/stats.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { if forwarder != nil { fwd_request(w, r, *forwarder) } else { fwd_request(w, r, "http://127.0.0.1:8081/") } }) api.Router().GET("/challenge.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Header().Set("Cache-Control", "no-cache") if forwarder != nil { fwd_request(w, r, *forwarder) } else { http.ServeFile(w, r, path.Join(settings.SettingsDir, settings.ChallengeFile)) } }) api.Router().GET("/settings.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Header().Set("Cache-Control", "no-cache") if forwarder != nil { fwd_request(w, r, *forwarder) } else { w.Header().Set("X-FIC-Time", fmt.Sprintf("%f", float64(time.Now().UnixNano()/1000)/1000000)) http.ServeFile(w, r, path.Join(settings.SettingsDir, settings.SettingsFile)) } }) api.Router().GET("/teams.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Header().Set("Cache-Control", "no-cache") if forwarder != nil { fwd_request(w, r, *forwarder) } else { http.ServeFile(w, r, path.Join(TeamsDir, "public", "teams.json")) } }) api.Router().GET("/themes.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Header().Set("Cache-Control", "no-cache") if forwarder != nil { fwd_request(w, r, *forwarder) } else { http.ServeFile(w, r, path.Join(TeamsDir, "themes.json")) } }) api.Router().GET("/public.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Header().Set("Cache-Control", "no-cache") if forwarder != nil && fwdPublicJson { fwd_request(w, r, *forwarder) } else { http.ServeFile(w, r, path.Join(DashboardDir, "public.json")) } }) api.Router().GET("/public0.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Header().Set("Cache-Control", "no-cache") if forwarder != nil && fwdPublicJson { fwd_request(w, r, *forwarder) } else { http.ServeFile(w, r, path.Join(DashboardDir, "public0.json")) } }) api.Router().GET("/public1.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Header().Set("Cache-Control", "no-cache") if forwarder != nil && fwdPublicJson { fwd_request(w, r, *forwarder) } else { http.ServeFile(w, r, path.Join(DashboardDir, "public1.json")) } }) api.Router().GET("/public2.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Header().Set("Cache-Control", "no-cache") if forwarder != nil && fwdPublicJson { fwd_request(w, r, *forwarder) } else { http.ServeFile(w, r, path.Join(DashboardDir, "public2.json")) } }) api.Router().GET("/public3.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Header().Set("Cache-Control", "no-cache") if forwarder != nil && fwdPublicJson { fwd_request(w, r, *forwarder) } else { http.ServeFile(w, r, path.Join(DashboardDir, "public3.json")) } }) api.Router().GET("/public4.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Header().Set("Cache-Control", "no-cache") if forwarder != nil && fwdPublicJson { fwd_request(w, r, *forwarder) } else { http.ServeFile(w, r, path.Join(DashboardDir, "public4.json")) } }) api.Router().GET("/public5.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Header().Set("Cache-Control", "no-cache") if forwarder != nil && fwdPublicJson { fwd_request(w, r, *forwarder) } else { http.ServeFile(w, r, path.Join(DashboardDir, "public5.json")) } }) api.Router().GET("/public6.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Header().Set("Cache-Control", "no-cache") if forwarder != nil && fwdPublicJson { fwd_request(w, r, *forwarder) } else { http.ServeFile(w, r, path.Join(DashboardDir, "public6.json")) } }) api.Router().GET("/public7.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Header().Set("Cache-Control", "no-cache") if forwarder != nil && fwdPublicJson { fwd_request(w, r, *forwarder) } else { http.ServeFile(w, r, path.Join(DashboardDir, "public7.json")) } }) api.Router().GET("/public8.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Header().Set("Cache-Control", "no-cache") if forwarder != nil && fwdPublicJson { fwd_request(w, r, *forwarder) } else { http.ServeFile(w, r, path.Join(DashboardDir, "public8.json")) } }) api.Router().GET("/public9.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Header().Set("Cache-Control", "no-cache") if forwarder != nil && fwdPublicJson { fwd_request(w, r, *forwarder) } else { http.ServeFile(w, r, path.Join(DashboardDir, "public9.json")) } }) }