server/admin/static.go

128 lines
3.5 KiB
Go
Raw Normal View History

package main
import (
2021-06-08 23:11:23 +00:00
"bytes"
"embed"
2022-05-16 09:38:46 +00:00
"errors"
2021-06-08 23:11:23 +00:00
"log"
"net/http"
2016-01-22 16:34:34 +00:00
"path"
"strings"
2021-06-08 23:11:23 +00:00
"text/template"
"srs.epita.fr/fic-server/admin/api"
"srs.epita.fr/fic-server/admin/sync"
"srs.epita.fr/fic-server/libfic"
2022-05-16 09:38:46 +00:00
"srs.epita.fr/fic-server/settings"
2016-01-22 16:34:34 +00:00
2022-05-16 09:38:46 +00:00
"github.com/gin-gonic/gin"
)
2021-06-08 23:11:23 +00:00
//go:embed static
var assets embed.FS
var indexPage []byte
func genIndex(baseURL string) {
b := bytes.NewBufferString("")
if indexTmpl, err := template.New("index").Parse(indextpl); err != nil {
log.Fatal("Cannot create template:", err)
} else if err = indexTmpl.Execute(b, map[string]string{"urlbase": path.Clean(path.Join(baseURL+"/", "nuke"))[:len(path.Clean(path.Join(baseURL+"/", "nuke")))-4]}); err != nil {
log.Fatal("An error occurs during template execution:", err)
} else {
indexPage = b.Bytes()
}
}
2022-05-16 09:38:46 +00:00
func serveIndex(c *gin.Context) {
c.Writer.Write(indexPage)
2021-06-08 23:11:23 +00:00
}
var staticFS http.FileSystem
2022-05-16 09:38:46 +00:00
func serveFile(c *gin.Context, url string) {
c.Request.URL.Path = url
http.FileServer(staticFS).ServeHTTP(c.Writer, c.Request)
2021-06-08 23:11:23 +00:00
}
2022-05-16 09:38:46 +00:00
func declareStaticRoutes(router *gin.RouterGroup, cfg *settings.Settings, baseURL string) {
router.GET("/", func(c *gin.Context) {
serveIndex(c)
})
2022-05-16 09:38:46 +00:00
router.GET("/claims/*_", func(c *gin.Context) {
serveIndex(c)
2018-01-17 00:21:32 +00:00
})
2022-05-16 09:38:46 +00:00
router.GET("/exercices/*_", func(c *gin.Context) {
serveIndex(c)
})
2022-05-16 09:38:46 +00:00
router.GET("/events/*_", func(c *gin.Context) {
serveIndex(c)
})
2022-05-16 09:38:46 +00:00
router.GET("/files", func(c *gin.Context) {
serveIndex(c)
})
2022-05-16 09:38:46 +00:00
router.GET("/public/*_", func(c *gin.Context) {
serveIndex(c)
2017-01-20 18:18:43 +00:00
})
2022-05-16 09:38:46 +00:00
router.GET("/pki/*_", func(c *gin.Context) {
serveIndex(c)
2018-01-21 13:18:26 +00:00
})
router.GET("/repositories", func(c *gin.Context) {
serveIndex(c)
})
router.GET("/settings", func(c *gin.Context) {
serveIndex(c)
})
router.GET("/sync", func(c *gin.Context) {
2022-05-16 09:38:46 +00:00
serveIndex(c)
2017-01-15 01:37:59 +00:00
})
2022-05-24 19:25:27 +00:00
router.GET("/tags/*_", func(c *gin.Context) {
serveIndex(c)
})
2022-05-16 09:38:46 +00:00
router.GET("/teams/*_", func(c *gin.Context) {
serveIndex(c)
})
2022-05-16 09:38:46 +00:00
router.GET("/themes/*_", func(c *gin.Context) {
serveIndex(c)
})
2016-01-22 16:34:34 +00:00
2022-05-16 09:38:46 +00:00
router.GET("/css/*_", func(c *gin.Context) {
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
})
2022-05-16 09:38:46 +00:00
router.GET("/fonts/*_", func(c *gin.Context) {
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
})
2022-05-16 09:38:46 +00:00
router.GET("/img/*_", func(c *gin.Context) {
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
})
2022-05-16 09:38:46 +00:00
router.GET("/js/*_", func(c *gin.Context) {
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
})
2022-05-16 09:38:46 +00:00
router.GET("/views/*_", func(c *gin.Context) {
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
})
2022-05-16 09:38:46 +00:00
router.GET("/files/*_", func(c *gin.Context) {
// TODO: handle .gz file here
2022-05-22 23:39:35 +00:00
http.ServeFile(c.Writer, c.Request, path.Join(fic.FilesDir, strings.TrimPrefix(c.Request.URL.Path, path.Join(baseURL, "files"))))
})
2022-05-16 09:38:46 +00:00
router.GET("/submissions/*_", func(c *gin.Context) {
2022-05-22 23:39:35 +00:00
http.ServeFile(c.Writer, c.Request, path.Join(api.TimestampCheck, strings.TrimPrefix(c.Request.URL.Path, path.Join(baseURL, "submissions"))))
2020-01-29 15:01:11 +00:00
})
2022-05-16 09:38:46 +00:00
router.GET("/vids/*_", func(c *gin.Context) {
2022-05-22 23:39:35 +00:00
if importer, ok := sync.GlobalImporter.(sync.DirectAccessImporter); ok {
http.ServeFile(c.Writer, c.Request, importer.GetLocalPath(strings.TrimPrefix(c.Request.URL.Path, path.Join(baseURL, "vids"))))
} else {
2022-05-16 09:38:46 +00:00
c.AbortWithError(http.StatusBadRequest, errors.New("Only available with local importer."))
}
})
2022-05-16 09:38:46 +00:00
router.GET("/check_import.html", func(c *gin.Context) {
serveFile(c, "check_import.html")
})
2022-05-16 09:38:46 +00:00
router.GET("/full_import_report.json", func(c *gin.Context) {
http.ServeFile(c.Writer, c.Request, sync.DeepReportPath)
})
}