Added templating, versioning, middleware, etc

This commit is contained in:
Gregory Ballantine 2023-11-24 14:27:23 -05:00
parent cd7003223a
commit 92d8e5fa09
11 changed files with 82 additions and 2 deletions

View File

@ -4,7 +4,7 @@ VERSION=0.0.1
.PHONY: build .PHONY: build
## build: Compile the packages. ## build: Compile the packages.
build: build:
@go build -o $(NAME) @go build -o $(NAME) -ldflags "-X git.metaunix.net/bitgoblin/blt/app.Version=$(VERSION)"
.PHONY: run .PHONY: run
## run: Build and Run in development mode. ## run: Build and Run in development mode.

5
app/vars.go Normal file
View File

@ -0,0 +1,5 @@
package app
var (
Version string = "N/a"
)

1
go.mod
View File

@ -8,6 +8,7 @@ require (
github.com/charmbracelet/lipgloss v0.7.1 // indirect github.com/charmbracelet/lipgloss v0.7.1 // indirect
github.com/charmbracelet/log v0.2.3 // indirect github.com/charmbracelet/log v0.2.3 // indirect
github.com/flamego/flamego v1.9.4 // 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/go-logfmt/logfmt v0.6.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect github.com/mattn/go-isatty v0.0.18 // indirect

2
go.sum
View File

@ -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/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 h1:SNsooIfNa6ljQM1rBmfg4cFcXPIhQdG/uvNHqXxPvD8=
github.com/flamego/flamego v1.9.4/go.mod h1:2tAVbugA3fgX8xOBoqR2jmJSSvZDLBFGXTFCR5h5eAU= 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 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4=
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= 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= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=

17
main.go
View File

@ -5,13 +5,28 @@ import (
"net/http" "net/http"
"github.com/flamego/flamego" "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() { func main() {
// initial Flamego server object
f := flamego.Classic() 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("Server is running...")
log.Println(http.ListenAndServe("0.0.0.0:2830", f)) log.Println(http.ListenAndServe("0.0.0.0:2830", f))
} }

View File

@ -0,0 +1,7 @@
{{ template "header" . }}
<p>This is the BLT dashboard.</p>
<p>BLT version {{ .app_version }}</p>
{{ template "footer" . }}

View File

@ -0,0 +1,4 @@
{{ define "footer" }}
</body>
</html>
{{ end }}

View File

@ -0,0 +1,11 @@
{{ define "header" }}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>{{ .title }} | BLT</title>
</head>
<body>
{{ end }}

11
web/middleware/vars.go Normal file
View File

@ -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
}

12
web/routes.go Normal file
View File

@ -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)
}

12
web/routes/index.go Normal file
View File

@ -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")
}