2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2018-11-11 19:22:47 +01:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package api // import "miniflux.app/v2/internal/api"
|
2018-11-11 19:22:47 +01:00
|
|
|
|
|
|
|
import (
|
2020-06-22 06:13:00 +02:00
|
|
|
"net/http"
|
2023-10-09 00:21:38 +02:00
|
|
|
"runtime"
|
2020-06-22 06:13:00 +02:00
|
|
|
|
2023-10-09 00:21:38 +02:00
|
|
|
"miniflux.app/v2/internal/http/response/json"
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/storage"
|
2023-10-09 00:21:38 +02:00
|
|
|
"miniflux.app/v2/internal/version"
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/worker"
|
2018-11-11 19:22:47 +01:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
)
|
|
|
|
|
2021-01-05 00:32:32 +01:00
|
|
|
type handler struct {
|
2022-10-15 08:17:17 +02:00
|
|
|
store *storage.Storage
|
|
|
|
pool *worker.Pool
|
|
|
|
router *mux.Router
|
2021-01-05 00:32:32 +01:00
|
|
|
}
|
|
|
|
|
2018-11-11 19:22:47 +01:00
|
|
|
// Serve declares API routes for the application.
|
2021-01-03 01:33:41 +01:00
|
|
|
func Serve(router *mux.Router, store *storage.Storage, pool *worker.Pool) {
|
2022-10-15 08:17:17 +02:00
|
|
|
handler := &handler{store, pool, router}
|
2018-11-11 19:22:47 +01:00
|
|
|
|
|
|
|
sr := router.PathPrefix("/v1").Subrouter()
|
2020-03-02 02:38:29 +01:00
|
|
|
middleware := newMiddleware(store)
|
2020-06-16 19:05:07 +02:00
|
|
|
sr.Use(middleware.handleCORS)
|
2020-03-02 02:38:29 +01:00
|
|
|
sr.Use(middleware.apiKeyAuth)
|
|
|
|
sr.Use(middleware.basicAuth)
|
2020-06-22 06:13:00 +02:00
|
|
|
sr.Methods(http.MethodOptions)
|
|
|
|
sr.HandleFunc("/users", handler.createUser).Methods(http.MethodPost)
|
|
|
|
sr.HandleFunc("/users", handler.users).Methods(http.MethodGet)
|
|
|
|
sr.HandleFunc("/users/{userID:[0-9]+}", handler.userByID).Methods(http.MethodGet)
|
|
|
|
sr.HandleFunc("/users/{userID:[0-9]+}", handler.updateUser).Methods(http.MethodPut)
|
|
|
|
sr.HandleFunc("/users/{userID:[0-9]+}", handler.removeUser).Methods(http.MethodDelete)
|
2020-11-25 20:46:05 +01:00
|
|
|
sr.HandleFunc("/users/{userID:[0-9]+}/mark-all-as-read", handler.markUserAsRead).Methods(http.MethodPut)
|
2020-06-22 06:13:00 +02:00
|
|
|
sr.HandleFunc("/users/{username}", handler.userByUsername).Methods(http.MethodGet)
|
|
|
|
sr.HandleFunc("/me", handler.currentUser).Methods(http.MethodGet)
|
|
|
|
sr.HandleFunc("/categories", handler.createCategory).Methods(http.MethodPost)
|
|
|
|
sr.HandleFunc("/categories", handler.getCategories).Methods(http.MethodGet)
|
|
|
|
sr.HandleFunc("/categories/{categoryID}", handler.updateCategory).Methods(http.MethodPut)
|
|
|
|
sr.HandleFunc("/categories/{categoryID}", handler.removeCategory).Methods(http.MethodDelete)
|
2020-11-25 20:46:05 +01:00
|
|
|
sr.HandleFunc("/categories/{categoryID}/mark-all-as-read", handler.markCategoryAsRead).Methods(http.MethodPut)
|
2021-01-19 04:44:02 +01:00
|
|
|
sr.HandleFunc("/categories/{categoryID}/feeds", handler.getCategoryFeeds).Methods(http.MethodGet)
|
2022-12-11 18:07:01 +01:00
|
|
|
sr.HandleFunc("/categories/{categoryID}/refresh", handler.refreshCategory).Methods(http.MethodPut)
|
2021-01-19 04:44:02 +01:00
|
|
|
sr.HandleFunc("/categories/{categoryID}/entries", handler.getCategoryEntries).Methods(http.MethodGet)
|
|
|
|
sr.HandleFunc("/categories/{categoryID}/entries/{entryID}", handler.getCategoryEntry).Methods(http.MethodGet)
|
2021-01-04 22:49:28 +01:00
|
|
|
sr.HandleFunc("/discover", handler.discoverSubscriptions).Methods(http.MethodPost)
|
2020-06-22 06:13:00 +02:00
|
|
|
sr.HandleFunc("/feeds", handler.createFeed).Methods(http.MethodPost)
|
|
|
|
sr.HandleFunc("/feeds", handler.getFeeds).Methods(http.MethodGet)
|
2022-05-21 20:44:56 +02:00
|
|
|
sr.HandleFunc("/feeds/counters", handler.fetchCounters).Methods(http.MethodGet)
|
2020-06-22 06:13:00 +02:00
|
|
|
sr.HandleFunc("/feeds/refresh", handler.refreshAllFeeds).Methods(http.MethodPut)
|
|
|
|
sr.HandleFunc("/feeds/{feedID}/refresh", handler.refreshFeed).Methods(http.MethodPut)
|
|
|
|
sr.HandleFunc("/feeds/{feedID}", handler.getFeed).Methods(http.MethodGet)
|
|
|
|
sr.HandleFunc("/feeds/{feedID}", handler.updateFeed).Methods(http.MethodPut)
|
|
|
|
sr.HandleFunc("/feeds/{feedID}", handler.removeFeed).Methods(http.MethodDelete)
|
2023-10-06 07:23:29 +02:00
|
|
|
sr.HandleFunc("/feeds/{feedID}/icon", handler.getIconByFeedID).Methods(http.MethodGet)
|
2020-11-25 20:46:05 +01:00
|
|
|
sr.HandleFunc("/feeds/{feedID}/mark-all-as-read", handler.markFeedAsRead).Methods(http.MethodPut)
|
2020-06-22 06:13:00 +02:00
|
|
|
sr.HandleFunc("/export", handler.exportFeeds).Methods(http.MethodGet)
|
|
|
|
sr.HandleFunc("/import", handler.importFeeds).Methods(http.MethodPost)
|
|
|
|
sr.HandleFunc("/feeds/{feedID}/entries", handler.getFeedEntries).Methods(http.MethodGet)
|
|
|
|
sr.HandleFunc("/feeds/{feedID}/entries/{entryID}", handler.getFeedEntry).Methods(http.MethodGet)
|
|
|
|
sr.HandleFunc("/entries", handler.getEntries).Methods(http.MethodGet)
|
|
|
|
sr.HandleFunc("/entries", handler.setEntryStatus).Methods(http.MethodPut)
|
|
|
|
sr.HandleFunc("/entries/{entryID}", handler.getEntry).Methods(http.MethodGet)
|
2023-10-07 07:42:59 +02:00
|
|
|
sr.HandleFunc("/entries/{entryID}", handler.updateEntry).Methods(http.MethodPut)
|
2020-06-22 06:13:00 +02:00
|
|
|
sr.HandleFunc("/entries/{entryID}/bookmark", handler.toggleBookmark).Methods(http.MethodPut)
|
2023-07-31 00:33:56 +02:00
|
|
|
sr.HandleFunc("/entries/{entryID}/save", handler.saveEntry).Methods(http.MethodPost)
|
2022-01-27 05:29:37 +01:00
|
|
|
sr.HandleFunc("/entries/{entryID}/fetch-content", handler.fetchContent).Methods(http.MethodGet)
|
2023-10-06 06:37:45 +02:00
|
|
|
sr.HandleFunc("/flush-history", handler.flushHistory).Methods(http.MethodPut, http.MethodDelete)
|
2023-10-06 07:23:29 +02:00
|
|
|
sr.HandleFunc("/icons/{iconID}", handler.getIconByIconID).Methods(http.MethodGet)
|
2023-10-09 00:21:38 +02:00
|
|
|
sr.HandleFunc("/version", handler.versionHandler).Methods(http.MethodGet)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) versionHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
json.OK(w, r, &versionResponse{
|
|
|
|
Version: version.Version,
|
|
|
|
Commit: version.Commit,
|
|
|
|
BuildDate: version.BuildDate,
|
|
|
|
GoVersion: runtime.Version(),
|
|
|
|
Compiler: runtime.Compiler,
|
|
|
|
Arch: runtime.GOARCH,
|
|
|
|
OS: runtime.GOOS,
|
|
|
|
})
|
2018-11-11 19:22:47 +01:00
|
|
|
}
|