dashboard: Use gin-gonic instead of httprouter directly
This commit is contained in:
parent
8cb7bf8b96
commit
635e67c224
5 changed files with 193 additions and 215 deletions
|
@ -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"))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
Reference in a new issue