qa: Use gin
This commit is contained in:
parent
9fd5564410
commit
abdf146fea
13 changed files with 596 additions and 378 deletions
55
qa/static.go
55
qa/static.go
|
@ -2,23 +2,25 @@ package main
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"embed"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"srs.epita.fr/fic-server/qa/api"
|
||||
|
||||
"github.com/julienschmidt/httprouter"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
//go:embed static
|
||||
var assets embed.FS
|
||||
|
||||
var BaseURL = "/"
|
||||
|
||||
var indexTmpl []byte
|
||||
|
||||
func getIndexHtml(w io.Writer) {
|
||||
func getIndexHtml(c *gin.Context) {
|
||||
if len(indexTmpl) == 0 {
|
||||
if file, err := os.Open(path.Join(StaticDir, "index.html")); err != nil {
|
||||
log.Println("Unable to open index.html: ", err)
|
||||
|
@ -33,34 +35,41 @@ func getIndexHtml(w io.Writer) {
|
|||
}
|
||||
}
|
||||
|
||||
w.Write(indexTmpl)
|
||||
c.Writer.Write(indexTmpl)
|
||||
}
|
||||
|
||||
func init() {
|
||||
api.Router().GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
getIndexHtml(w)
|
||||
var staticFS http.FileSystem
|
||||
|
||||
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) {
|
||||
getIndexHtml(c)
|
||||
})
|
||||
|
||||
api.Router().GET("/exercices/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
getIndexHtml(w)
|
||||
router.GET("/exercices/*_", func(c *gin.Context) {
|
||||
getIndexHtml(c)
|
||||
})
|
||||
api.Router().GET("/themes/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
getIndexHtml(w)
|
||||
router.GET("/themes/*_", func(c *gin.Context) {
|
||||
getIndexHtml(c)
|
||||
})
|
||||
|
||||
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))
|
||||
})
|
||||
}
|
||||
|
|
Reference in a new issue