CHANGE: added Frontend + auth
This commit is contained in:
@@ -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))
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user