package main import ( "bytes" "embed" "log" "net/http" "path" "strings" "text/template" "srs.epita.fr/fic-server/admin/api" "srs.epita.fr/fic-server/admin/sync" "srs.epita.fr/fic-server/libfic" "github.com/julienschmidt/httprouter" ) //go:embed static var assets embed.FS var indexPage []byte func genIndex(baseURL string) { b := bytes.NewBufferString("") if indexTmpl, err := template.New("index").Parse(indextpl); err != nil { log.Fatal("Cannot create template:", err) } else if err = indexTmpl.Execute(b, map[string]string{"urlbase": path.Clean(path.Join(baseURL+"/", "nuke"))[:len(path.Clean(path.Join(baseURL+"/", "nuke")))-4]}); err != nil { log.Fatal("An error occurs during template execution:", err) } else { indexPage = b.Bytes() } } func serveIndex(w http.ResponseWriter, r *http.Request) { w.Write(indexPage) } var staticFS http.FileSystem func serveFile(w http.ResponseWriter, r *http.Request, url string) { r.URL.Path = url http.FileServer(staticFS).ServeHTTP(w, r) } func init() { api.Router().GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { serveIndex(w, r) }) api.Router().GET("/claims/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { serveIndex(w, r) }) api.Router().GET("/exercices/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { serveIndex(w, r) }) api.Router().GET("/events/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { serveIndex(w, r) }) api.Router().GET("/files", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { serveIndex(w, r) }) api.Router().GET("/public/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { serveIndex(w, r) }) api.Router().GET("/pki/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { serveIndex(w, r) }) api.Router().GET("/settings/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { serveIndex(w, r) }) api.Router().GET("/teams/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { serveIndex(w, r) }) api.Router().GET("/themes/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { serveIndex(w, r) }) api.Router().GET("/css/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { serveFile(w, r, r.URL.Path) }) api.Router().GET("/fonts/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { serveFile(w, r, r.URL.Path) }) api.Router().GET("/img/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { serveFile(w, r, r.URL.Path) }) api.Router().GET("/js/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { serveFile(w, r, r.URL.Path) }) api.Router().GET("/views/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { serveFile(w, r, r.URL.Path) }) api.Router().GET("/files/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { http.ServeFile(w, r, path.Join(fic.FilesDir, strings.TrimPrefix(r.URL.Path, "/files"))) }) api.Router().GET("/submissions/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { http.ServeFile(w, r, path.Join(api.TimestampCheck, strings.TrimPrefix(r.URL.Path, "/submissions"))) }) api.Router().GET("/vids/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { if importer, ok := sync.GlobalImporter.(sync.LocalImporter); ok { http.ServeFile(w, r, path.Join(importer.Base, strings.TrimPrefix(r.URL.Path, "/vids"))) } else { http.Error(w, "Only available with local importer.", 400) } }) api.Router().GET("/check_import.html", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { serveFile(w, r, "check_import.html") }) api.Router().GET("/full_import_report.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { http.ServeFile(w, r, sync.DeepReportPath) }) }