#109 basic infrastructure for server side rendering with templ
Some checks failed
Build Docker Image / Explore-Gitea-Actions (push) Failing after 5s

This commit is contained in:
Tim
2024-08-22 22:53:28 +02:00
parent de8a5051e7
commit 4718622ce0
12 changed files with 82 additions and 31 deletions

1
.gitignore vendored
View File

@@ -20,6 +20,7 @@
# Go workspace file # Go workspace file
go.work go.work
go.work.sum go.work.sum
*_templ.go
# env file # env file
.env .env

3
go.mod
View File

@@ -1,4 +1,4 @@
module api module me-fit
go 1.22.5 go 1.22.5
@@ -19,6 +19,7 @@ require (
cloud.google.com/go/iam v1.1.12 // indirect cloud.google.com/go/iam v1.1.12 // indirect
cloud.google.com/go/longrunning v0.5.11 // indirect cloud.google.com/go/longrunning v0.5.11 // indirect
cloud.google.com/go/storage v1.43.0 // indirect cloud.google.com/go/storage v1.43.0 // indirect
github.com/a-h/templ v0.2.747 // indirect
github.com/beorn7/perks v1.0.1 // indirect github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect github.com/felixge/httpsnoop v1.0.4 // indirect

2
go.sum
View File

@@ -36,6 +36,8 @@ cloud.google.com/go/storage v1.43.0/go.mod h1:ajvxEa7WmZS1PxvKRq4bq0tFT3vMd502Jw
firebase.google.com/go v3.13.0+incompatible h1:3TdYC3DDi6aHn20qoRkxwGqNgdjtblwVAyRLQwGn/+4= firebase.google.com/go v3.13.0+incompatible h1:3TdYC3DDi6aHn20qoRkxwGqNgdjtblwVAyRLQwGn/+4=
firebase.google.com/go v3.13.0+incompatible/go.mod h1:xlah6XbEyW6tbfSklcfe5FHJIwjt8toICdV5Wh9ptHs= firebase.google.com/go v3.13.0+incompatible/go.mod h1:xlah6XbEyW6tbfSklcfe5FHJIwjt8toICdV5Wh9ptHs=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/a-h/templ v0.2.747 h1:D0dQ2lxC3W7Dxl6fxQ/1zZHBQslSkTSvl5FxP/CfdKg=
github.com/a-h/templ v0.2.747/go.mod h1:69ObQIbrcuwPCU32ohNaWce3Cb7qM5GMiqN1K+2yop4=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=

24
handler.go Normal file
View File

@@ -0,0 +1,24 @@
package main
import (
"me-fit/middleware"
"me-fit/service"
"database/sql"
"net/http"
)
func getHandler(db *sql.DB) http.Handler {
var router = http.NewServeMux()
router.HandleFunc("/", service.HandleStaticUi)
// Serve static files (CSS, JS and images)
router.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
router.HandleFunc("POST /api/workout", service.NewWorkout(db))
router.HandleFunc("GET /api/workout", service.GetWorkouts(db))
router.HandleFunc("DELETE /api/workout", service.DeleteWorkout(db))
return middleware.Logging(middleware.EnableCors(router))
}

View File

@@ -1,7 +1,8 @@
package main package main
import ( import (
"api/utils" "me-fit/utils"
"database/sql" "database/sql"
"log" "log"
"net/http" "net/http"

View File

@@ -1,22 +0,0 @@
package main
import (
"api/middleware"
"api/workout"
"database/sql"
"net/http"
)
func getHandler(db *sql.DB) http.Handler {
var router = http.NewServeMux()
static := http.FileServer(http.Dir("./static"))
router.Handle("/static", static)
router.HandleFunc("POST /workout", workout.NewWorkout(db))
router.HandleFunc("GET /workout", workout.GetWorkouts(db))
router.HandleFunc("DELETE /workout", workout.DeleteWorkout(db))
return middleware.Logging(middleware.EnableCors(router))
}

19
service/static_ui.go Normal file
View File

@@ -0,0 +1,19 @@
package service
import (
"me-fit/templates"
"net/http"
"github.com/a-h/templ"
)
func HandleStaticUi(w http.ResponseWriter, r *http.Request) {
var comp templ.Component = nil
if r.URL.Path != "/" {
comp = templates.Layout(templates.NotFound())
} else {
comp = templates.Layout(nil)
}
comp.Render(r.Context(), w)
}

View File

@@ -1,7 +1,7 @@
package workout package service
import ( import (
"api/utils" "me-fit/utils"
"database/sql" "database/sql"
"net/http" "net/http"

5
templates/footer.templ Normal file
View File

@@ -0,0 +1,5 @@
package templates
templ footer() {
<div>Footer</div>
}

6
templates/header.templ Normal file
View File

@@ -0,0 +1,6 @@
package templates
templ header() {
<div>Header</div>
}

View File

@@ -1,9 +1,7 @@
package templates package templates
templ header() {
}
templ index(name string) { templ Layout(comp templ.Component) {
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
@@ -11,8 +9,16 @@ templ index(name string) {
<title>ME-FIT</title> <title>ME-FIT</title>
<link rel="icon" href="static/favicon.svg"/> <link rel="icon" href="static/favicon.svg"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/> <meta name="viewport" content="width=device-width, initial-scale=1"/>
<script defer src="https://umami.me-fit.eu/script.js" data-website-id="211de91f-c87a-4e6f-9bac-8214633535e3"></script> <script defer src="https://umami.me-fit.eu/script.js" data-website-id="3c8efb09-44e4-4372-8a1e-c3bc675cd89a"></script>
</head> </head>
<body></body> <body>
@header()
if comp != nil {
@comp
}
@footer()
</body>
</html> </html>
} }

View File

@@ -0,0 +1,8 @@
package templates
templ NotFound() {
<h1>Not Found</h1>
}