2018-11-21 04:19:57 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-09-06 22:25:56 +00:00
|
|
|
"bytes"
|
2022-06-06 08:47:38 +00:00
|
|
|
"embed"
|
2018-11-21 04:19:57 +00:00
|
|
|
"fmt"
|
2019-09-06 22:25:56 +00:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2018-11-21 04:19:57 +00:00
|
|
|
"net/http"
|
|
|
|
"path"
|
2019-01-19 02:08:07 +00:00
|
|
|
"strings"
|
2018-11-21 04:19:57 +00:00
|
|
|
"time"
|
|
|
|
|
2019-01-19 02:08:07 +00:00
|
|
|
"srs.epita.fr/fic-server/libfic"
|
2018-11-21 04:19:57 +00:00
|
|
|
"srs.epita.fr/fic-server/settings"
|
|
|
|
|
2022-06-06 08:47:38 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2018-11-21 04:19:57 +00:00
|
|
|
)
|
|
|
|
|
2022-06-06 08:47:38 +00:00
|
|
|
//go:embed static
|
|
|
|
|
|
|
|
var assets embed.FS
|
|
|
|
var staticFS http.FileSystem
|
|
|
|
|
2019-01-21 10:36:52 +00:00
|
|
|
var forwarder *string = nil
|
|
|
|
var fwdPublicJson = false
|
|
|
|
|
2019-09-06 22:25:56 +00:00
|
|
|
var indexTmpl []byte
|
|
|
|
|
2022-06-06 08:47:38 +00:00
|
|
|
func getIndexHtml(w io.Writer, baseURL string) {
|
2019-09-06 22:25:56 +00:00
|
|
|
if len(indexTmpl) == 0 {
|
2022-06-06 08:47:38 +00:00
|
|
|
if file, err := staticFS.Open("index.html"); err != nil {
|
2019-09-06 22:25:56 +00:00
|
|
|
log.Println("Unable to open index.html: ", err)
|
|
|
|
} else {
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if indexTmpl, err = ioutil.ReadAll(file); err != nil {
|
|
|
|
log.Println("Cannot read whole index.html: ", err)
|
|
|
|
} else {
|
2022-06-06 08:47:38 +00:00
|
|
|
indexTmpl = bytes.Replace(indexTmpl, []byte("{{.urlbase}}"), []byte(path.Clean(path.Join(baseURL+"/", "nuke"))[:len(path.Clean(path.Join(baseURL+"/", "nuke")))-4]), -1)
|
2019-09-06 22:25:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(indexTmpl)
|
|
|
|
}
|
|
|
|
|
2022-06-06 08:47:38 +00:00
|
|
|
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)
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
2019-01-22 23:17:46 +00:00
|
|
|
for i := 0; i <= 9; i++ {
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET(fmt.Sprintf("/public%d.html", i), func(c *gin.Context) {
|
|
|
|
getIndexHtml(c.Writer, baseURL)
|
2019-01-22 23:17:46 +00:00
|
|
|
})
|
|
|
|
}
|
2018-11-21 04:19:57 +00:00
|
|
|
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/css/*_", func(c *gin.Context) {
|
|
|
|
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/fonts/*_", func(c *gin.Context) {
|
|
|
|
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/img/*_", func(c *gin.Context) {
|
|
|
|
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/js/*_", func(c *gin.Context) {
|
|
|
|
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/views/*_", func(c *gin.Context) {
|
|
|
|
serveFile(c, strings.TrimPrefix(c.Request.URL.Path, baseURL))
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
|
|
|
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/files/*_", func(c *gin.Context) {
|
2019-01-21 10:36:52 +00:00
|
|
|
if forwarder != nil {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, *forwarder)
|
2019-01-21 10:36:52 +00:00
|
|
|
} else {
|
2022-06-08 01:22:29 +00:00
|
|
|
http.ServeFile(c.Writer, c.Request, path.Join(fic.FilesDir, strings.TrimPrefix(c.Request.URL.Path, "/"+path.Join(baseURL, "files"))))
|
2019-01-21 10:36:52 +00:00
|
|
|
}
|
2019-01-19 02:08:07 +00:00
|
|
|
})
|
|
|
|
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/events.json", func(c *gin.Context) {
|
|
|
|
c.Writer.Header().Set("Cache-Control", "no-cache")
|
2019-01-21 10:36:52 +00:00
|
|
|
if forwarder != nil {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, *forwarder)
|
2019-01-21 10:36:52 +00:00
|
|
|
} else {
|
2022-06-06 08:47:38 +00:00
|
|
|
http.ServeFile(c.Writer, c.Request, path.Join(TeamsDir, "events.json"))
|
2019-01-21 10:36:52 +00:00
|
|
|
}
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/my.json", func(c *gin.Context) {
|
|
|
|
c.Writer.Header().Set("Cache-Control", "no-cache")
|
2019-01-21 10:36:52 +00:00
|
|
|
if forwarder != nil {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, *forwarder)
|
2019-01-21 10:36:52 +00:00
|
|
|
} else {
|
2022-06-06 08:47:38 +00:00
|
|
|
http.ServeFile(c.Writer, c.Request, path.Join(TeamsDir, "public", "my.json"))
|
2019-01-21 10:36:52 +00:00
|
|
|
}
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/api/teams/:tid/score-grid.json", func(c *gin.Context) {
|
2019-01-21 10:36:52 +00:00
|
|
|
if forwarder != nil {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, *forwarder)
|
2019-01-21 10:36:52 +00:00
|
|
|
} else {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, "http://127.0.0.1:8081/")
|
2019-01-21 10:36:52 +00:00
|
|
|
}
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/api/teams/:tid/stats.json", func(c *gin.Context) {
|
2019-01-21 10:36:52 +00:00
|
|
|
if forwarder != nil {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, *forwarder)
|
2019-01-21 10:36:52 +00:00
|
|
|
} else {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, "http://127.0.0.1:8081/")
|
2019-01-21 10:36:52 +00:00
|
|
|
}
|
2019-01-19 07:01:29 +00:00
|
|
|
})
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/challenge.json", func(c *gin.Context) {
|
|
|
|
c.Writer.Header().Set("Cache-Control", "no-cache")
|
2022-05-01 20:33:59 +00:00
|
|
|
if forwarder != nil {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, *forwarder)
|
2022-05-01 20:33:59 +00:00
|
|
|
} else {
|
2022-06-06 08:47:38 +00:00
|
|
|
http.ServeFile(c.Writer, c.Request, path.Join(settings.SettingsDir, settings.ChallengeFile))
|
2022-05-01 20:33:59 +00:00
|
|
|
}
|
|
|
|
})
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/settings.json", func(c *gin.Context) {
|
|
|
|
c.Writer.Header().Set("Cache-Control", "no-cache")
|
2019-01-21 10:36:52 +00:00
|
|
|
if forwarder != nil {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, *forwarder)
|
2019-01-21 10:36:52 +00:00
|
|
|
} else {
|
2022-06-06 08:47:38 +00:00
|
|
|
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))
|
2019-01-21 10:36:52 +00:00
|
|
|
}
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/teams.json", func(c *gin.Context) {
|
|
|
|
c.Writer.Header().Set("Cache-Control", "no-cache")
|
2019-01-21 10:36:52 +00:00
|
|
|
if forwarder != nil {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, *forwarder)
|
2019-01-21 10:36:52 +00:00
|
|
|
} else {
|
2022-06-06 08:47:38 +00:00
|
|
|
http.ServeFile(c.Writer, c.Request, path.Join(TeamsDir, "public", "teams.json"))
|
2019-01-21 10:36:52 +00:00
|
|
|
}
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/themes.json", func(c *gin.Context) {
|
|
|
|
c.Writer.Header().Set("Cache-Control", "no-cache")
|
2019-01-21 10:36:52 +00:00
|
|
|
if forwarder != nil {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, *forwarder)
|
2019-01-21 10:36:52 +00:00
|
|
|
} else {
|
2022-06-06 08:47:38 +00:00
|
|
|
http.ServeFile(c.Writer, c.Request, path.Join(TeamsDir, "themes.json"))
|
2019-01-21 10:36:52 +00:00
|
|
|
}
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
|
|
|
|
2023-05-12 13:41:43 +00:00
|
|
|
router.GET("/background.png", func(c *gin.Context) {
|
|
|
|
if forwarder != nil && fwdPublicJson {
|
|
|
|
fwd_request(c.Writer, c.Request, *forwarder)
|
|
|
|
} else {
|
|
|
|
http.ServeFile(c.Writer, c.Request, path.Join(DashboardDir, "background.png"))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/public.json", func(c *gin.Context) {
|
|
|
|
c.Writer.Header().Set("Cache-Control", "no-cache")
|
2019-01-21 10:36:52 +00:00
|
|
|
if forwarder != nil && fwdPublicJson {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, *forwarder)
|
2019-01-21 10:36:52 +00:00
|
|
|
} else {
|
2022-06-06 08:47:38 +00:00
|
|
|
http.ServeFile(c.Writer, c.Request, path.Join(DashboardDir, "public.json"))
|
2019-01-21 10:36:52 +00:00
|
|
|
}
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/public0.json", func(c *gin.Context) {
|
|
|
|
c.Writer.Header().Set("Cache-Control", "no-cache")
|
2019-01-21 10:36:52 +00:00
|
|
|
if forwarder != nil && fwdPublicJson {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, *forwarder)
|
2019-01-21 10:36:52 +00:00
|
|
|
} else {
|
2022-06-06 08:47:38 +00:00
|
|
|
http.ServeFile(c.Writer, c.Request, path.Join(DashboardDir, "public0.json"))
|
2019-01-21 10:36:52 +00:00
|
|
|
}
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/public1.json", func(c *gin.Context) {
|
|
|
|
c.Writer.Header().Set("Cache-Control", "no-cache")
|
2019-01-21 10:36:52 +00:00
|
|
|
if forwarder != nil && fwdPublicJson {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, *forwarder)
|
2019-01-21 10:36:52 +00:00
|
|
|
} else {
|
2022-06-06 08:47:38 +00:00
|
|
|
http.ServeFile(c.Writer, c.Request, path.Join(DashboardDir, "public1.json"))
|
2019-01-21 10:36:52 +00:00
|
|
|
}
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/public2.json", func(c *gin.Context) {
|
|
|
|
c.Writer.Header().Set("Cache-Control", "no-cache")
|
2019-01-21 10:36:52 +00:00
|
|
|
if forwarder != nil && fwdPublicJson {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, *forwarder)
|
2019-01-21 10:36:52 +00:00
|
|
|
} else {
|
2022-06-06 08:47:38 +00:00
|
|
|
http.ServeFile(c.Writer, c.Request, path.Join(DashboardDir, "public2.json"))
|
2019-01-21 10:36:52 +00:00
|
|
|
}
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/public3.json", func(c *gin.Context) {
|
|
|
|
c.Writer.Header().Set("Cache-Control", "no-cache")
|
2019-01-21 10:36:52 +00:00
|
|
|
if forwarder != nil && fwdPublicJson {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, *forwarder)
|
2019-01-21 10:36:52 +00:00
|
|
|
} else {
|
2022-06-06 08:47:38 +00:00
|
|
|
http.ServeFile(c.Writer, c.Request, path.Join(DashboardDir, "public3.json"))
|
2019-01-21 10:36:52 +00:00
|
|
|
}
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/public4.json", func(c *gin.Context) {
|
|
|
|
c.Writer.Header().Set("Cache-Control", "no-cache")
|
2019-01-21 10:36:52 +00:00
|
|
|
if forwarder != nil && fwdPublicJson {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, *forwarder)
|
2019-01-21 10:36:52 +00:00
|
|
|
} else {
|
2022-06-06 08:47:38 +00:00
|
|
|
http.ServeFile(c.Writer, c.Request, path.Join(DashboardDir, "public4.json"))
|
2019-01-21 10:36:52 +00:00
|
|
|
}
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/public5.json", func(c *gin.Context) {
|
|
|
|
c.Writer.Header().Set("Cache-Control", "no-cache")
|
2019-01-21 10:36:52 +00:00
|
|
|
if forwarder != nil && fwdPublicJson {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, *forwarder)
|
2019-01-21 10:36:52 +00:00
|
|
|
} else {
|
2022-06-06 08:47:38 +00:00
|
|
|
http.ServeFile(c.Writer, c.Request, path.Join(DashboardDir, "public5.json"))
|
2019-01-21 10:36:52 +00:00
|
|
|
}
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/public6.json", func(c *gin.Context) {
|
|
|
|
c.Writer.Header().Set("Cache-Control", "no-cache")
|
2019-01-21 10:36:52 +00:00
|
|
|
if forwarder != nil && fwdPublicJson {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, *forwarder)
|
2019-01-21 10:36:52 +00:00
|
|
|
} else {
|
2022-06-06 08:47:38 +00:00
|
|
|
http.ServeFile(c.Writer, c.Request, path.Join(DashboardDir, "public6.json"))
|
2019-01-21 10:36:52 +00:00
|
|
|
}
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/public7.json", func(c *gin.Context) {
|
|
|
|
c.Writer.Header().Set("Cache-Control", "no-cache")
|
2019-01-21 10:36:52 +00:00
|
|
|
if forwarder != nil && fwdPublicJson {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, *forwarder)
|
2019-01-21 10:36:52 +00:00
|
|
|
} else {
|
2022-06-06 08:47:38 +00:00
|
|
|
http.ServeFile(c.Writer, c.Request, path.Join(DashboardDir, "public7.json"))
|
2019-01-21 10:36:52 +00:00
|
|
|
}
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/public8.json", func(c *gin.Context) {
|
|
|
|
c.Writer.Header().Set("Cache-Control", "no-cache")
|
2019-01-21 10:36:52 +00:00
|
|
|
if forwarder != nil && fwdPublicJson {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, *forwarder)
|
2019-01-21 10:36:52 +00:00
|
|
|
} else {
|
2022-06-06 08:47:38 +00:00
|
|
|
http.ServeFile(c.Writer, c.Request, path.Join(DashboardDir, "public8.json"))
|
2019-01-21 10:36:52 +00:00
|
|
|
}
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
2022-06-06 08:47:38 +00:00
|
|
|
router.GET("/public9.json", func(c *gin.Context) {
|
|
|
|
c.Writer.Header().Set("Cache-Control", "no-cache")
|
2019-01-21 10:36:52 +00:00
|
|
|
if forwarder != nil && fwdPublicJson {
|
2022-06-06 08:47:38 +00:00
|
|
|
fwd_request(c.Writer, c.Request, *forwarder)
|
2019-01-21 10:36:52 +00:00
|
|
|
} else {
|
2022-06-06 08:47:38 +00:00
|
|
|
http.ServeFile(c.Writer, c.Request, path.Join(DashboardDir, "public9.json"))
|
2019-01-21 10:36:52 +00:00
|
|
|
}
|
2018-11-21 04:19:57 +00:00
|
|
|
})
|
|
|
|
}
|