|
|
- 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)
-
- authApi.HandleFunc("/check", Auth.Check).Methods("GET")
-
- // Define routes for friends and friend requests
- authApi.HandleFunc("/friend_requests", Friends.EncryptedFriendRequestList).Methods("GET")
- authApi.HandleFunc("/friend_request", Friends.CreateFriendRequest).Methods("POST")
-
- authApi.HandleFunc("/conversations", Messages.EncryptedConversationList).Methods("GET")
- authApi.HandleFunc("/conversation_details", Messages.EncryptedConversationDetailsList).Methods("GET")
-
- authApi.HandleFunc("/conversations", Messages.CreateConversation).Methods("POST")
-
- // Define routes for messages
- authApi.HandleFunc("/message", Messages.CreateMessage).Methods("POST")
- authApi.HandleFunc("/messages/{threadKey}", Messages.Messages).Methods("GET")
- }
|