routes.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package server // import "miniflux.app/v2/internal/http/server"
  4. import (
  5. "net/http"
  6. "miniflux.app/v2/internal/api"
  7. "miniflux.app/v2/internal/config"
  8. "miniflux.app/v2/internal/fever"
  9. "miniflux.app/v2/internal/googlereader"
  10. "miniflux.app/v2/internal/storage"
  11. "miniflux.app/v2/internal/ui"
  12. "miniflux.app/v2/internal/worker"
  13. )
  14. func newRouter(store *storage.Storage, pool *worker.Pool) http.Handler {
  15. readinessProbe := newReadinessProbe(store)
  16. // Application routes served under the base path.
  17. appMux := http.NewServeMux()
  18. appMux.HandleFunc("GET /healthcheck", readinessProbe)
  19. // Fever API routing.
  20. feverHandler := fever.Middleware(store)(fever.NewHandler(store))
  21. appMux.Handle("/fever/", feverHandler)
  22. // Google Reader API routing.
  23. googleReaderHandler := googlereader.NewHandler(store)
  24. appMux.HandleFunc("POST /accounts/ClientLogin", googleReaderHandler.ServeHTTP)
  25. appMux.Handle("/reader/api/0/", googleReaderHandler)
  26. // REST API routing.
  27. if config.Opts.HasAPI() {
  28. appMux.Handle("/v1/", api.NewHandler(store, pool))
  29. }
  30. // Metrics endpoint.
  31. if config.Opts.HasMetricsCollector() {
  32. appMux.Handle("GET /metrics", metricsHandler())
  33. }
  34. // UI routing (catch-all).
  35. appMux.Handle("/", ui.Serve(store, pool))
  36. // Apply shared middleware.
  37. var appHandler http.Handler = appMux
  38. if config.Opts.HasMaintenanceMode() {
  39. appHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  40. w.Write([]byte(config.Opts.MaintenanceMessage()))
  41. })
  42. }
  43. appHandler = middleware(appHandler)
  44. // Root router: health probes at root, app routes under base path.
  45. rootMux := http.NewServeMux()
  46. // These routes do not take the base path into consideration and are always available at the root of the server.
  47. rootMux.HandleFunc("/liveness", livenessProbe)
  48. rootMux.HandleFunc("/healthz", livenessProbe)
  49. rootMux.HandleFunc("/readiness", readinessProbe)
  50. rootMux.HandleFunc("/readyz", readinessProbe)
  51. basePath := config.Opts.BasePath()
  52. if basePath != "" {
  53. rootMux.Handle(basePath+"/", http.StripPrefix(basePath, appHandler))
  54. } else {
  55. rootMux.Handle("/", appHandler)
  56. }
  57. return rootMux
  58. }