This repository has been archived on 2024-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
atsebay.t/static-dev.go

72 lines
2.4 KiB
Go

// +build dev
package main
import (
"flag"
"net/http"
"os"
"path"
"path/filepath"
"github.com/julienschmidt/httprouter"
)
var StaticDir string = "htdocs/"
func init() {
Router().GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
})
Router().GET("/auth", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
})
Router().GET("/grades", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
})
Router().GET("/surveys", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
})
Router().GET("/surveys/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
})
Router().GET("/users", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
})
Router().GET("/users/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
})
Router().GET("/css/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
serveStaticAsset(w, r)
})
Router().GET("/fonts/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
serveStaticAsset(w, r)
})
Router().GET("/img/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
serveStaticAsset(w, r)
})
Router().GET("/js/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
serveStaticAsset(w, r)
})
Router().GET("/views/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
serveStaticAsset(w, r)
})
flag.StringVar(&StaticDir, "static", StaticDir, "Directory containing static files")
}
func serveStaticAsset(w http.ResponseWriter, r *http.Request) {
http.StripPrefix("/", http.FileServer(http.Dir(StaticDir))).ServeHTTP(w, r)
}
func sanitizeStaticOptions() error {
StaticDir, _ := filepath.Abs(StaticDir)
if _, err := os.Stat(StaticDir); os.IsNotExist(err) {
StaticDir, _ = filepath.Abs(filepath.Join(filepath.Dir(os.Args[0]), "htdocs"))
if _, err := os.Stat(StaticDir); os.IsNotExist(err) {
return err
}
}
return nil
}