server/dashboard/static.go

233 lines
7.9 KiB
Go
Raw Normal View History

2018-11-21 04:19:57 +00:00
package main
import (
"bytes"
2018-11-21 04:19:57 +00:00
"fmt"
"io"
"io/ioutil"
"log"
2018-11-21 04:19:57 +00:00
"net/http"
"os"
2018-11-21 04:19:57 +00:00
"path"
2019-01-19 02:08:07 +00:00
"strings"
2018-11-21 04:19:57 +00:00
"time"
"srs.epita.fr/fic-server/dashboard/api"
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"
"github.com/julienschmidt/httprouter"
)
var BaseURL = "/"
var forwarder *string = nil
var fwdPublicJson = false
var indexTmpl []byte
func getIndexHtml(w io.Writer) {
if len(indexTmpl) == 0 {
if file, err := os.Open(path.Join(StaticDir, "index.html")); err != nil {
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 {
indexTmpl = bytes.Replace(indexTmpl, []byte("{{.urlbase}}"), []byte(path.Clean(path.Join(BaseURL+"/", "nuke"))[:len(path.Clean(path.Join(BaseURL+"/", "nuke")))-4]), -1)
}
}
}
w.Write(indexTmpl)
}
2018-11-21 04:19:57 +00:00
func init() {
api.Router().GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.Redirect(w, r, "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++ {
api.Router().GET(fmt.Sprintf("/public%d.html", i), func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
getIndexHtml(w)
2019-01-22 23:17:46 +00:00
})
}
2018-11-21 04:19:57 +00:00
api.Router().GET("/css/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, r.URL.Path))
})
api.Router().GET("/fonts/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, r.URL.Path))
})
api.Router().GET("/img/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, r.URL.Path))
})
api.Router().GET("/js/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, r.URL.Path))
})
api.Router().GET("/views/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, r.URL.Path))
})
2019-01-19 02:08:07 +00:00
api.Router().GET("/files/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if forwarder != nil {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(fic.FilesDir, strings.TrimPrefix(r.URL.Path, "/files")))
}
2019-01-19 02:08:07 +00:00
})
2018-11-21 04:19:57 +00:00
api.Router().GET("/events.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
if forwarder != nil {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(TeamsDir, "events.json"))
}
2018-11-21 04:19:57 +00:00
})
api.Router().GET("/my.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
if forwarder != nil {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(TeamsDir, "public", "my.json"))
}
2018-11-21 04:19:57 +00:00
})
2020-01-30 03:17:53 +00:00
api.Router().GET("/api/teams/:tid/score-grid.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if forwarder != nil {
fwd_request(w, r, *forwarder)
} else {
2020-01-30 03:17:53 +00:00
fwd_request(w, r, "http://127.0.0.1:8081/")
}
2018-11-21 04:19:57 +00:00
})
2019-01-19 07:01:29 +00:00
api.Router().GET("/api/teams/:tid/stats.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if forwarder != nil {
fwd_request(w, r, *forwarder)
} else {
fwd_request(w, r, "http://127.0.0.1:8081/")
}
2019-01-19 07:01:29 +00:00
})
2022-05-01 20:33:59 +00:00
api.Router().GET("/challenge.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
if forwarder != nil {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(settings.SettingsDir, settings.ChallengeFile))
}
})
2018-11-21 04:19:57 +00:00
api.Router().GET("/settings.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
if forwarder != nil {
fwd_request(w, r, *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))
}
2018-11-21 04:19:57 +00:00
})
api.Router().GET("/teams.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
if forwarder != nil {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(TeamsDir, "public", "teams.json"))
}
2018-11-21 04:19:57 +00:00
})
api.Router().GET("/themes.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
if forwarder != nil {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(TeamsDir, "themes.json"))
}
2018-11-21 04:19:57 +00:00
})
api.Router().GET("/public.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public.json"))
}
2018-11-21 04:19:57 +00:00
})
api.Router().GET("/public0.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public0.json"))
}
2018-11-21 04:19:57 +00:00
})
api.Router().GET("/public1.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public1.json"))
}
2018-11-21 04:19:57 +00:00
})
api.Router().GET("/public2.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public2.json"))
}
2018-11-21 04:19:57 +00:00
})
api.Router().GET("/public3.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public3.json"))
}
2018-11-21 04:19:57 +00:00
})
api.Router().GET("/public4.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public4.json"))
}
2018-11-21 04:19:57 +00:00
})
api.Router().GET("/public5.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public5.json"))
}
2018-11-21 04:19:57 +00:00
})
api.Router().GET("/public6.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public6.json"))
}
2018-11-21 04:19:57 +00:00
})
api.Router().GET("/public7.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public7.json"))
}
2018-11-21 04:19:57 +00:00
})
api.Router().GET("/public8.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public8.json"))
}
2018-11-21 04:19:57 +00:00
})
api.Router().GET("/public9.json", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Cache-Control", "no-cache")
if forwarder != nil && fwdPublicJson {
fwd_request(w, r, *forwarder)
} else {
http.ServeFile(w, r, path.Join(DashboardDir, "public9.json"))
}
2018-11-21 04:19:57 +00:00
})
}