Initial commit.

This commit is contained in:
mikestefanello 2021-12-03 06:11:01 -05:00
commit 63f43e568c
23 changed files with 1199 additions and 0 deletions

18
controllers/about.go Normal file
View file

@ -0,0 +1,18 @@
package controllers
import (
"github.com/labstack/echo/v4"
)
type About struct {
Controller
}
func (a *About) Get(c echo.Context) error {
p := NewPage(c)
p.Layout = "main"
p.Name = "about"
p.Data = "This is the about page"
return a.RenderPage(c, p)
}

25
controllers/contact.go Normal file
View file

@ -0,0 +1,25 @@
package controllers
import (
"goweb/msg"
"github.com/labstack/echo/v4"
)
type Contact struct {
Controller
}
func (a *Contact) Get(c echo.Context) error {
p := NewPage(c)
p.Layout = "main"
p.Name = "contact"
p.Data = "This is the contact page"
return a.RenderPage(c, p)
}
func (a *Contact) Post(c echo.Context) error {
msg.Set(c, msg.Success, "Thank you for contacting us!")
msg.Set(c, msg.Info, "We will respond to you shortly.")
return a.Redirect(c, "home")
}

100
controllers/controller.go Normal file
View file

@ -0,0 +1,100 @@
package controllers
import (
"bytes"
"fmt"
"html/template"
"net/http"
"sync"
"goweb/config"
"goweb/container"
"goweb/funcmap"
"github.com/labstack/echo/v4"
)
const (
TemplateDir = "views"
TemplateExt = ".gohtml"
)
var (
// Cache of compiled page templates
templates = sync.Map{}
// Template function map
funcMap = funcmap.GetFuncMap()
)
type Controller struct {
Container *container.Container
}
func NewController(c *container.Container) Controller {
return Controller{
Container: c,
}
}
func (t *Controller) RenderPage(c echo.Context, p Page) error {
if p.Name == "" {
c.Logger().Error("Page render failed due to missing name")
return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error")
}
if p.AppName == "" {
p.AppName = t.Container.Config.App.Name
}
if err := t.parsePageTemplates(p); err != nil {
return err
}
tmpl, ok := templates.Load(p.Name)
if !ok {
c.Logger().Error("Uncached page template requested")
return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error")
}
buf := new(bytes.Buffer)
err := tmpl.(*template.Template).ExecuteTemplate(buf, p.Layout+TemplateExt, p)
if err != nil {
return err
}
return c.HTMLBlob(p.StatusCode, buf.Bytes())
}
func (t *Controller) parsePageTemplates(p Page) error {
// Check if the template has not yet been parsed or if the app environment is local, so that templates reflect
// changes without having the restart the server
if _, ok := templates.Load(p.Name); !ok || t.Container.Config.App.Environment == config.EnvLocal {
parsed, err :=
template.New(p.Layout+TemplateExt).
Funcs(funcMap).
ParseFiles(
fmt.Sprintf("%s/layouts/%s%s", TemplateDir, p.Layout, TemplateExt),
fmt.Sprintf("%s/pages/%s%s", TemplateDir, p.Name, TemplateExt),
)
if err != nil {
return err
}
parsed, err = parsed.ParseGlob(fmt.Sprintf("%s/components/*%s", TemplateDir, TemplateExt))
if err != nil {
return err
}
// Store the template so this process only happens once
templates.Store(p.Name, parsed)
}
return nil
}
func (t *Controller) Redirect(c echo.Context, route string, routeParams ...interface{}) error {
return c.Redirect(http.StatusFound, c.Echo().Reverse(route, routeParams))
}

36
controllers/error.go Normal file
View file

@ -0,0 +1,36 @@
package controllers
import (
"fmt"
"net/http"
"github.com/labstack/echo/v4"
)
type Error struct {
Controller
}
func (e *Error) Get(err error, c echo.Context) {
code := http.StatusInternalServerError
if he, ok := err.(*echo.HTTPError); ok {
code = he.Code
}
if code >= 500 {
c.Logger().Error(err)
} else {
c.Logger().Info(err)
}
p := NewPage(c)
p.Layout = "main"
p.Title = http.StatusText(code)
// TODO: fallback if there is no error template
p.Name = fmt.Sprintf("errors/%d", code)
p.StatusCode = code
if err = e.RenderPage(c, p); err != nil {
c.Logger().Error(err)
}
}

18
controllers/home.go Normal file
View file

@ -0,0 +1,18 @@
package controllers
import (
"github.com/labstack/echo/v4"
)
type Home struct {
Controller
}
func (h *Home) Get(c echo.Context) error {
p := NewPage(c)
p.Layout = "main"
p.Name = "home"
p.Data = "Hello world"
return h.RenderPage(c, p)
}

54
controllers/page.go Normal file
View file

@ -0,0 +1,54 @@
package controllers
import (
"html/template"
"net/http"
"goweb/msg"
"github.com/labstack/echo/v4"
)
type Page struct {
AppName string
Title string
Context echo.Context
Reverse func(name string, params ...interface{}) string
Path string
Data interface{}
Layout string
Name string
IsHome bool
IsAuth bool
StatusCode int
Metatags struct {
Description string
Keywords []string
}
}
func NewPage(c echo.Context) Page {
p := Page{
Context: c,
Reverse: c.Echo().Reverse,
Path: c.Request().URL.Path,
StatusCode: http.StatusOK,
}
p.IsHome = p.Path == "/"
return p
}
func (p Page) SetMessage(typ msg.Type, value string) {
msg.Set(p.Context, typ, value)
}
func (p Page) GetMessages(typ msg.Type) []template.HTML {
strs := msg.Get(p.Context, typ)
ret := make([]template.HTML, len(strs))
for k, v := range strs {
ret[k] = template.HTML(v)
}
return ret
}