package main import ( "arbeitszeitmessung/endpoints" "arbeitszeitmessung/helper" "arbeitszeitmessung/models" "context" "fmt" "log" "net/http" "time" "github.com/joho/godotenv" _ "github.com/lib/pq" ) func main() { var err error err = godotenv.Load(".env") if err != nil { log.Println("No .env file found in directory!") } models.DB, err = OpenDatabase() if err != nil { log.Fatal(err) } defer models.DB.Close() fs := http.FileServer(http.Dir("./static")) endpoints.CreateSessionManager(24 * time.Hour) server := http.NewServeMux() // handles the different http routes 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", 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)) }) }