Skip to content

Commit

Permalink
Merge pull request #2 from fogo-sh/seperate-template-logic
Browse files Browse the repository at this point in the history
break out template and static logic into own pkgs
  • Loading branch information
jackharrhy authored Aug 27, 2023
2 parents f3de05c + cd8304f commit 7d9bb35
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 42 deletions.
52 changes: 10 additions & 42 deletions pkg/devserver/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package devserver

import (
"embed"
"errors"
"fmt"
"html/template"
Expand All @@ -16,6 +15,8 @@ import (
slogecho "github.com/samber/slog-echo"

"github.com/fogo-sh/almanac/pkg/content"
"github.com/fogo-sh/almanac/pkg/static"
"github.com/fogo-sh/almanac/pkg/templates"
)

type Config struct {
Expand All @@ -37,50 +38,18 @@ func (s *Server) Start() error {
return nil
}

type PageTemplate struct {
Title string
Content string
}

var pageTemplateContent = `<!DOCTYPE html>
<html>
<head>
<title>{{ .Title }}</title>
<link rel="stylesheet" href="/assets/css/main.css">
</head>
<body>
<h1>{{ .Title }}</h1>
{{ .Content }}
</body>
</html>`

var pageTemplate *template.Template

type PageData struct {
Title string
Content template.HTML
}

func init() {
t, err := template.New("page").Parse(pageTemplateContent)
if err != nil {
panic(err)
}
pageTemplate = t
}

type Renderer struct{}

func (r *Renderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
if name != "page" {
return fmt.Errorf("unknown template: %s", name)
}

return pageTemplate.Execute(w, data)
return templates.PageTemplate.Execute(w, data)
}

func serveNotFound(c echo.Context) error {
return c.Render(http.StatusNotFound, "page", PageData{
return c.Render(http.StatusNotFound, "page", templates.PageTemplateData{
Title: "Not found!",
Content: template.HTML("<p>Looks like this page doesn't exist yet</p>"),
})
Expand All @@ -103,27 +72,26 @@ func (s *Server) servePage(c echo.Context) error {
return fmt.Errorf("error processing file: %w", err)
}

return c.Render(http.StatusOK, "page", PageData{
return c.Render(http.StatusOK, "page", templates.PageTemplateData{
Title: page,
Content: template.HTML(string(file.ParsedContent)),
})
}

//go:embed static
var staticFS embed.FS

func NewServer(config Config) *Server {
echoInst := echo.New()

var configuredFrontendFS http.FileSystem
if config.UseBundledAssets {
configuredFrontendFS = http.FS(staticFS)
configuredFrontendFS = http.FS(static.StaticFS)
} else {
configuredFrontendFS = http.Dir("./pkg/devserver/static/")
configuredFrontendFS = http.Dir("./pkg/static/")
}

println(config.UseBundledAssets)

echoInst.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Root: ".",
Root: "./static/",
Index: "index.html",
Browse: false,
HTML5: true,
Expand Down
6 changes: 6 additions & 0 deletions pkg/static/assets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package static

import "embed"

//go:embed static
var StaticFS embed.FS
File renamed without changes.
35 changes: 35 additions & 0 deletions pkg/templates/page.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package templates

import "html/template"

type PageTemplateData struct {
Title string
Content template.HTML
}

var pageTemplateContent = `<!DOCTYPE html>
<html>
<head>
<title>{{ .Title }}</title>
<link rel="stylesheet" href="/assets/css/main.css">
</head>
<body>
<h1>{{ .Title }}</h1>
{{ .Content }}
</body>
</html>`

var PageTemplate *template.Template

type PageData struct {
Title string
Content template.HTML
}

func init() {
t, err := template.New("page").Parse(pageTemplateContent)
if err != nil {
panic(err)
}
PageTemplate = t
}

0 comments on commit 7d9bb35

Please sign in to comment.