admin: Embed static assets into binary
This commit is contained in:
parent
eb26879c74
commit
7fc860edec
37 changed files with 1569 additions and 1669 deletions
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
|
@ -13,7 +14,6 @@ import (
|
|||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
"text/template"
|
||||
|
||||
"srs.epita.fr/fic-server/admin/api"
|
||||
"srs.epita.fr/fic-server/admin/pki"
|
||||
|
|
@ -22,8 +22,6 @@ import (
|
|||
"srs.epita.fr/fic-server/settings"
|
||||
)
|
||||
|
||||
var StaticDir string
|
||||
|
||||
type ResponseWriterPrefix struct {
|
||||
real http.ResponseWriter
|
||||
prefix string
|
||||
|
|
@ -94,7 +92,7 @@ func main() {
|
|||
var baseURL = flag.String("baseurl", "/", "URL prepended to each URL")
|
||||
flag.StringVar(&api.TimestampCheck, "timestampCheck", api.TimestampCheck, "Path regularly touched by frontend to check time synchronisation")
|
||||
flag.StringVar(&pki.PKIDir, "pki", "./PKI", "Base directory where found PKI scripts")
|
||||
flag.StringVar(&StaticDir, "static", "./htdocs-admin/", "Directory containing static files")
|
||||
var staticDir = flag.String("static", "", "Directory containing static files (default if not provided: use embedded files)")
|
||||
flag.StringVar(&api.TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files")
|
||||
flag.StringVar(&api.DashboardDir, "dashbord", "./DASHBOARD", "Base directory where save public JSON files")
|
||||
flag.StringVar(&settings.SettingsDir, "settings", settings.SettingsDir, "Base directory where load and save settings")
|
||||
|
|
@ -129,10 +127,20 @@ func main() {
|
|||
// Sanitize options
|
||||
var err error
|
||||
log.Println("Checking paths...")
|
||||
if StaticDir, err = filepath.Abs(StaticDir); err != nil {
|
||||
log.Fatal(err)
|
||||
if staticDir != nil && *staticDir != "" {
|
||||
if sDir, err := filepath.Abs(*staticDir); err != nil {
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
staticFS = http.Dir(sDir)
|
||||
sync.DeepReportPath = path.Join(sDir, sync.DeepReportPath)
|
||||
}
|
||||
} else {
|
||||
sub, err := fs.Sub(assets, "static")
|
||||
if err != nil {
|
||||
log.Fatal("Unable to cd to static/ directory:", err)
|
||||
}
|
||||
staticFS = http.FS(sub)
|
||||
}
|
||||
sync.DeepReportPath = path.Join(StaticDir, sync.DeepReportPath)
|
||||
if fic.FilesDir, err = filepath.Abs(fic.FilesDir); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
@ -194,13 +202,7 @@ func main() {
|
|||
|
||||
// Update base URL on main page
|
||||
log.Println("Changing base URL to", *baseURL+"/", "...")
|
||||
if file, err := os.OpenFile(path.Join(StaticDir, "index.html"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.FileMode(0644)); err != nil {
|
||||
log.Println("Unable to open index.html: ", err)
|
||||
} else if indexTmpl, err := template.New("index").Parse(indextpl); err != nil {
|
||||
log.Println("Cannot create template: ", err)
|
||||
} else if err = indexTmpl.Execute(file, map[string]string{"urlbase": path.Clean(path.Join(*baseURL+"/", "nuke"))[:len(path.Clean(path.Join(*baseURL+"/", "nuke")))-4]}); err != nil {
|
||||
log.Println("An error occurs during template execution: ", err)
|
||||
}
|
||||
genIndex(*baseURL)
|
||||
|
||||
// Prepare graceful shutdown
|
||||
interrupt := make(chan os.Signal, 1)
|
||||
|
|
|
|||
Reference in a new issue