dashboard: move public.json files into a dedicated directory

This commit is contained in:
nemunaire 2018-12-06 03:46:14 +01:00
parent 485ffafc9a
commit 7970b552e9
5 changed files with 25 additions and 22 deletions

View file

@ -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))

View file

@ -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

View file

@ -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

View file

@ -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()

View file

@ -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"))
})
}