2016-01-13 00:22:01 +00:00
|
|
|
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"
|
2016-01-13 00:22:01 +00:00
|
|
|
"net/http"
|
2016-01-22 16:34:34 +00:00
|
|
|
"path"
|
2018-12-08 20:00:00 +00:00
|
|
|
"strings"
|
2021-06-08 23:11:23 +00:00
|
|
|
"text/template"
|
2016-01-13 00:22:01 +00:00
|
|
|
|
2016-12-15 23:51:56 +00:00
|
|
|
"srs.epita.fr/fic-server/admin/api"
|
2019-01-19 00:02:10 +00:00
|
|
|
"srs.epita.fr/fic-server/admin/sync"
|
2018-12-08 20:00:00 +00:00
|
|
|
"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"
|
2016-12-15 23:51:56 +00:00
|
|
|
)
|
|
|
|
|
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)
|
2016-12-15 23:51:56 +00:00
|
|
|
})
|
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)
|
2016-12-15 23:51:56 +00:00
|
|
|
})
|
2022-05-16 09:38:46 +00:00
|
|
|
router.GET("/events/*_", func(c *gin.Context) {
|
|
|
|
serveIndex(c)
|
2016-12-15 23:51:56 +00:00
|
|
|
})
|
2022-05-16 09:38:46 +00:00
|
|
|
router.GET("/files", func(c *gin.Context) {
|
|
|
|
serveIndex(c)
|
2017-12-27 00:53:01 +00:00
|
|
|
})
|
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
|
|
|
})
|
2023-10-23 10:03:40 +00:00
|
|
|
router.GET("/repositories", func(c *gin.Context) {
|
|
|
|
serveIndex(c)
|
|
|
|
})
|
2022-05-19 19:15:25 +00:00
|
|
|
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)
|
2016-12-15 23:51:56 +00:00
|
|
|
})
|
2022-05-16 09:38:46 +00:00
|
|
|
router.GET("/themes/*_", func(c *gin.Context) {
|
|
|
|
serveIndex(c)
|
2016-12-15 23:51:56 +00:00
|
|
|
})
|
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))
|
2016-12-15 23:51:56 +00:00
|
|
|
})
|
2022-05-16 09:38:46 +00:00
|
|
|
router.GET("/fonts/*_", func(c *gin.Context) {
|
|
|
|
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
|
2016-12-15 23:51:56 +00:00
|
|
|
})
|
2022-05-16 09:38:46 +00:00
|
|
|
router.GET("/img/*_", func(c *gin.Context) {
|
|
|
|
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
|
2016-12-15 23:51:56 +00:00
|
|
|
})
|
2022-05-16 09:38:46 +00:00
|
|
|
router.GET("/js/*_", func(c *gin.Context) {
|
|
|
|
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
|
2016-12-15 23:51:56 +00:00
|
|
|
})
|
2022-05-16 09:38:46 +00:00
|
|
|
router.GET("/views/*_", func(c *gin.Context) {
|
|
|
|
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
|
2016-12-15 23:51:56 +00:00
|
|
|
})
|
2017-12-12 06:11:56 +00:00
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
router.GET("/files/*_", func(c *gin.Context) {
|
2022-11-21 11:02:36 +00:00
|
|
|
// 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"))))
|
2018-12-08 20:00:00 +00:00
|
|
|
})
|
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"))))
|
2019-01-19 00:02:10 +00:00
|
|
|
} else {
|
2022-05-16 09:38:46 +00:00
|
|
|
c.AbortWithError(http.StatusBadRequest, errors.New("Only available with local importer."))
|
2019-01-19 00:02:10 +00:00
|
|
|
}
|
|
|
|
})
|
2018-12-08 20:00:00 +00:00
|
|
|
|
2022-05-16 09:38:46 +00:00
|
|
|
router.GET("/check_import.html", func(c *gin.Context) {
|
|
|
|
serveFile(c, "check_import.html")
|
2017-12-12 06:11:56 +00:00
|
|
|
})
|
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)
|
2017-12-12 06:11:56 +00:00
|
|
|
})
|
2016-01-13 00:22:01 +00:00
|
|
|
}
|