Embed the static files.
This commit is contained in:
parent
b740896359
commit
acc9175b07
79
src/assetstatic/asset_build.go
Normal file
79
src/assetstatic/asset_build.go
Normal file
File diff suppressed because one or more lines are too long
48
src/assetstatic/asset_dev.go
Normal file
48
src/assetstatic/asset_dev.go
Normal file
@ -0,0 +1,48 @@
|
||||
// +build !embed
|
||||
|
||||
// Automagically generated by yaber v0.2 (https://github.com/lmas/yaber),
|
||||
// please avoid editting this file as it might be regenerated again.
|
||||
|
||||
package assetstatic
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func Asset(path string) ([]byte, error) {
|
||||
return ioutil.ReadFile(path)
|
||||
}
|
||||
|
||||
func AssetDir(dir string) (map[string][]byte, error) {
|
||||
list := make(map[string][]byte)
|
||||
dirs := []string{dir}
|
||||
|
||||
for len(dirs) > 0 {
|
||||
d := dirs[0]
|
||||
dirs = dirs[1:]
|
||||
files, e := ioutil.ReadDir(d)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
|
||||
for _, f := range files {
|
||||
fpath := filepath.Join(d, f.Name())
|
||||
|
||||
if f.IsDir() {
|
||||
dirs = append(dirs, fpath)
|
||||
continue
|
||||
}
|
||||
if !f.Mode().IsRegular() {
|
||||
continue
|
||||
}
|
||||
|
||||
fbody, e := ioutil.ReadFile(fpath)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
list[fpath] = fbody
|
||||
}
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
20
src/web.go
20
src/web.go
@ -1,13 +1,16 @@
|
||||
package ss13
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"mime"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/lmas/ss13_se/src/assetstatic"
|
||||
"github.com/lmas/ss13_se/src/assettemplates"
|
||||
)
|
||||
|
||||
@ -55,9 +58,22 @@ func (i *Instance) Serve(addr string) error {
|
||||
}
|
||||
i.router.SetHTMLTemplate(tmpl)
|
||||
|
||||
// Setup all URLS
|
||||
i.router.Static("/static", "./static")
|
||||
// Load static files
|
||||
staticfiles, e := assetstatic.AssetDir("static/")
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
for p, _ := range staticfiles {
|
||||
ctype := mime.TypeByExtension(filepath.Ext(p))
|
||||
// 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]
|
||||
i.router.GET(fmt.Sprintf("/%s", p), func(c *gin.Context) {
|
||||
c.Data(http.StatusOK, ctype, b)
|
||||
})
|
||||
}
|
||||
|
||||
// Setup all URLS
|
||||
i.router.GET("/", i.page_index)
|
||||
|
||||
i.router.GET("/server/:server_id/*slug", i.page_server)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user