server/qa/static.go

67 lines
1.9 KiB
Go

package main
import (
"bytes"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"srs.epita.fr/fic-server/qa/api"
"github.com/julienschmidt/httprouter"
)
var BaseURL = "/"
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)
}
func init() {
api.Router().GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
getIndexHtml(w)
})
api.Router().GET("/exercices/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
getIndexHtml(w)
})
api.Router().GET("/themes/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
getIndexHtml(w)
})
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))
})
}