Files
arbeitszeitmessung/Backend/main.go

61 lines
1.6 KiB
Go

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
server.HandleFunc("/time/new", endpoints.TimeCreateHandler)
server.Handle("/time", ParamsMiddleware(endpoints.TimeHandler))
server.HandleFunc("/logout", endpoints.LogoutHandler)
server.HandleFunc("/user/login", endpoints.LoginHandler)
server.HandleFunc("/user", endpoints.UserHandler)
server.HandleFunc("/team", endpoints.TeamHandler)
server.HandleFunc("/team/presence", endpoints.TeamHandler)
server.Handle("/", http.RedirectHandler("/time", http.StatusPermanentRedirect))
server.Handle("/static/", http.StripPrefix("/static/", fs))
serverSessionMiddleware := endpoints.Session.LoadAndSave(server)
// starting the http server
fmt.Printf("Server is running at http://localhost:%s\n", helper.GetEnv("EXPOSED_PORT", "8080"))
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))
})
}