package Api import ( "log" "net/http" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Auth" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Friends" "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Messages" "github.com/gorilla/mux" ) func loggingMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Printf( "%s %s, Content Length: %d", r.Method, r.RequestURI, r.ContentLength, ) next.ServeHTTP(w, r) }) } func authenticationMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { var ( err error ) _, err = Auth.CheckCookie(r) if err != nil { http.Error(w, "Forbidden", http.StatusUnauthorized) return } next.ServeHTTP(w, r) }) } func InitApiEndpoints(router *mux.Router) { var ( api *mux.Router authApi *mux.Router ) log.Println("Initializing API routes...") api = router.PathPrefix("/api/v1/").Subrouter() api.Use(loggingMiddleware) // Define routes for authentication api.HandleFunc("/signup", Auth.Signup).Methods("POST") api.HandleFunc("/login", Auth.Login).Methods("POST") api.HandleFunc("/logout", Auth.Logout).Methods("GET") authApi = api.PathPrefix("/auth/").Subrouter() authApi.Use(authenticationMiddleware) // Define routes for friends and friend requests authApi.HandleFunc("/friend", Friends.CreateFriendRequest).Methods("POST") authApi.HandleFunc("/friends", Friends.EncryptedFriendList).Methods("GET") authApi.HandleFunc("/friend/{userID}", Friends.Friend).Methods("GET") authApi.HandleFunc("/friend/{userID}/request", Friends.FriendRequest).Methods("POST") // Define routes for messages authApi.HandleFunc("/messages/{threadKey}", Messages.MessageThread).Methods("GET") }