diff --git a/server/main.go b/server/main.go new file mode 100644 index 0000000..385edb2 --- /dev/null +++ b/server/main.go @@ -0,0 +1,21 @@ +package main + +import "github.com/labstack/echo" + +func main() { + e := echo.New() + + t := Template{} + t.Reset() + e.Renderer = &t + + e.Static("/static", "static/") + + e.GET("/", index) + + e.Logger.Fatal(e.Start(":8080")) +} + +func index(c echo.Context) error { + return c.Render(200, "index", nil) +} diff --git a/server/templates.go b/server/templates.go new file mode 100644 index 0000000..72c3426 --- /dev/null +++ b/server/templates.go @@ -0,0 +1,66 @@ +package main + +import ( + "fmt" + "html/template" + "io" + "log" + "os" + "path" + "path/filepath" + "strings" + + "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) + } + + pages := []string{} + err := filepath.Walk(pagesPath, func(path string, f os.FileInfo, err error) error { + if err != nil { + return err + } + + if !f.IsDir() { + pages = append(pages, path) + } + + return nil + }) + + if err != nil { + log.Fatal(err) + } + + 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...)) + } +} + +// Render echo Render func +func (t *Template) Render(w io.Writer, name string, data interface{}, c echo.Context) error { + tmpl, ok := t.templates[name] + 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)) +} diff --git a/server/types.go b/server/types.go new file mode 100644 index 0000000..90040a0 --- /dev/null +++ b/server/types.go @@ -0,0 +1,10 @@ +package main + +import ( + "html/template" +) + +// Template echo template struct +type Template struct { + templates map[string]*template.Template +} diff --git a/static/css/main.css b/static/css/main.css new file mode 100644 index 0000000..ef96bbf --- /dev/null +++ b/static/css/main.css @@ -0,0 +1,27 @@ +html, +body { + height: 100%; +} + +body { + display: grid; + + background: #212121; + + grid-template-rows: 1fr auto 1fr; +} + +body > h1 { + font-family: sans-serif; + font-size: 4em; + + width: 100%; + padding: 40px; + + text-align: center; + + color: #4AE1FF; + background: rgba(255,255,255,.01); + + grid-row-start: 2; +} diff --git a/templates/components/base.gohtml b/templates/components/base.gohtml new file mode 100644 index 0000000..f4585c6 --- /dev/null +++ b/templates/components/base.gohtml @@ -0,0 +1,18 @@ +{{ define "base" }} + + + + + + + Monitor - {{ block "title" . }}Home{{ end }} + + + {{ block "stylesheets" . }}{{ end }} + + + {{ block "body" . }}{{ end }} + {{ block "scripts" . }}{{ end}} + + +{{ end }} diff --git a/templates/pages/index.gohtml b/templates/pages/index.gohtml new file mode 100644 index 0000000..0b3078f --- /dev/null +++ b/templates/pages/index.gohtml @@ -0,0 +1,3 @@ +{{ define "body" }} +

I'm working!

+{{ end }}