api.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package api // import "miniflux.app/v2/internal/api"
  4. import (
  5. "net/http"
  6. "runtime"
  7. "miniflux.app/v2/internal/http/response/json"
  8. "miniflux.app/v2/internal/storage"
  9. "miniflux.app/v2/internal/version"
  10. "miniflux.app/v2/internal/worker"
  11. "github.com/gorilla/mux"
  12. )
  13. type handler struct {
  14. store *storage.Storage
  15. pool *worker.Pool
  16. router *mux.Router
  17. }
  18. // Serve declares API routes for the application.
  19. func Serve(router *mux.Router, store *storage.Storage, pool *worker.Pool) {
  20. handler := &handler{store, pool, router}
  21. sr := router.PathPrefix("/v1").Subrouter()
  22. middleware := newMiddleware(store)
  23. sr.Use(middleware.handleCORS)
  24. sr.Use(middleware.apiKeyAuth)
  25. sr.Use(middleware.basicAuth)
  26. sr.Methods(http.MethodOptions)
  27. sr.HandleFunc("/users", handler.createUser).Methods(http.MethodPost)
  28. sr.HandleFunc("/users", handler.users).Methods(http.MethodGet)
  29. sr.HandleFunc("/users/{userID:[0-9]+}", handler.userByID).Methods(http.MethodGet)
  30. sr.HandleFunc("/users/{userID:[0-9]+}", handler.updateUser).Methods(http.MethodPut)
  31. sr.HandleFunc("/users/{userID:[0-9]+}", handler.removeUser).Methods(http.MethodDelete)
  32. sr.HandleFunc("/users/{userID:[0-9]+}/mark-all-as-read", handler.markUserAsRead).Methods(http.MethodPut)
  33. sr.HandleFunc("/users/{username}", handler.userByUsername).Methods(http.MethodGet)
  34. sr.HandleFunc("/me", handler.currentUser).Methods(http.MethodGet)
  35. sr.HandleFunc("/categories", handler.createCategory).Methods(http.MethodPost)
  36. sr.HandleFunc("/categories", handler.getCategories).Methods(http.MethodGet)
  37. sr.HandleFunc("/categories/{categoryID}", handler.updateCategory).Methods(http.MethodPut)
  38. sr.HandleFunc("/categories/{categoryID}", handler.removeCategory).Methods(http.MethodDelete)
  39. sr.HandleFunc("/categories/{categoryID}/mark-all-as-read", handler.markCategoryAsRead).Methods(http.MethodPut)
  40. sr.HandleFunc("/categories/{categoryID}/feeds", handler.getCategoryFeeds).Methods(http.MethodGet)
  41. sr.HandleFunc("/categories/{categoryID}/refresh", handler.refreshCategory).Methods(http.MethodPut)
  42. sr.HandleFunc("/categories/{categoryID}/entries", handler.getCategoryEntries).Methods(http.MethodGet)
  43. sr.HandleFunc("/categories/{categoryID}/entries/{entryID}", handler.getCategoryEntry).Methods(http.MethodGet)
  44. sr.HandleFunc("/discover", handler.discoverSubscriptions).Methods(http.MethodPost)
  45. sr.HandleFunc("/feeds", handler.createFeed).Methods(http.MethodPost)
  46. sr.HandleFunc("/feeds", handler.getFeeds).Methods(http.MethodGet)
  47. sr.HandleFunc("/feeds/counters", handler.fetchCounters).Methods(http.MethodGet)
  48. sr.HandleFunc("/feeds/refresh", handler.refreshAllFeeds).Methods(http.MethodPut)
  49. sr.HandleFunc("/feeds/{feedID}/refresh", handler.refreshFeed).Methods(http.MethodPut)
  50. sr.HandleFunc("/feeds/{feedID}", handler.getFeed).Methods(http.MethodGet)
  51. sr.HandleFunc("/feeds/{feedID}", handler.updateFeed).Methods(http.MethodPut)
  52. sr.HandleFunc("/feeds/{feedID}", handler.removeFeed).Methods(http.MethodDelete)
  53. sr.HandleFunc("/feeds/{feedID}/icon", handler.getIconByFeedID).Methods(http.MethodGet)
  54. sr.HandleFunc("/feeds/{feedID}/mark-all-as-read", handler.markFeedAsRead).Methods(http.MethodPut)
  55. sr.HandleFunc("/export", handler.exportFeeds).Methods(http.MethodGet)
  56. sr.HandleFunc("/import", handler.importFeeds).Methods(http.MethodPost)
  57. sr.HandleFunc("/feeds/{feedID}/entries", handler.getFeedEntries).Methods(http.MethodGet)
  58. sr.HandleFunc("/feeds/{feedID}/entries/import", handler.importFeedEntry).Methods(http.MethodPost)
  59. sr.HandleFunc("/feeds/{feedID}/entries/{entryID}", handler.getFeedEntry).Methods(http.MethodGet)
  60. sr.HandleFunc("/entries", handler.getEntries).Methods(http.MethodGet)
  61. sr.HandleFunc("/entries", handler.setEntryStatus).Methods(http.MethodPut)
  62. sr.HandleFunc("/entries/{entryID}", handler.getEntry).Methods(http.MethodGet)
  63. sr.HandleFunc("/entries/{entryID}", handler.updateEntry).Methods(http.MethodPut)
  64. sr.HandleFunc("/entries/{entryID}/bookmark", handler.toggleStarred).Methods(http.MethodPut)
  65. sr.HandleFunc("/entries/{entryID}/star", handler.toggleStarred).Methods(http.MethodPut)
  66. sr.HandleFunc("/entries/{entryID}/save", handler.saveEntry).Methods(http.MethodPost)
  67. sr.HandleFunc("/entries/{entryID}/fetch-content", handler.fetchContent).Methods(http.MethodGet)
  68. sr.HandleFunc("/flush-history", handler.flushHistory).Methods(http.MethodPut, http.MethodDelete)
  69. sr.HandleFunc("/icons/{iconID}", handler.getIconByIconID).Methods(http.MethodGet)
  70. sr.HandleFunc("/enclosures/{enclosureID}", handler.getEnclosureByID).Methods(http.MethodGet)
  71. sr.HandleFunc("/enclosures/{enclosureID}", handler.updateEnclosureByID).Methods(http.MethodPut)
  72. sr.HandleFunc("/integrations/status", handler.getIntegrationsStatus).Methods(http.MethodGet)
  73. sr.HandleFunc("/version", handler.versionHandler).Methods(http.MethodGet)
  74. sr.HandleFunc("/api-keys", handler.createAPIKey).Methods(http.MethodPost)
  75. sr.HandleFunc("/api-keys", handler.getAPIKeys).Methods(http.MethodGet)
  76. sr.HandleFunc("/api-keys/{apiKeyID}", handler.deleteAPIKey).Methods(http.MethodDelete)
  77. }
  78. func (h *handler) versionHandler(w http.ResponseWriter, r *http.Request) {
  79. json.OK(w, r, &versionResponse{
  80. Version: version.Version,
  81. Commit: version.Commit,
  82. BuildDate: version.BuildDate,
  83. GoVersion: runtime.Version(),
  84. Compiler: runtime.Compiler,
  85. Arch: runtime.GOARCH,
  86. OS: runtime.GOOS,
  87. })
  88. }