dashboard: Use gin-gonic instead of httprouter directly

This commit is contained in:
nemunaire 2022-06-06 10:47:38 +02:00
parent 8cb7bf8b96
commit 635e67c224
5 changed files with 193 additions and 215 deletions

View File

@ -1,81 +0,0 @@
package api
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"
"github.com/julienschmidt/httprouter"
)
type DispatchFunction func(httprouter.Params, []byte) (interface{}, error)
func apiHandler(f DispatchFunction) func(http.ResponseWriter, *http.Request, httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if addr := r.Header.Get("X-Forwarded-For"); addr != "" {
r.RemoteAddr = addr
}
log.Printf("%s \"%s %s\" [%s]\n", r.RemoteAddr, r.Method, r.URL.Path, r.UserAgent())
// Read the body
if r.ContentLength < 0 || r.ContentLength > 6553600 {
http.Error(w, fmt.Sprintf("{errmsg:\"Request too large or request size unknown\"}"), http.StatusRequestEntityTooLarge)
return
}
var body []byte
if r.ContentLength > 0 {
tmp := make([]byte, 1024)
for {
n, err := r.Body.Read(tmp)
for j := 0; j < n; j++ {
body = append(body, tmp[j])
}
if err != nil || n <= 0 {
break
}
}
}
var ret interface{}
var err error = nil
ret, err = f(ps, body)
// Format response
resStatus := http.StatusOK
if err != nil {
ret = map[string]string{"errmsg": err.Error()}
resStatus = http.StatusBadRequest
log.Println(r.RemoteAddr, resStatus, err.Error())
}
if ret == nil {
ret = map[string]string{"errmsg": "Page not found"}
resStatus = http.StatusNotFound
}
w.Header().Set("X-FIC-Time", fmt.Sprintf("%f", float64(time.Now().UnixNano()/1000)/1000000))
if str, found := ret.(string); found {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resStatus)
io.WriteString(w, str)
} else if bts, found := ret.([]byte); found {
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", "attachment")
w.Header().Set("Content-Transfer-Encoding", "binary")
w.WriteHeader(resStatus)
w.Write(bts)
} else if j, err := json.Marshal(ret); err != nil {
w.Header().Set("Content-Type", "application/json")
http.Error(w, fmt.Sprintf("{\"errmsg\":%q}", err), http.StatusInternalServerError)
} else {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resStatus)
w.Write(j)
}
}
}

View File

@ -1,11 +0,0 @@
package api
import (
"github.com/julienschmidt/httprouter"
)
var router = httprouter.New()
func Router() *httprouter.Router {
return router
}

60
dashboard/app.go Normal file
View File

@ -0,0 +1,60 @@
package main
import (
"context"
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
type App struct {
router *gin.Engine
srv *http.Server
bind string
}
func NewApp(baseURL string, bind string) App {
gin.ForceConsoleColor()
router := gin.Default()
var baserouter *gin.RouterGroup
if len(baseURL) > 1 {
router.GET("/", func(c *gin.Context) {
c.Redirect(http.StatusFound, baseURL)
})
baserouter = router.Group(baseURL)
} else {
baserouter = router.Group("")
}
declareStaticRoutes(baserouter, baseURL)
app := App{
router: router,
bind: bind,
}
return app
}
func (app *App) Start() {
app.srv = &http.Server{
Addr: app.bind,
Handler: app.router,
}
log.Printf("Ready, listening on %s\n", app.bind)
if err := app.srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("listen: %s\n", err)
}
}
func (app *App) Stop() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := app.srv.Shutdown(ctx); err != nil {
log.Fatal("Server Shutdown:", err)
}
}

View File

@ -1,9 +1,8 @@
package main
import (
"context"
"flag"
"fmt"
"io/fs"
"log"
"net/http"
"net/url"
@ -14,12 +13,10 @@ import (
"strings"
"syscall"
"srs.epita.fr/fic-server/dashboard/api"
"srs.epita.fr/fic-server/libfic"
"srs.epita.fr/fic-server/settings"
)
var StaticDir string
var DashboardDir string
var TeamsDir string
@ -64,15 +61,16 @@ func StripPrefix(prefix string, h http.Handler) http.Handler {
}
func main() {
var baseURL string
// Read paremeters from environment
if v, exists := os.LookupEnv("FIC_BASEURL"); exists {
BaseURL = v
baseURL = v
}
// Read parameters from command line
var bind = flag.String("bind", "127.0.0.1:8082", "Bind port/socket")
flag.StringVar(&BaseURL, "baseurl", BaseURL, "URL prepended to each URL")
flag.StringVar(&StaticDir, "static", "./htdocs-dashboard/", "Directory containing static files")
flag.StringVar(&baseURL, "baseurl", baseURL, "URL prepended to each URL")
staticDir := flag.String("static", "./htdocs-dashboard/", "Directory containing static files")
flag.StringVar(&fic.FilesDir, "files", fic.FilesDir, "Base directory where found challenges files, local part")
flag.StringVar(&DashboardDir, "dashbord", "./DASHBOARD", "Base directory where save public JSON files")
flag.StringVar(&TeamsDir, "teams", "./TEAMS", "Base directory where save teams JSON files")
@ -86,8 +84,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 {
log.Println("Serving pages from", sDir)
staticFS = http.Dir(sDir)
}
} else {
sub, err := fs.Sub(assets, "static")
if err != nil {
log.Fatal("Unable to cd to static/ directory:", err)
}
log.Println("Serving pages from memory.")
staticFS = http.FS(sub)
}
if fic.FilesDir, err = filepath.Abs(fic.FilesDir); err != nil {
log.Fatal(err)
@ -95,10 +105,10 @@ func main() {
if settings.SettingsDir, err = filepath.Abs(settings.SettingsDir); err != nil {
log.Fatal(err)
}
if BaseURL != "/" {
BaseURL = path.Clean(BaseURL)
if baseURL != "/" {
baseURL = path.Clean(baseURL)
} else {
BaseURL = ""
baseURL = ""
}
if fwdr != nil && len(*fwdr) > 0 {
forwarder = fwdr
@ -108,21 +118,13 @@ func main() {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)
srv := &http.Server{
Addr: *bind,
Handler: StripPrefix(BaseURL, api.Router()),
}
// Serve content
go func() {
log.Fatal(srv.ListenAndServe())
}()
log.Println(fmt.Sprintf("Ready, listening on %s", *bind))
app := NewApp(baseURL, *bind)
go app.Start()
// Wait shutdown signal
<-interrupt
log.Print("The service is shutting down...")
srv.Shutdown(context.Background())
app.Stop()
log.Println("done")
}

View File

@ -2,32 +2,35 @@ package main
import (
"bytes"
"embed"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strings"
"time"
"srs.epita.fr/fic-server/dashboard/api"
"srs.epita.fr/fic-server/libfic"
"srs.epita.fr/fic-server/settings"
"github.com/julienschmidt/httprouter"
"github.com/gin-gonic/gin"
)
var BaseURL = "/"
//go:embed static
var assets embed.FS
var staticFS http.FileSystem
var forwarder *string = nil
var fwdPublicJson = false
var indexTmpl []byte
func getIndexHtml(w io.Writer) {
func getIndexHtml(w io.Writer, baseURL string) {
if len(indexTmpl) == 0 {
if file, err := os.Open(path.Join(StaticDir, "index.html")); err != nil {
if file, err := staticFS.Open("index.html"); err != nil {
log.Println("Unable to open index.html: ", err)
} else {
defer file.Close()
@ -35,7 +38,7 @@ func getIndexHtml(w io.Writer) {
if indexTmpl, err = ioutil.ReadAll(file); err != nil {
log.Println("Cannot read whole index.html: ", err)
} else {
indexTmpl = bytes.Replace(indexTmpl, []byte("{{.urlbase}}"), []byte(path.Clean(path.Join(BaseURL+"/", "nuke"))[:len(path.Clean(path.Join(BaseURL+"/", "nuke")))-4]), -1)
indexTmpl = bytes.Replace(indexTmpl, []byte("{{.urlbase}}"), []byte(path.Clean(path.Join(baseURL+"/", "nuke"))[:len(path.Clean(path.Join(baseURL+"/", "nuke")))-4]), -1)
}
}
}
@ -43,190 +46,195 @@ func getIndexHtml(w io.Writer) {
w.Write(indexTmpl)
}
func init() {
api.Router().GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.Redirect(w, r, "public0.html", http.StatusFound)
func serveFile(c *gin.Context, url string) {
c.Request.URL.Path = url
http.FileServer(staticFS).ServeHTTP(c.Writer, c.Request)
}
func declareStaticRoutes(router *gin.RouterGroup, baseURL string) {
router.GET("/", func(c *gin.Context) {
http.Redirect(c.Writer, c.Request, "public0.html", http.StatusFound)
})
for i := 0; i <= 9; i++ {
api.Router().GET(fmt.Sprintf("/public%d.html", i), func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
getIndexHtml(w)
router.GET(fmt.Sprintf("/public%d.html", i), func(c *gin.Context) {
getIndexHtml(c.Writer, baseURL)
})
}
api.Router().GET("/css/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, r.URL.Path))
router.GET("/css/*_", func(c *gin.Context) {
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
})
api.Router().GET("/fonts/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, r.URL.Path))
router.GET("/fonts/*_", func(c *gin.Context) {
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
})
api.Router().GET("/img/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, r.URL.Path))
router.GET("/img/*_", func(c *gin.Context) {
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
})
api.Router().GET("/js/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, r.URL.Path))
router.GET("/js/*_", func(c *gin.Context) {
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
})
api.Router().GET("/views/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, r.URL.Path))
router.GET("/views/*_", func(c *gin.Context) {
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
})
api.Router().GET("/files/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
router.GET("/files/*_", func(c *gin.Context) {
if forwarder != nil {
fwd_request(w, r, *forwarder)
fwd_request(c.Writer, c.Request, *forwarder)
} else {
http.ServeFile(w, r, path.Join(fic.FilesDir, strings.TrimPrefix(r.URL.Path, "/files")))
http.ServeFile(c.Writer, c.Request, path.Join(fic.FilesDir, strings.TrimPrefix(c.Request.URL.Path, path.Join(baseURL, "files"))))
}
})
api.Router().GET("/events.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
router.GET("/events.json", func(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", "no-cache")
if forwarder != nil {
fwd_request(w, r, *forwarder)
fwd_request(c.Writer, c.Request, *forwarder)
} else {
http.ServeFile(w, r, path.Join(TeamsDir, "events.json"))
http.ServeFile(c.Writer, c.Request, path.Join(TeamsDir, "events.json"))
}
})
api.Router().GET("/my.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
router.GET("/my.json", func(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", "no-cache")
if forwarder != nil {
fwd_request(w, r, *forwarder)
fwd_request(c.Writer, c.Request, *forwarder)
} else {
http.ServeFile(w, r, path.Join(TeamsDir, "public", "my.json"))
http.ServeFile(c.Writer, c.Request, path.Join(TeamsDir, "public", "my.json"))
}
})
api.Router().GET("/api/teams/:tid/score-grid.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
router.GET("/api/teams/:tid/score-grid.json", func(c *gin.Context) {
if forwarder != nil {
fwd_request(w, r, *forwarder)
fwd_request(c.Writer, c.Request, *forwarder)
} else {
fwd_request(w, r, "http://127.0.0.1:8081/")
fwd_request(c.Writer, c.Request, "http://127.0.0.1:8081/")
}
})
api.Router().GET("/api/teams/:tid/stats.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
router.GET("/api/teams/:tid/stats.json", func(c *gin.Context) {
if forwarder != nil {
fwd_request(w, r, *forwarder)
fwd_request(c.Writer, c.Request, *forwarder)
} else {
fwd_request(w, r, "http://127.0.0.1:8081/")
fwd_request(c.Writer, c.Request, "http://127.0.0.1:8081/")
}
})
api.Router().GET("/challenge.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
router.GET("/challenge.json", func(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", "no-cache")
if forwarder != nil {
fwd_request(w, r, *forwarder)
fwd_request(c.Writer, c.Request, *forwarder)
} else {
http.ServeFile(w, r, path.Join(settings.SettingsDir, settings.ChallengeFile))
http.ServeFile(c.Writer, c.Request, path.Join(settings.SettingsDir, settings.ChallengeFile))
}
})
api.Router().GET("/settings.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
router.GET("/settings.json", func(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", "no-cache")
if forwarder != nil {
fwd_request(w, r, *forwarder)
fwd_request(c.Writer, c.Request, *forwarder)
} else {
w.Header().Set("X-FIC-Time", fmt.Sprintf("%f", float64(time.Now().UnixNano()/1000)/1000000))
http.ServeFile(w, r, path.Join(settings.SettingsDir, settings.SettingsFile))
c.Writer.Header().Set("X-FIC-Time", fmt.Sprintf("%f", float64(time.Now().UnixNano()/1000)/1000000))
http.ServeFile(c.Writer, c.Request, path.Join(settings.SettingsDir, settings.SettingsFile))
}
})
api.Router().GET("/teams.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
router.GET("/teams.json", func(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", "no-cache")
if forwarder != nil {
fwd_request(w, r, *forwarder)
fwd_request(c.Writer, c.Request, *forwarder)
} else {
http.ServeFile(w, r, path.Join(TeamsDir, "public", "teams.json"))
http.ServeFile(c.Writer, c.Request, path.Join(TeamsDir, "public", "teams.json"))
}
})
api.Router().GET("/themes.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
router.GET("/themes.json", func(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", "no-cache")
if forwarder != nil {
fwd_request(w, r, *forwarder)
fwd_request(c.Writer, c.Request, *forwarder)
} else {
http.ServeFile(w, r, path.Join(TeamsDir, "themes.json"))
http.ServeFile(c.Writer, c.Request, path.Join(TeamsDir, "themes.json"))
}
})
api.Router().GET("/public.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
router.GET("/public.json", func(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
fwd_request(c.Writer, c.Request, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public.json"))
http.ServeFile(c.Writer, c.Request, 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")
router.GET("/public0.json", func(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
fwd_request(c.Writer, c.Request, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public0.json"))
http.ServeFile(c.Writer, c.Request, 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")
router.GET("/public1.json", func(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
fwd_request(c.Writer, c.Request, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public1.json"))
http.ServeFile(c.Writer, c.Request, 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")
router.GET("/public2.json", func(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
fwd_request(c.Writer, c.Request, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public2.json"))
http.ServeFile(c.Writer, c.Request, 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")
router.GET("/public3.json", func(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
fwd_request(c.Writer, c.Request, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public3.json"))
http.ServeFile(c.Writer, c.Request, 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")
router.GET("/public4.json", func(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
fwd_request(c.Writer, c.Request, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public4.json"))
http.ServeFile(c.Writer, c.Request, 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")
router.GET("/public5.json", func(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
fwd_request(c.Writer, c.Request, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public5.json"))
http.ServeFile(c.Writer, c.Request, 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")
router.GET("/public6.json", func(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
fwd_request(c.Writer, c.Request, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public6.json"))
http.ServeFile(c.Writer, c.Request, 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")
router.GET("/public7.json", func(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
fwd_request(c.Writer, c.Request, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public7.json"))
http.ServeFile(c.Writer, c.Request, 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")
router.GET("/public8.json", func(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
fwd_request(c.Writer, c.Request, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public8.json"))
http.ServeFile(c.Writer, c.Request, 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")
router.GET("/public9.json", func(c *gin.Context) {
c.Writer.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
fwd_request(c.Writer, c.Request, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public9.json"))
http.ServeFile(c.Writer, c.Request, path.Join(DashboardDir, "public9.json"))
}
})
}