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.

94 lines
2.5 KiB

  1. package Api
  2. import (
  3. "log"
  4. "net/http"
  5. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Api/Auth"
  6. "github.com/gorilla/mux"
  7. )
  8. func loggingMiddleware(next http.Handler) http.Handler {
  9. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  10. log.Printf(
  11. "%s %s %s, Content Length: %d",
  12. r.RemoteAddr,
  13. r.Method,
  14. r.RequestURI,
  15. r.ContentLength,
  16. )
  17. next.ServeHTTP(w, r)
  18. })
  19. }
  20. func authenticationMiddleware(next http.Handler) http.Handler {
  21. return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  22. var (
  23. userSession Auth.Session
  24. err error
  25. )
  26. userSession, err = Auth.CheckCookie(r)
  27. if err != nil {
  28. http.Error(w, "Forbidden", http.StatusUnauthorized)
  29. return
  30. }
  31. log.Printf(
  32. "Authenticated user: %s (%s)",
  33. userSession.Email,
  34. userSession.UserID,
  35. )
  36. next.ServeHTTP(w, r)
  37. })
  38. }
  39. func InitApiEndpoints(router *mux.Router) {
  40. var (
  41. api *mux.Router
  42. adminApi *mux.Router
  43. )
  44. log.Println("Initializing API routes...")
  45. api = router.PathPrefix("/api/v1/").Subrouter()
  46. api.Use(loggingMiddleware)
  47. api.HandleFunc("/posts/front-page", getFrontPagePosts).Methods("GET")
  48. api.HandleFunc("/post", getPostsPublic).Methods("GET")
  49. api.HandleFunc("/post/{postID}", getPostPublic).Methods("GET")
  50. // Define routes for authentication
  51. api.HandleFunc("/admin/login", Auth.Login).Methods("POST")
  52. api.HandleFunc("/admin/logout", Auth.Logout).Methods("GET")
  53. api.HandleFunc("/admin/me", Auth.Me).Methods("GET")
  54. adminApi = api.PathPrefix("/admin/").Subrouter()
  55. adminApi.Use(authenticationMiddleware)
  56. // Define routes for posts api
  57. adminApi.HandleFunc("/post", getPosts).Methods("GET")
  58. adminApi.HandleFunc("/post/{postID}", getPost).Methods("GET")
  59. adminApi.HandleFunc("/post", createPost).Methods("POST")
  60. adminApi.HandleFunc("/post/{postID}", updatePost).Methods("PUT")
  61. adminApi.HandleFunc("/post/{postID}", deletePost).Methods("DELETE")
  62. adminApi.HandleFunc("/post/{postID}/publish", publishPost).Methods("GET")
  63. adminApi.HandleFunc("/post/{postID}/image", createPostImage).Methods("POST")
  64. adminApi.HandleFunc("/post/{postID}/image/{imageID}", deletePostImage).Methods("DELETE")
  65. // Define routes for users api
  66. adminApi.HandleFunc("/user", getUsers).Methods("GET")
  67. adminApi.HandleFunc("/user", createUser).Methods("POST")
  68. adminApi.HandleFunc("/user/{userID}", getUser).Methods("GET")
  69. adminApi.HandleFunc("/user/{userID}", updateUser).Methods("PUT")
  70. adminApi.HandleFunc("/user/{userID}", deletePost).Methods("DELETE")
  71. adminApi.HandleFunc("/user/{userID}/update-password", Auth.UpdatePassword).Methods("PUT")
  72. }