api.go 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package api // import "miniflux.app/api"
  5. import (
  6. "net/http"
  7. "miniflux.app/storage"
  8. "miniflux.app/worker"
  9. "github.com/gorilla/mux"
  10. )
  11. type handler struct {
  12. store *storage.Storage
  13. pool *worker.Pool
  14. router *mux.Router
  15. }
  16. // Serve declares API routes for the application.
  17. func Serve(router *mux.Router, store *storage.Storage, pool *worker.Pool) {
  18. handler := &handler{store, pool, router}
  19. sr := router.PathPrefix("/v1").Subrouter()
  20. middleware := newMiddleware(store)
  21. sr.Use(middleware.handleCORS)
  22. sr.Use(middleware.apiKeyAuth)
  23. sr.Use(middleware.basicAuth)
  24. sr.Methods(http.MethodOptions)
  25. sr.HandleFunc("/users", handler.createUser).Methods(http.MethodPost)
  26. sr.HandleFunc("/users", handler.users).Methods(http.MethodGet)
  27. sr.HandleFunc("/users/{userID:[0-9]+}", handler.userByID).Methods(http.MethodGet)
  28. sr.HandleFunc("/users/{userID:[0-9]+}", handler.updateUser).Methods(http.MethodPut)
  29. sr.HandleFunc("/users/{userID:[0-9]+}", handler.removeUser).Methods(http.MethodDelete)
  30. sr.HandleFunc("/users/{userID:[0-9]+}/mark-all-as-read", handler.markUserAsRead).Methods(http.MethodPut)
  31. sr.HandleFunc("/users/{username}", handler.userByUsername).Methods(http.MethodGet)
  32. sr.HandleFunc("/me", handler.currentUser).Methods(http.MethodGet)
  33. sr.HandleFunc("/categories", handler.createCategory).Methods(http.MethodPost)
  34. sr.HandleFunc("/categories", handler.getCategories).Methods(http.MethodGet)
  35. sr.HandleFunc("/categories/{categoryID}", handler.updateCategory).Methods(http.MethodPut)
  36. sr.HandleFunc("/categories/{categoryID}", handler.removeCategory).Methods(http.MethodDelete)
  37. sr.HandleFunc("/categories/{categoryID}/mark-all-as-read", handler.markCategoryAsRead).Methods(http.MethodPut)
  38. sr.HandleFunc("/categories/{categoryID}/feeds", handler.getCategoryFeeds).Methods(http.MethodGet)
  39. sr.HandleFunc("/categories/{categoryID}/entries", handler.getCategoryEntries).Methods(http.MethodGet)
  40. sr.HandleFunc("/categories/{categoryID}/entries/{entryID}", handler.getCategoryEntry).Methods(http.MethodGet)
  41. sr.HandleFunc("/discover", handler.discoverSubscriptions).Methods(http.MethodPost)
  42. sr.HandleFunc("/feeds", handler.createFeed).Methods(http.MethodPost)
  43. sr.HandleFunc("/feeds", handler.getFeeds).Methods(http.MethodGet)
  44. sr.HandleFunc("/feeds/counters", handler.fetchCounters).Methods(http.MethodGet)
  45. sr.HandleFunc("/feeds/refresh", handler.refreshAllFeeds).Methods(http.MethodPut)
  46. sr.HandleFunc("/feeds/{feedID}/refresh", handler.refreshFeed).Methods(http.MethodPut)
  47. sr.HandleFunc("/feeds/{feedID}", handler.getFeed).Methods(http.MethodGet)
  48. sr.HandleFunc("/feeds/{feedID}", handler.updateFeed).Methods(http.MethodPut)
  49. sr.HandleFunc("/feeds/{feedID}", handler.removeFeed).Methods(http.MethodDelete)
  50. sr.HandleFunc("/feeds/{feedID}/icon", handler.feedIcon).Methods(http.MethodGet)
  51. sr.HandleFunc("/feeds/{feedID}/mark-all-as-read", handler.markFeedAsRead).Methods(http.MethodPut)
  52. sr.HandleFunc("/export", handler.exportFeeds).Methods(http.MethodGet)
  53. sr.HandleFunc("/import", handler.importFeeds).Methods(http.MethodPost)
  54. sr.HandleFunc("/feeds/{feedID}/entries", handler.getFeedEntries).Methods(http.MethodGet)
  55. sr.HandleFunc("/feeds/{feedID}/entries/{entryID}", handler.getFeedEntry).Methods(http.MethodGet)
  56. sr.HandleFunc("/entries", handler.getEntries).Methods(http.MethodGet)
  57. sr.HandleFunc("/entries", handler.setEntryStatus).Methods(http.MethodPut)
  58. sr.HandleFunc("/entries/{entryID}", handler.getEntry).Methods(http.MethodGet)
  59. sr.HandleFunc("/entries/{entryID}/bookmark", handler.toggleBookmark).Methods(http.MethodPut)
  60. sr.HandleFunc("/entries/{entryID}/fetch-content", handler.fetchContent).Methods(http.MethodGet)
  61. }