admin: Embed static assets into binary
This commit is contained in:
parent
eb26879c74
commit
7fc860edec
37 changed files with 1569 additions and 1669 deletions
|
|
@ -1,9 +1,13 @@
|
|||
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"
|
||||
|
|
@ -12,52 +16,80 @@ import (
|
|||
"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) {
|
||||
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
|
||||
serveIndex(w, r)
|
||||
})
|
||||
api.Router().GET("/claims/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
|
||||
serveIndex(w, r)
|
||||
})
|
||||
api.Router().GET("/exercices/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
|
||||
serveIndex(w, r)
|
||||
})
|
||||
api.Router().GET("/events/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
|
||||
serveIndex(w, r)
|
||||
})
|
||||
api.Router().GET("/files", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
|
||||
serveIndex(w, r)
|
||||
})
|
||||
api.Router().GET("/public/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
|
||||
serveIndex(w, r)
|
||||
})
|
||||
api.Router().GET("/pki/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
|
||||
serveIndex(w, r)
|
||||
})
|
||||
api.Router().GET("/settings/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
|
||||
serveIndex(w, r)
|
||||
})
|
||||
api.Router().GET("/teams/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
|
||||
serveIndex(w, r)
|
||||
})
|
||||
api.Router().GET("/themes/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
|
||||
serveIndex(w, r)
|
||||
})
|
||||
|
||||
api.Router().GET("/css/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
http.ServeFile(w, r, path.Join(StaticDir, r.URL.Path))
|
||||
serveFile(w, r, 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))
|
||||
serveFile(w, r, 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))
|
||||
serveFile(w, r, 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))
|
||||
serveFile(w, r, 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))
|
||||
serveFile(w, r, r.URL.Path)
|
||||
})
|
||||
|
||||
api.Router().GET("/files/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
|
|
@ -75,9 +107,9 @@ func init() {
|
|||
})
|
||||
|
||||
api.Router().GET("/check_import.html", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
http.ServeFile(w, r, path.Join(StaticDir, "check_import.html"))
|
||||
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, path.Join(StaticDir, "full_import_report.json"))
|
||||
http.ServeFile(w, r, sync.DeepReportPath)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Reference in a new issue