Clean up template loading and add handler for static files.
This commit is contained in:
parent
3fb5a6a3e9
commit
5adf029b4c
14
src/utils.go
14
src/utils.go
@ -1,12 +1,26 @@
|
|||||||
package ss13
|
package ss13
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
now = time.Now()
|
now = time.Now()
|
||||||
|
|
||||||
|
funcmap = template.FuncMap{
|
||||||
|
// safe_href let's us use URLs with custom protocols
|
||||||
|
"safe_href": func(s string) template.HTMLAttr {
|
||||||
|
return template.HTMLAttr(`href="` + s + `"`)
|
||||||
|
},
|
||||||
|
"inms": func(t time.Time) int64 {
|
||||||
|
return t.Unix() * 1000
|
||||||
|
},
|
||||||
|
"year": func() int {
|
||||||
|
return time.Now().Year()
|
||||||
|
},
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func LogError(err error) bool {
|
func LogError(err error) bool {
|
||||||
|
|||||||
100
src/web.go
100
src/web.go
@ -5,6 +5,7 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
"mime"
|
"mime"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
@ -13,6 +14,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func New(debug bool, path string) (*Instance, error) {
|
func New(debug bool, path string) (*Instance, error) {
|
||||||
|
// WHen in debug mode we load the assets from disk instead of the
|
||||||
|
// embedded ones.
|
||||||
|
SetRawAssets(debug)
|
||||||
|
|
||||||
|
tmpl := template.New("AllTemplates").Funcs(funcmap)
|
||||||
|
tmplfiles, err := AssetDir("templates/")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
for p, b := range tmplfiles {
|
||||||
|
name := filepath.Base(p)
|
||||||
|
template.Must(tmpl.New(name).Parse(string(b)))
|
||||||
|
}
|
||||||
|
|
||||||
c, e := LoadConfig(path)
|
c, e := LoadConfig(path)
|
||||||
if e != nil {
|
if e != nil {
|
||||||
return nil, e
|
return nil, e
|
||||||
@ -28,9 +43,19 @@ func New(debug bool, path string) (*Instance, error) {
|
|||||||
Config: c,
|
Config: c,
|
||||||
Debug: debug,
|
Debug: debug,
|
||||||
|
|
||||||
db: db,
|
db: db,
|
||||||
|
router: mux.NewRouter().StrictSlash(true),
|
||||||
|
tmpls: tmpl,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
i.router.HandleFunc("/", i.page_index)
|
||||||
|
i.router.HandleFunc("/about", i.page_about)
|
||||||
|
i.router.HandleFunc("/r/ver", i.page_apollo)
|
||||||
|
i.router.HandleFunc("/server/{id}", i.page_server)
|
||||||
|
i.router.HandleFunc("/server/{id}/{slug}", i.page_server)
|
||||||
|
i.router.HandleFunc("/static/{file:.*}", i.page_static)
|
||||||
|
i.router.NotFoundHandler = http.HandlerFunc(i.page_404)
|
||||||
|
|
||||||
return &i, nil
|
return &i, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,65 +72,6 @@ func (i *Instance) Run() error {
|
|||||||
time.Sleep(td)
|
time.Sleep(td)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
i.router = mux.NewRouter().StrictSlash(true)
|
|
||||||
i.router.NotFoundHandler = http.HandlerFunc(i.page_404)
|
|
||||||
|
|
||||||
// Custom template functions
|
|
||||||
funcmap := template.FuncMap{
|
|
||||||
// safe_href let's us use URLs with custom protocols
|
|
||||||
"safe_href": func(s string) template.HTMLAttr {
|
|
||||||
return template.HTMLAttr(`href="` + s + `"`)
|
|
||||||
},
|
|
||||||
"inms": func(t time.Time) int64 {
|
|
||||||
return t.Unix() * 1000
|
|
||||||
},
|
|
||||||
"year": func() int {
|
|
||||||
return time.Now().Year()
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// WHen in debug mode we load the assets from disk instead of the
|
|
||||||
// embedded ones.
|
|
||||||
SetRawAssets(i.Debug)
|
|
||||||
|
|
||||||
// Load templates
|
|
||||||
tmpl := template.New("AllTemplates").Funcs(funcmap)
|
|
||||||
tmplfiles, err := AssetDir("templates/")
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
for p, b := range tmplfiles {
|
|
||||||
name := filepath.Base(p)
|
|
||||||
template.Must(tmpl.New(name).Parse(string(b)))
|
|
||||||
}
|
|
||||||
i.tmpls = tmpl
|
|
||||||
|
|
||||||
// Load static files
|
|
||||||
staticfiles, e := AssetDir("static/")
|
|
||||||
if e != nil {
|
|
||||||
panic(e)
|
|
||||||
}
|
|
||||||
for p, _ := range staticfiles {
|
|
||||||
// Need to make a local copy of the var or else all files will
|
|
||||||
// return the content of a single file (quirk with range).
|
|
||||||
b := staticfiles[p]
|
|
||||||
ctype := mime.TypeByExtension(filepath.Ext(p))
|
|
||||||
i.router.HandleFunc(fmt.Sprintf("/%s", p),
|
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Header().Add("Content-Type", ctype)
|
|
||||||
_, e := w.Write(b)
|
|
||||||
LogError(e)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Setup all URLS
|
|
||||||
i.router.HandleFunc("/", i.page_index)
|
|
||||||
i.router.HandleFunc("/about", i.page_about)
|
|
||||||
i.router.HandleFunc("/r/ver", i.page_apollo)
|
|
||||||
i.router.HandleFunc("/server/{id}", i.page_server)
|
|
||||||
i.router.HandleFunc("/server/{id}/{slug}", i.page_server)
|
|
||||||
|
|
||||||
return http.ListenAndServe(i.Config.ListenAddr, i.router)
|
return http.ListenAndServe(i.Config.ListenAddr, i.router)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,6 +80,20 @@ func (i *Instance) page_404(w http.ResponseWriter, r *http.Request) {
|
|||||||
i.tmpls.ExecuteTemplate(w, "page_404.html", nil)
|
i.tmpls.ExecuteTemplate(w, "page_404.html", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *Instance) page_static(w http.ResponseWriter, r *http.Request) {
|
||||||
|
p := path.Join("static", mux.Vars(r)["file"])
|
||||||
|
b, e := Asset(p)
|
||||||
|
if LogError(e) {
|
||||||
|
i.page_404(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctype := mime.TypeByExtension(filepath.Ext(p))
|
||||||
|
w.Header().Add("Content-Type", ctype)
|
||||||
|
_, e = w.Write(b)
|
||||||
|
LogError(e)
|
||||||
|
}
|
||||||
|
|
||||||
func (i *Instance) page_index(w http.ResponseWriter, r *http.Request) {
|
func (i *Instance) page_index(w http.ResponseWriter, r *http.Request) {
|
||||||
servers := i.db.AllServers()
|
servers := i.db.AllServers()
|
||||||
i.tmpls.ExecuteTemplate(w, "page_index.html", D{
|
i.tmpls.ExecuteTemplate(w, "page_index.html", D{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user