Add bindata
This commit is contained in:
parent
f9ed3ac363
commit
201201edf3
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
bindata.go
|
9
Makefile
Normal file
9
Makefile
Normal 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
3
internal/static/main.go
Normal file
@ -0,0 +1,3 @@
|
||||
package static
|
||||
|
||||
//go:generate go-bindata -pkg $GOPACKAGE --prefix "../../static/" ../../static/...
|
3
internal/templates/main.go
Normal file
3
internal/templates/main.go
Normal file
@ -0,0 +1,3 @@
|
||||
package templates
|
||||
|
||||
//go:generate go-bindata -pkg $GOPACKAGE --prefix "../../templates/" ../../templates/...
|
@ -9,13 +9,12 @@ func main() {
|
||||
t.Reset()
|
||||
e.Renderer = &t
|
||||
|
||||
e.Static("/static", "static/")
|
||||
|
||||
e.GET("/", index)
|
||||
e.GET("/static/*", getStatic)
|
||||
|
||||
e.Logger.Fatal(e.Start(":8080"))
|
||||
}
|
||||
|
||||
func index(c echo.Context) error {
|
||||
return c.Render(200, "index", nil)
|
||||
return c.Render(200, "pages/index", nil)
|
||||
}
|
||||
|
@ -4,51 +4,46 @@ import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"mime"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"git.fuyu.moe/Crow/echoBoilerplate/internal/static"
|
||||
"git.fuyu.moe/Crow/echoBoilerplate/internal/templates"
|
||||
"github.com/labstack/echo"
|
||||
)
|
||||
|
||||
// Reset reads all the templates
|
||||
func (t *Template) Reset() {
|
||||
pagesPath := `templates/pages/`
|
||||
componentsPath := `templates/components/*.gohtml`
|
||||
|
||||
if t.templates == nil {
|
||||
t.templates = make(map[string]*template.Template)
|
||||
}
|
||||
|
||||
components := template.New(``)
|
||||
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 {
|
||||
return err
|
||||
panic(err)
|
||||
}
|
||||
template.Must(components.New(v).Parse(string(data)))
|
||||
}
|
||||
|
||||
if !f.IsDir() {
|
||||
pages = append(pages, path)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
for _, page := range pages {
|
||||
data, err := templates.Asset(page)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
panic(err)
|
||||
}
|
||||
tmpl, _ := components.Clone()
|
||||
|
||||
components, err := filepath.Glob(componentsPath)
|
||||
if err != nil {
|
||||
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...))
|
||||
name := removeExt(page)
|
||||
t.templates[name] = template.Must(tmpl.New(page).Parse(string(data)))
|
||||
}
|
||||
}
|
||||
|
||||
@ -58,9 +53,24 @@ func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Con
|
||||
if !ok {
|
||||
return fmt.Errorf("template '%s' not found", name)
|
||||
}
|
||||
|
||||
return tmpl.ExecuteTemplate(w, "base", data)
|
||||
}
|
||||
|
||||
func removeExt(s string) string {
|
||||
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user