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.

70 lines
1.7 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 (
  24. err error
  25. )
  26. _, err = Auth.CheckCookie(r)
  27. if err != nil {
  28. http.Error(w, "Forbidden", http.StatusUnauthorized)
  29. return
  30. }
  31. next.ServeHTTP(w, r)
  32. })
  33. }
  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. // Define routes for friends and friend requests
  49. authApi.HandleFunc("/friends", Friends.EncryptedFriendList).Methods("GET")
  50. authApi.HandleFunc("/friend/{userID}", Friends.Friend).Methods("GET")
  51. authApi.HandleFunc("/friend/{userID}/request", Friends.FriendRequest).Methods("POST")
  52. // Define routes for messages
  53. authApi.HandleFunc("/messages/{threadID}", Messages.MessageThread).Methods("GET")
  54. }