From 7970b552e99fd3e6ef6ea3fabd3a7eef4edb9211 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Thu, 6 Dec 2018 03:46:14 +0100 Subject: [PATCH] dashboard: move public.json files into a dedicated directory --- admin/api/certificate.go | 2 ++ admin/api/public.go | 16 +++++----------- admin/main.go | 5 +++++ dashboard/main.go | 2 ++ dashboard/static.go | 22 +++++++++++----------- 5 files changed, 25 insertions(+), 22 deletions(-) diff --git a/admin/api/certificate.go b/admin/api/certificate.go index bdf56454..7a5eb203 100644 --- a/admin/api/certificate.go +++ b/admin/api/certificate.go @@ -19,6 +19,8 @@ import ( "github.com/julienschmidt/httprouter" ) +var TeamsDir string + func init() { router.GET("/api/ca/", apiHandler(infoCA)) router.GET("/api/ca.pem", apiHandler(getCAPEM)) diff --git a/admin/api/public.go b/admin/api/public.go index 37db0e4a..bf6e43e1 100644 --- a/admin/api/public.go +++ b/admin/api/public.go @@ -9,7 +9,7 @@ import ( "github.com/julienschmidt/httprouter" ) -var TeamsDir string +var DashboardDir string func init() { router.GET("/api/public/:sid", apiHandler(getPublic)) @@ -54,15 +54,15 @@ func savePublicTo(path string, s []FICPublicScene) error { } func getPublic(ps httprouter.Params, body []byte) (interface{}, error) { - if _, err := os.Stat(path.Join(TeamsDir, "public", fmt.Sprintf("public%s.json", ps.ByName("sid")))); !os.IsNotExist(err) { - return readPublic(path.Join(TeamsDir, "public", fmt.Sprintf("public%s.json", ps.ByName("sid")))) + if _, err := os.Stat(path.Join(DashboardDir, fmt.Sprintf("public%s.json", ps.ByName("sid")))); !os.IsNotExist(err) { + return readPublic(path.Join(DashboardDir, fmt.Sprintf("public%s.json", ps.ByName("sid")))) } else { return []FICPublicScene{}, nil } } func deletePublic(ps httprouter.Params, body []byte) (interface{}, error) { - if err := savePublicTo(path.Join(TeamsDir, "public", fmt.Sprintf("public%s.json", ps.ByName("sid"))), []FICPublicScene{}); err != nil { + if err := savePublicTo(path.Join(DashboardDir, fmt.Sprintf("public%s.json", ps.ByName("sid"))), []FICPublicScene{}); err != nil { return nil, err } else { return []FICPublicScene{}, err @@ -75,13 +75,7 @@ func savePublic(ps httprouter.Params, body []byte) (interface{}, error) { return nil, err } - if _, err := os.Stat(path.Join(TeamsDir, "public")); os.IsNotExist(err) { - if err := os.Mkdir(path.Join(TeamsDir, "public"), 0750); err != nil { - return nil, err - } - } - - if err := savePublicTo(path.Join(TeamsDir, "public", fmt.Sprintf("public%s.json", ps.ByName("sid"))), scenes); err != nil { + if err := savePublicTo(path.Join(DashboardDir, fmt.Sprintf("public%s.json", ps.ByName("sid"))), scenes); err != nil { return nil, err } else { return scenes, err diff --git a/admin/main.go b/admin/main.go index 9519c6db..4804b7eb 100644 --- a/admin/main.go +++ b/admin/main.go @@ -95,6 +95,7 @@ func main() { flag.StringVar(&pki.PKIDir, "pki", "./PKI", "Base directory where found PKI scripts") flag.StringVar(&StaticDir, "static", "./htdocs-admin/", "Directory containing static 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") flag.StringVar(&fic.FilesDir, "files", fic.FilesDir, "Base directory where found challenges files, local part") flag.StringVar(&localImporterDirectory, "localimport", localImporterDirectory, @@ -137,6 +138,9 @@ func main() { if pki.PKIDir, err = filepath.Abs(pki.PKIDir); err != nil { log.Fatal(err) } + if api.DashboardDir, err = filepath.Abs(api.DashboardDir); err != nil { + log.Fatal(err) + } if api.TeamsDir, err = filepath.Abs(api.TeamsDir); err != nil { log.Fatal(err) } @@ -155,6 +159,7 @@ func main() { os.MkdirAll(fic.FilesDir, 0777) os.MkdirAll(pki.PKIDir, 0711) os.MkdirAll(api.TeamsDir, 0777) + os.MkdirAll(api.DashboardDir, 0777) os.MkdirAll(settings.SettingsDir, 0777) // Initialize settings and load them diff --git a/dashboard/main.go b/dashboard/main.go index 0319a447..00bf7cc2 100644 --- a/dashboard/main.go +++ b/dashboard/main.go @@ -20,6 +20,7 @@ import ( ) var StaticDir string +var DashboardDir string var TeamsDir string type ResponseWriterPrefix struct { @@ -67,6 +68,7 @@ func main() { var bind = flag.String("bind", "127.0.0.1:8082", "Bind port/socket") var baseURL = flag.String("baseurl", "/", "URL prepended to each URL") flag.StringVar(&StaticDir, "static", "./htdocs-dashboard/", "Directory containing static files") + flag.StringVar(&DashboardDir, "dashbord", "./DASHBOARD", "Base directory where save public JSON files") flag.StringVar(&TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files") flag.StringVar(&settings.SettingsDir, "settings", settings.SettingsDir, "Base directory where load and save settings") flag.Parse() diff --git a/dashboard/static.go b/dashboard/static.go index ab96b156..d860ed69 100644 --- a/dashboard/static.go +++ b/dashboard/static.go @@ -61,46 +61,46 @@ func init() { api.Router().GET("/public.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Header().Set("Cache-Control", "no-cache") - http.ServeFile(w, r, path.Join(TeamsDir, "public", "public.json")) + 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") - http.ServeFile(w, r, path.Join(TeamsDir, "public", "public0.json")) + 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") - http.ServeFile(w, r, path.Join(TeamsDir, "public", "public1.json")) + 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") - http.ServeFile(w, r, path.Join(TeamsDir, "public", "public2.json")) + 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") - http.ServeFile(w, r, path.Join(TeamsDir, "public", "public3.json")) + 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") - http.ServeFile(w, r, path.Join(TeamsDir, "public", "public4.json")) + 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") - http.ServeFile(w, r, path.Join(TeamsDir, "public", "public5.json")) + 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") - http.ServeFile(w, r, path.Join(TeamsDir, "public", "public6.json")) + 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") - http.ServeFile(w, r, path.Join(TeamsDir, "public", "public7.json")) + 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") - http.ServeFile(w, r, path.Join(TeamsDir, "public", "public8.json")) + 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") - http.ServeFile(w, r, path.Join(TeamsDir, "public", "public9.json")) + http.ServeFile(w, r, path.Join(DashboardDir, "public9.json")) }) }