From 92d8e5fa098a0dbf325759d3b99fb1b34a9e6820 Mon Sep 17 00:00:00 2001 From: Gregory Ballantine Date: Fri, 24 Nov 2023 14:27:23 -0500 Subject: [PATCH] Added templating, versioning, middleware, etc --- Makefile | 2 +- app/vars.go | 5 +++++ go.mod | 1 + go.sum | 2 ++ main.go | 17 ++++++++++++++++- templates/index/dashboard.tmpl | 7 +++++++ templates/layout/footer.tmpl | 4 ++++ templates/layout/header.tmpl | 11 +++++++++++ web/middleware/vars.go | 11 +++++++++++ web/routes.go | 12 ++++++++++++ web/routes/index.go | 12 ++++++++++++ 11 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 app/vars.go create mode 100644 templates/index/dashboard.tmpl create mode 100644 templates/layout/footer.tmpl create mode 100644 templates/layout/header.tmpl create mode 100644 web/middleware/vars.go create mode 100644 web/routes.go create mode 100644 web/routes/index.go diff --git a/Makefile b/Makefile index 8c90ef2..5e6bb9e 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ VERSION=0.0.1 .PHONY: build ## build: Compile the packages. build: - @go build -o $(NAME) + @go build -o $(NAME) -ldflags "-X git.metaunix.net/bitgoblin/blt/app.Version=$(VERSION)" .PHONY: run ## run: Build and Run in development mode. diff --git a/app/vars.go b/app/vars.go new file mode 100644 index 0000000..10f363b --- /dev/null +++ b/app/vars.go @@ -0,0 +1,5 @@ +package app + +var ( + Version string = "N/a" +) diff --git a/go.mod b/go.mod index cd8cb01..9407bad 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/charmbracelet/lipgloss v0.7.1 // indirect github.com/charmbracelet/log v0.2.3 // indirect github.com/flamego/flamego v1.9.4 // indirect + github.com/flamego/template v1.2.2 // indirect github.com/go-logfmt/logfmt v0.6.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mattn/go-isatty v0.0.18 // indirect diff --git a/go.sum b/go.sum index 4b1cd64..cde8b81 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ github.com/charmbracelet/log v0.2.3 h1:YVmBhJtpGL7nW/nlf5u+SEloU8XYljxozGzZpgwIv github.com/charmbracelet/log v0.2.3/go.mod h1:ZApwwzDbbETVTIRTk7724yQRJAXIktt98yGVMMaa3y8= github.com/flamego/flamego v1.9.4 h1:SNsooIfNa6ljQM1rBmfg4cFcXPIhQdG/uvNHqXxPvD8= github.com/flamego/flamego v1.9.4/go.mod h1:2tAVbugA3fgX8xOBoqR2jmJSSvZDLBFGXTFCR5h5eAU= +github.com/flamego/template v1.2.2 h1:aMpt8RzXBb2ZGuABf9p/q8oBBpXrurUV8rgBbz7mj2o= +github.com/flamego/template v1.2.2/go.mod h1:xTAmwCCPaOuxN5t4CpzOP7WZN5WkLRiJfJCpsiB0aUg= github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= diff --git a/main.go b/main.go index d56ecfc..15a70af 100644 --- a/main.go +++ b/main.go @@ -5,13 +5,28 @@ import ( "net/http" "github.com/flamego/flamego" + "github.com/flamego/template" + + "git.metaunix.net/bitgoblin/blt/web" + "git.metaunix.net/bitgoblin/blt/web/middleware" ) +var Version string = "N/a" + func main() { + // initial Flamego server object f := flamego.Classic() - f.Get("/{*}", printRequestPath) + // initialize templating engine + f.Use(template.Templater()) + // inject custom middleware + f.Use(middleware.CustomVars) + + // register routes to server (moved to separate file for cleanliness) + web.RegisterRoutes(f) + + // alert the user and start the server log.Println("Server is running...") log.Println(http.ListenAndServe("0.0.0.0:2830", f)) } diff --git a/templates/index/dashboard.tmpl b/templates/index/dashboard.tmpl new file mode 100644 index 0000000..d7fb51c --- /dev/null +++ b/templates/index/dashboard.tmpl @@ -0,0 +1,7 @@ +{{ template "header" . }} + +

This is the BLT dashboard.

+ +

BLT version {{ .app_version }}

+ +{{ template "footer" . }} diff --git a/templates/layout/footer.tmpl b/templates/layout/footer.tmpl new file mode 100644 index 0000000..2edaabe --- /dev/null +++ b/templates/layout/footer.tmpl @@ -0,0 +1,4 @@ +{{ define "footer" }} + + +{{ end }} diff --git a/templates/layout/header.tmpl b/templates/layout/header.tmpl new file mode 100644 index 0000000..d324a97 --- /dev/null +++ b/templates/layout/header.tmpl @@ -0,0 +1,11 @@ +{{ define "header" }} + + + + + + + {{ .title }} | BLT + + +{{ end }} diff --git a/web/middleware/vars.go b/web/middleware/vars.go new file mode 100644 index 0000000..c69f4e3 --- /dev/null +++ b/web/middleware/vars.go @@ -0,0 +1,11 @@ +package middleware + +import ( + "github.com/flamego/template" + + "git.metaunix.net/bitgoblin/blt/app" +) + +func CustomVars(data template.Data) { + data["app_version"] = app.Version +} diff --git a/web/routes.go b/web/routes.go new file mode 100644 index 0000000..382047e --- /dev/null +++ b/web/routes.go @@ -0,0 +1,12 @@ +package web + +import ( + "github.com/flamego/flamego" + + "git.metaunix.net/bitgoblin/blt/web/routes" +) + +func RegisterRoutes(f *flamego.Flame) { + // index routes + f.Get("/", routes.GetDashboard) +} diff --git a/web/routes/index.go b/web/routes/index.go new file mode 100644 index 0000000..4926e8d --- /dev/null +++ b/web/routes/index.go @@ -0,0 +1,12 @@ +package routes + +import ( + "net/http" + + "github.com/flamego/template" +) + +func GetDashboard(t template.Template, data template.Data) { + data["title"] = "Dashboard" + t.HTML(http.StatusOK, "index/dashboard") +}