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.

82 lines
2.6 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. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Api/Users"
  9. "github.com/gorilla/mux"
  10. )
  11. func loggingMiddleware(next http.Handler) http.Handler {
  12. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  13. log.Printf(
  14. "%s %s, Content Length: %d",
  15. r.Method,
  16. r.RequestURI,
  17. r.ContentLength,
  18. )
  19. next.ServeHTTP(w, r)
  20. })
  21. }
  22. func authenticationMiddleware(next http.Handler) http.Handler {
  23. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  24. var err error
  25. _, err = Auth.CheckCookie(r)
  26. if err != nil {
  27. http.Error(w, "Forbidden", http.StatusUnauthorized)
  28. return
  29. }
  30. next.ServeHTTP(w, r)
  31. })
  32. }
  33. // InitAPIEndpoints initializes all API endpoints required by mobile app
  34. func InitAPIEndpoints(router *mux.Router) {
  35. var (
  36. api *mux.Router
  37. authAPI *mux.Router
  38. )
  39. log.Println("Initializing API routes...")
  40. api = router.PathPrefix("/api/v1/").Subrouter()
  41. api.Use(loggingMiddleware)
  42. // Define routes for authentication
  43. api.HandleFunc("/signup", Auth.Signup).Methods("POST")
  44. api.HandleFunc("/login", Auth.Login).Methods("POST")
  45. api.HandleFunc("/logout", Auth.Logout).Methods("GET")
  46. authAPI = api.PathPrefix("/auth/").Subrouter()
  47. authAPI.Use(authenticationMiddleware)
  48. authAPI.HandleFunc("/check", Auth.Check).Methods("GET")
  49. authAPI.HandleFunc("/change_password", Auth.ChangePassword).Methods("POST")
  50. authAPI.HandleFunc("/message_expiry", Auth.ChangeMessageExpiry).Methods("POST")
  51. authAPI.HandleFunc("/users", Users.SearchUsers).Methods("GET")
  52. authAPI.HandleFunc("/friend_requests", Friends.EncryptedFriendRequestList).Methods("GET")
  53. authAPI.HandleFunc("/friend_request", Friends.CreateFriendRequest).Methods("POST")
  54. authAPI.HandleFunc("/friend_request/qr_code", Friends.CreateFriendRequestQrCode).Methods("POST")
  55. authAPI.HandleFunc("/friend_request/{requestID}", Friends.AcceptFriendRequest).Methods("POST")
  56. authAPI.HandleFunc("/friend_request/{requestID}", Friends.RejectFriendRequest).Methods("DELETE")
  57. authAPI.HandleFunc("/conversations", Messages.EncryptedConversationList).Methods("GET")
  58. authAPI.HandleFunc("/conversation_details", Messages.EncryptedConversationDetailsList).Methods("GET")
  59. authAPI.HandleFunc("/conversations", Messages.CreateConversation).Methods("POST")
  60. authAPI.HandleFunc("/conversations", Messages.UpdateConversation).Methods("PUT")
  61. authAPI.HandleFunc("/message", Messages.CreateMessage).Methods("POST")
  62. authAPI.HandleFunc("/messages/{associationKey}", Messages.Messages).Methods("GET")
  63. }