diff --git a/.gitignore b/.gitignore index 11d7c47..b5a3f80 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ # Go workspace file go.work go.work.sum +*_templ.go # env file .env diff --git a/go.mod b/go.mod index 7451c2b..b19f162 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module api +module me-fit go 1.22.5 @@ -19,6 +19,7 @@ require ( cloud.google.com/go/iam v1.1.12 // indirect cloud.google.com/go/longrunning v0.5.11 // 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/cespare/xxhash/v2 v2.3.0 // indirect github.com/felixge/httpsnoop v1.0.4 // indirect diff --git a/go.sum b/go.sum index b34e756..e1eb88c 100644 --- a/go.sum +++ b/go.sum @@ -38,6 +38,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/go.mod h1:xlah6XbEyW6tbfSklcfe5FHJIwjt8toICdV5Wh9ptHs= 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/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= diff --git a/handler.go b/handler.go new file mode 100644 index 0000000..2fae639 --- /dev/null +++ b/handler.go @@ -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)) +} diff --git a/main.go b/main.go index c1d0e30..7a09917 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,8 @@ package main import ( - "api/utils" + "me-fit/utils" + "database/sql" "log" "net/http" diff --git a/router.go b/router.go deleted file mode 100644 index 299f34b..0000000 --- a/router.go +++ /dev/null @@ -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)) -} diff --git a/service/static_ui.go b/service/static_ui.go new file mode 100644 index 0000000..fff37f7 --- /dev/null +++ b/service/static_ui.go @@ -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) +} diff --git a/workout/workout.go b/service/workout.go similarity index 98% rename from workout/workout.go rename to service/workout.go index 90aac9d..8ec57d2 100644 --- a/workout/workout.go +++ b/service/workout.go @@ -1,7 +1,7 @@ -package workout +package service import ( - "api/utils" + "me-fit/utils" "database/sql" "net/http" diff --git a/templates/footer.templ b/templates/footer.templ new file mode 100644 index 0000000..0fe36a5 --- /dev/null +++ b/templates/footer.templ @@ -0,0 +1,5 @@ +package templates + +templ footer() { +