diff --git a/.air.toml b/.air.toml index d241399..f7973c6 100644 --- a/.air.toml +++ b/.air.toml @@ -3,7 +3,7 @@ tmp_dir = "tmp" [build] args_bin = [] - bin = "/bin/bash -c 'FRONTEND_URL=xxx ./tmp/main'" + bin = "./tmp/main" cmd = "templ generate && go build -o ./tmp/main ." delay = 1000 exclude_dir = ["static", "migrations", "node_modules", "tmp"] diff --git a/go.mod b/go.mod index fa55d4f..0e8a6af 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,9 @@ go 1.22.5 require ( firebase.google.com/go v3.13.0+incompatible + github.com/a-h/templ v0.2.747 github.com/golang-migrate/migrate/v4 v4.17.1 + github.com/joho/godotenv v1.5.1 github.com/mattn/go-sqlite3 v1.14.22 github.com/prometheus/client_golang v1.20.1 google.golang.org/api v0.193.0 @@ -19,7 +21,6 @@ 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 b2f0123..2aaf2b0 100644 --- a/go.sum +++ b/go.sum @@ -105,6 +105,8 @@ github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU= diff --git a/handler.go b/handler.go index 2fae639..9b8de5d 100644 --- a/handler.go +++ b/handler.go @@ -11,11 +11,12 @@ import ( func getHandler(db *sql.DB) http.Handler { var router = http.NewServeMux() - router.HandleFunc("/", service.HandleStaticUi) + router.HandleFunc("/", service.HandleIndexAnd404) // Serve static files (CSS, JS and images) router.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/")))) + router.HandleFunc("/app", service.App) router.HandleFunc("POST /api/workout", service.NewWorkout(db)) router.HandleFunc("GET /api/workout", service.GetWorkouts(db)) router.HandleFunc("DELETE /api/workout", service.DeleteWorkout(db)) diff --git a/main.go b/main.go index 7a09917..eb9fd34 100644 --- a/main.go +++ b/main.go @@ -7,6 +7,7 @@ import ( "log" "net/http" + "github.com/joho/godotenv" _ "github.com/mattn/go-sqlite3" "github.com/prometheus/client_golang/prometheus/promhttp" ) @@ -14,6 +15,11 @@ import ( func main() { log.Println("Starting server...") + err := godotenv.Load() + if err != nil { + log.Fatal("Error loading .env file") + } + db, err := sql.Open("sqlite3", "./data.db") if err != nil { log.Fatal("Could not open Database data.db: ", err) diff --git a/middleware/cors.go b/middleware/cors.go index ae74ece..104e73f 100644 --- a/middleware/cors.go +++ b/middleware/cors.go @@ -7,14 +7,14 @@ import ( ) func EnableCors(next http.Handler) http.Handler { - var frontent_url = os.Getenv("FRONTEND_URL") - if frontent_url == "" { - log.Fatal("FRONTEND_URL is not set") + var base_url = os.Getenv("BASE_URL") + if base_url == "" { + log.Fatal("BASE_URL is not set") } - log.Println("FRONTEND_URL is", frontent_url) + log.Println("BASE_URL is", base_url) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Access-Control-Allow-Origin", frontent_url) + w.Header().Set("Access-Control-Allow-Origin", base_url) w.Header().Set("Access-Control-Allow-Methods", "GET, POST, DELETE") w.Header().Set("Access-Control-Allow-Headers", "Authorization") diff --git a/service/static_ui.go b/service/static_ui.go index bc5e565..391f6e1 100644 --- a/service/static_ui.go +++ b/service/static_ui.go @@ -7,7 +7,7 @@ import ( "github.com/a-h/templ" ) -func HandleStaticUi(w http.ResponseWriter, r *http.Request) { +func HandleIndexAnd404(w http.ResponseWriter, r *http.Request) { var comp templ.Component = nil if r.URL.Path != "/" { comp = templates.Layout(templates.NotFound()) diff --git a/service/workout.go b/service/workout.go index 8ec57d2..63007ca 100644 --- a/service/workout.go +++ b/service/workout.go @@ -1,6 +1,7 @@ package service import ( + "me-fit/templates" "me-fit/utils" "database/sql" @@ -22,6 +23,13 @@ var ( ) ) +func App(w http.ResponseWriter, r *http.Request) { + comp := templates.App() + layout := templates.Layout(comp) + layout.Render(r.Context(), w) + w.WriteHeader(http.StatusOK) +} + func NewWorkout(db *sql.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { metrics.WithLabelValues("new").Inc() diff --git a/templates/app.templ b/templates/app.templ new file mode 100644 index 0000000..2eed8e7 --- /dev/null +++ b/templates/app.templ @@ -0,0 +1,65 @@ +package templates + +templ App() { + + + Track your workout + + + Push Ups + Pull Ups + + + + Save + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +} diff --git a/templates/header.templ b/templates/header.templ index 3a17fec..66393fd 100644 --- a/templates/header.templ +++ b/templates/header.templ @@ -1,4 +1,3 @@ - package templates templ header() { diff --git a/view/src/routes/+page.svelte b/view/src/routes/+page.svelte deleted file mode 100644 index e69de29..0000000 diff --git a/view/src/routes/app/+page.svelte b/view/src/routes/app/+page.svelte deleted file mode 100644 index 567b862..0000000 --- a/view/src/routes/app/+page.svelte +++ /dev/null @@ -1,160 +0,0 @@ - - - - - Track your workout - - - - Push Ups - Pull Ups - - - - - - Save - - - - Workout history - - - - Date - Type - Sets - Reps - - - - - - {#each workouts as workout} - - {workout.date} - {workout.type} - {workout.sets} - {workout.reps} - - - deleteWorkout(workout.id)}> - - - - - - {/each} - - - -