CHANGE: added Frontend + auth

This commit is contained in:
2025-02-19 21:16:58 +01:00
parent f2e9eaf092
commit 35778e58b3
25 changed files with 2160 additions and 22 deletions

View File

@@ -4,13 +4,17 @@ import (
"arbeitszeitmessung/endpoints"
"arbeitszeitmessung/helper"
"arbeitszeitmessung/models"
"context"
"fmt"
"log"
"net/http"
"time"
_ "github.com/lib/pq"
)
func main() {
var err error
models.DB, err = OpenDatabase()
@@ -19,12 +23,30 @@ func main() {
}
defer models.DB.Close()
fs := http.FileServer(http.Dir("./static"))
endpoints.CreateSessionManager(24 * time.Hour)
server := http.NewServeMux()
// handles the different http routes
http.HandleFunc("/time/new", endpoints.TimeCreateHandler)
http.HandleFunc("/time", endpoints.TimeHandler)
http.HandleFunc("/logout", endpoints.LogoutHandler)
withMiddleware := ParamsMiddleware(endpoints.TimeHandler)
server.HandleFunc("/time/new", endpoints.TimeCreateHandler)
server.Handle("/time", withMiddleware)
server.HandleFunc("/logout", endpoints.LogoutHandler)
server.HandleFunc("/", endpoints.LoginHandler)
server.Handle("/static/", http.StripPrefix("/static/", fs))
serverSessionMiddleware := endpoints.Session.LoadAndSave(server)
// starting the http server
fmt.Printf("Server is running at http://localhost:8000 exposed to port %s\n", helper.GetEnv("EXPOSED_PORT", "8000"))
log.Fatal(http.ListenAndServe(":8080", nil))
log.Fatal(http.ListenAndServe(":8080", serverSessionMiddleware))
}
func ParamsMiddleware(next http.HandlerFunc) http.Handler{
return http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
queryParams := r.URL.Query()
ctx := context.WithValue(r.Context(), "urlParams", queryParams)
next.ServeHTTP(w, r.WithContext(ctx))
})
}