Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.1 KiB

  1. package Api
  2. import (
  3. "log"
  4. "net/http"
  5. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Auth"
  6. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Friends"
  7. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Messages"
  8. "github.com/gorilla/mux"
  9. )
  10. func loggingMiddleware(next http.Handler) http.Handler {
  11. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  12. log.Printf(
  13. "%s %s, Content Length: %d",
  14. r.Method,
  15. r.RequestURI,
  16. r.ContentLength,
  17. )
  18. next.ServeHTTP(w, r)
  19. })
  20. }
  21. func authenticationMiddleware(next http.Handler) http.Handler {
  22. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  23. var err error
  24. _, err = Auth.CheckCookie(r)
  25. if err != nil {
  26. http.Error(w, "Forbidden", http.StatusUnauthorized)
  27. return
  28. }
  29. next.ServeHTTP(w, r)
  30. })
  31. }
  32. func InitApiEndpoints(router *mux.Router) {
  33. var (
  34. api *mux.Router
  35. authApi *mux.Router
  36. )
  37. log.Println("Initializing API routes...")
  38. api = router.PathPrefix("/api/v1/").Subrouter()
  39. api.Use(loggingMiddleware)
  40. // Define routes for authentication
  41. api.HandleFunc("/signup", Auth.Signup).Methods("POST")
  42. api.HandleFunc("/login", Auth.Login).Methods("POST")
  43. api.HandleFunc("/logout", Auth.Logout).Methods("GET")
  44. authApi = api.PathPrefix("/auth/").Subrouter()
  45. authApi.Use(authenticationMiddleware)
  46. authApi.HandleFunc("/check", Auth.Check).Methods("GET")
  47. // Define routes for friends and friend requests
  48. authApi.HandleFunc("/friend_requests", Friends.EncryptedFriendRequestList).Methods("GET")
  49. authApi.HandleFunc("/friend_request", Friends.CreateFriendRequest).Methods("POST")
  50. authApi.HandleFunc("/conversations", Messages.EncryptedConversationList).Methods("GET")
  51. authApi.HandleFunc("/conversation_details", Messages.EncryptedConversationDetailsList).Methods("GET")
  52. authApi.HandleFunc("/conversations", Messages.CreateConversation).Methods("POST")
  53. authApi.HandleFunc("/conversations", Messages.UpdateConversation).Methods("PUT")
  54. // Define routes for messages
  55. authApi.HandleFunc("/message", Messages.CreateMessage).Methods("POST")
  56. authApi.HandleFunc("/messages/{threadKey}", Messages.Messages).Methods("GET")
  57. }