admin: can change the baseurl interface

This commit is contained in:
nemunaire 2016-11-19 16:26:23 +01:00
parent aa7afe1a6a
commit 5fa19223a9
8 changed files with 82 additions and 329 deletions

View file

@ -6,7 +6,9 @@ import (
"log"
"net/http"
"os"
"path"
"path/filepath"
"text/template"
"srs.epita.fr/fic-server/libfic"
)
@ -21,7 +23,7 @@ var CloudPassword string
func main() {
var bind = flag.String("bind", "127.0.0.1:8081", "Bind port/socket")
var dsn = flag.String("dsn", "fic:fic@/fic", "DSN to connect to the MySQL server")
flag.StringVar(&BaseURL, "baseurl", "http://fic.srs.epita.fr/", "URL prepended to each URL")
flag.StringVar(&BaseURL, "baseurl", "/", "URL prepended to each URL")
flag.StringVar(&SubmissionDir, "submission", "./submissions/", "Base directory where save submissions")
flag.StringVar(&PKIDir, "pki", "./pki/", "Base directory where found PKI scripts")
flag.StringVar(&fic.FilesDir, "files", "./FILES/", "Base directory where found challenges files, local part")
@ -63,14 +65,24 @@ func main() {
log.Fatal("Cannot create database: ", err)
}
log.Println("Changing base url...")
if file, err := os.OpenFile(path.Join(staticDir, "index.html"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.FileMode(0644)); err != nil {
log.Println("Unable to open index.html: ", err)
} else if indexTmpl, err := template.New("index").Parse(indextpl); err != nil {
log.Println("Cannot create template: ", err)
} else if err := indexTmpl.Execute(file, map[string]string{"urlbase": path.Clean(path.Join(BaseURL, "nuke"))[:len(path.Clean(path.Join(BaseURL, "nuke"))) - 4]}); err != nil {
log.Println("An error occurs during template execution: ", err)
}
os.Chdir(PKIDir)
log.Println("Registering handlers...")
mux := http.NewServeMux()
mux.Handle("/api/", http.StripPrefix("/api", ApiHandler()))
mux.Handle("/teams/", StaticHandler(staticDir))
mux.Handle("/themes/", StaticHandler(staticDir))
mux.Handle("/", http.FileServer(http.Dir(staticDir)))
mux.Handle(path.Join(BaseURL, "api") + "/", http.StripPrefix(path.Join(BaseURL, "api"), ApiHandler()))
mux.Handle(path.Join(BaseURL, "teams") + "/", http.StripPrefix(BaseURL, StaticHandler(staticDir)))
mux.Handle(path.Join(BaseURL, "themes") + "/", http.StripPrefix(BaseURL, StaticHandler(staticDir)))
mux.Handle(BaseURL, http.StripPrefix(BaseURL, http.FileServer(http.Dir(staticDir))))
log.Println(fmt.Sprintf("Ready, listening on %s", *bind))
if err := http.ListenAndServe(*bind, mux); err != nil {