diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cd56d44 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bindata.go diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..cf9a775 --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +NAME=server + +.PHONY: generate +generate: + @go generate ./... + +.PHONY: build +build: generate + @go build -o $(NAME) server/*.go diff --git a/internal/static/main.go b/internal/static/main.go new file mode 100644 index 0000000..8db3663 --- /dev/null +++ b/internal/static/main.go @@ -0,0 +1,3 @@ +package static + +//go:generate go-bindata -pkg $GOPACKAGE --prefix "../../static/" ../../static/... diff --git a/internal/templates/main.go b/internal/templates/main.go new file mode 100644 index 0000000..64a8e61 --- /dev/null +++ b/internal/templates/main.go @@ -0,0 +1,3 @@ +package templates + +//go:generate go-bindata -pkg $GOPACKAGE --prefix "../../templates/" ../../templates/... diff --git a/server/main.go b/server/main.go index 385edb2..986a12d 100644 --- a/server/main.go +++ b/server/main.go @@ -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) } diff --git a/server/templates.go b/server/templates.go index 72c3426..9611126 100644 --- a/server/templates.go +++ b/server/templates.go @@ -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) } - - if !f.IsDir() { - pages = append(pages, path) - } - - return nil - }) - - if err != nil { - log.Fatal(err) + template.Must(components.New(v).Parse(string(data))) } - components, err := filepath.Glob(componentsPath) - if err != nil { - log.Fatal(err) - } + for _, page := range pages { + data, err := templates.Asset(page) + if err != nil { + panic(err) + } + tmpl, _ := components.Clone() - 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 +}