Add bindata

This commit is contained in:
Crow Crowcrow 2017-12-13 06:47:26 +01:00
parent f9ed3ac363
commit 201201edf3
No known key found for this signature in database
GPG Key ID: 1CC867E990E45038
6 changed files with 54 additions and 29 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
bindata.go

9
Makefile Normal file
View File

@ -0,0 +1,9 @@
NAME=server
.PHONY: generate
generate:
@go generate ./...
.PHONY: build
build: generate
@go build -o $(NAME) server/*.go

3
internal/static/main.go Normal file
View File

@ -0,0 +1,3 @@
package static
//go:generate go-bindata -pkg $GOPACKAGE --prefix "../../static/" ../../static/...

View File

@ -0,0 +1,3 @@
package templates
//go:generate go-bindata -pkg $GOPACKAGE --prefix "../../templates/" ../../templates/...

View File

@ -9,13 +9,12 @@ func main() {
t.Reset() t.Reset()
e.Renderer = &t e.Renderer = &t
e.Static("/static", "static/")
e.GET("/", index) e.GET("/", index)
e.GET("/static/*", getStatic)
e.Logger.Fatal(e.Start(":8080")) e.Logger.Fatal(e.Start(":8080"))
} }
func index(c echo.Context) error { func index(c echo.Context) error {
return c.Render(200, "index", nil) return c.Render(200, "pages/index", nil)
} }

View File

@ -4,51 +4,46 @@ import (
"fmt" "fmt"
"html/template" "html/template"
"io" "io"
"log" "mime"
"os"
"path" "path"
"path/filepath" "path/filepath"
"strings" "strings"
"git.fuyu.moe/Crow/echoBoilerplate/internal/static"
"git.fuyu.moe/Crow/echoBoilerplate/internal/templates"
"github.com/labstack/echo" "github.com/labstack/echo"
) )
// Reset reads all the templates // Reset reads all the templates
func (t *Template) Reset() { func (t *Template) Reset() {
pagesPath := `templates/pages/`
componentsPath := `templates/components/*.gohtml`
if t.templates == nil { if t.templates == nil {
t.templates = make(map[string]*template.Template) t.templates = make(map[string]*template.Template)
} }
components := template.New(``)
pages := []string{} pages := []string{}
err := filepath.Walk(pagesPath, func(path string, f os.FileInfo, err error) error { for _, v := range templates.AssetNames() {
if strings.HasPrefix(v, `pages/`) {
pages = append(pages, v)
continue
}
data, err := templates.Asset(v)
if err != nil { if err != nil {
return err panic(err)
}
template.Must(components.New(v).Parse(string(data)))
} }
if !f.IsDir() { for _, page := range pages {
pages = append(pages, path) data, err := templates.Asset(page)
}
return nil
})
if err != nil { if err != nil {
log.Fatal(err) panic(err)
} }
tmpl, _ := components.Clone()
components, err := filepath.Glob(componentsPath) name := removeExt(page)
if err != nil { t.templates[name] = template.Must(tmpl.New(page).Parse(string(data)))
log.Fatal(err)
}
for _, layout := range pages {
files := append(components, layout)
name := path.Base(removeExt(layout))
t.templates[name] = template.Must(template.New(``).ParseFiles(files...))
} }
} }
@ -58,9 +53,24 @@ func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Con
if !ok { if !ok {
return fmt.Errorf("template '%s' not found", name) return fmt.Errorf("template '%s' not found", name)
} }
return tmpl.ExecuteTemplate(w, "base", data) return tmpl.ExecuteTemplate(w, "base", data)
} }
func removeExt(s string) string { func removeExt(s string) string {
return strings.TrimSuffix(s, path.Ext(s)) return strings.TrimSuffix(s, path.Ext(s))
} }
func getStatic(c echo.Context) error {
path := path.Clean(strings.TrimPrefix(c.Request().URL.Path, `/static/`))
data, err := static.Asset(path)
if err != nil {
return c.String(404, `Resource not found`)
}
c.Response().Header().Set(`Content-Type`, mime.TypeByExtension(filepath.Ext(path)))
c.Response().Write(data)
return nil
}