aboutsummaryrefslogtreecommitdiff
path: root/src/backend
diff options
context:
space:
mode:
authorMiguel Ángel Moreno <mail@migalmoreno.com>2024-12-19 02:21:55 +0100
committerMiguel Ángel Moreno <mail@migalmoreno.com>2024-12-19 02:21:55 +0100
commit62d5d995345fa4ae8814f76cfa4f3eb930a7d9a1 (patch)
tree354779ed86601126f6342e271cf2142643905d4a /src/backend
parent13e285955508e601d6a648c25e29632340b4d9b6 (diff)
refactor: make routing shared between frontend and backend
Diffstat (limited to 'src/backend')
-rw-r--r--src/backend/tubo/http.clj4
-rw-r--r--src/backend/tubo/router.clj84
-rw-r--r--src/backend/tubo/routes.clj64
3 files changed, 86 insertions, 66 deletions
diff --git a/src/backend/tubo/http.clj b/src/backend/tubo/http.clj
index 200b52e..95913d6 100644
--- a/src/backend/tubo/http.clj
+++ b/src/backend/tubo/http.clj
@@ -1,7 +1,7 @@
(ns tubo.http
(:require
[org.httpkit.server :refer [run-server]]
- [tubo.routes :as routes])
+ [tubo.router :as router])
(:import
tubo.DownloaderImpl
org.schabi.newpipe.extractor.NewPipe
@@ -13,7 +13,7 @@
([] (start-server! 3000))
([port]
(NewPipe/init (DownloaderImpl/init) (Localization. "en" "US"))
- (reset! server (run-server #'routes/app {:port port}))
+ (reset! server (run-server #'router/app {:port port}))
(println "Server running in port" port)))
(defn stop-server! [] (when @server (@server :timeout 100) (reset! server nil)))
diff --git a/src/backend/tubo/router.clj b/src/backend/tubo/router.clj
new file mode 100644
index 0000000..8a031b5
--- /dev/null
+++ b/src/backend/tubo/router.clj
@@ -0,0 +1,84 @@
+(ns tubo.router
+ (:require
+ [reitit.core :as r]
+ [reitit.ring :as ring]
+ [reitit.ring.coercion :as rrc]
+ [reitit.coercion.malli]
+ [reitit.swagger :as swagger]
+ [reitit.swagger-ui :as swagger-ui]
+ [ring.middleware.reload :refer [wrap-reload]]
+ [ring.middleware.params :refer [wrap-params]]
+ [ring.middleware.json :refer [wrap-json-response]]
+ [tubo.handler :as handler]
+ [tubo.routes :as routes]))
+
+(defn expand-routes
+ [data opts]
+ (if (keyword? data)
+ (case data
+ :api/services {:get {:summary "returns all supported services"
+ :handler handler/services}}
+ :api/search {:get {:summary
+ "returns search results for a given service"
+ :coercion reitit.coercion.malli/coercion
+ :parameters {:path {:service-id int?}
+ :query {:q string?}}
+ :handler handler/search}}
+ :api/default-kiosk {:get
+ {:summary
+ "returns default kiosk entries for a given service"
+ :coercion reitit.coercion.malli/coercion
+ :parameters {:path {:service-id int?}}
+ :handler handler/kiosk}}
+ :api/all-kiosks {:get {:summary
+ "returns all kiosks supported by a given service"
+ :coercion reitit.coercion.malli/coercion
+ :parameters {:path {:service-id int?}}
+ :handler handler/kiosks}}
+ :api/kiosk {:get
+ {:summary
+ "returns kiosk entries for a given service and a kiosk ID"
+ :coercion reitit.coercion.malli/coercion
+ :parameters {:path {:service-id int? :kiosk-id string?}}
+ :handler handler/kiosk}}
+ :api/stream {:get {:summary "returns stream data for a given URL"
+ :handler handler/stream}}
+ :api/channel {:get {:summary "returns channel data for a given URL"
+ :handler handler/channel}}
+ :api/channel-tab {:get
+ {:summary
+ "returns channel tab data for a given URL and a tab ID"
+ :handler handler/channel-tabs}}
+ :api/playlist {:get {:summary "returns playlist data for a given URL"
+ :handler handler/playlist}}
+ :api/comments {:get {:summary "returns comments data for a given URL"
+ :handler handler/comments}}
+ :api/swagger-spec {:no-doc true
+ :get {:swagger {:info {:title "Tubo API"}
+ :basePath "/"}
+ :handler (swagger/create-swagger-handler)}}
+ :api/swagger-ui {:no-doc true
+ :get (swagger-ui/create-swagger-ui-handler)}
+ {:no-doc true
+ :handler handler/index})
+ (r/expand data opts)))
+
+(def router
+ (ring/router
+ routes/routes
+ {:expand expand-routes
+ :data {:middleware [rrc/coerce-request-middleware
+ rrc/coerce-response-middleware
+ rrc/coerce-exceptions-middleware]}}))
+
+(def app
+ (ring/ring-handler
+ router
+ (ring/routes
+ (ring/create-resource-handler {:path "/"})
+ (ring/redirect-trailing-slash-handler {:method :add})
+ (ring/create-default-handler
+ {:not-found (constantly {:status 404 :body "Not found"})}))
+ {:middleware [wrap-params
+ [wrap-json-response {:pretty true}]
+ wrap-reload]}))
diff --git a/src/backend/tubo/routes.clj b/src/backend/tubo/routes.clj
deleted file mode 100644
index e8dd536..0000000
--- a/src/backend/tubo/routes.clj
+++ /dev/null
@@ -1,64 +0,0 @@
-(ns tubo.routes
- (:require
- [reitit.ring :as ring]
- [reitit.ring.coercion :as rrc]
- [reitit.coercion.malli]
- [ring.middleware.reload :refer [wrap-reload]]
- [ring.middleware.params :refer [wrap-params]]
- [ring.middleware.json :refer [wrap-json-response]]
- [tubo.handler :as handler]))
-
-(def router
- (ring/router
- [["/" handler/index]
- ["/search" handler/index]
- ["/stream" handler/index]
- ["/channel" handler/index]
- ["/playlist" handler/index]
- ["/kiosk" handler/index]
- ["/settings" handler/index]
- ["/bookmark" handler/index]
- ["/bookmarks" handler/index]
- ["/api/v1"
- ["/services"
- ["" {:get handler/services}]
- ["/:service-id/search"
- {:get {:coercion reitit.coercion.malli/coercion
- :parameters {:path {:service-id int?}
- :query {:q string?}}
- :handler handler/search}}]
- ["/:service-id"
- ["/default-kiosk"
- {:get {:coercion reitit.coercion.malli/coercion
- :parameters {:path {:service-id int?}}
- :handler handler/kiosk}}]
- ["/kiosks"
- [""
- {:get {:coercion reitit.coercion.malli/coercion
- :parameters {:path {:service-id int?}}
- :handler handler/kiosks}}]
- ["/:kiosk-id"
- {:get {:coercion reitit.coercion.malli/coercion
- :parameters {:path {:service-id int? :kiosk-id string?}}
- :handler handler/kiosk}}]]]]
- ["/streams/:url" {:get handler/stream}]
- ["/channels"
- ["/:url"
- ["" {:get handler/channel}]
- ["/tabs/:tab-id" {:get handler/channel-tabs}]]]
- ["/playlists/:url" {:get handler/playlist}]
- ["/comments/:url" {:get handler/comments}]]]
- {:data {:middleware [rrc/coerce-request-middleware
- rrc/coerce-response-middleware
- rrc/coerce-exceptions-middleware]}}))
-
-(def app
- (ring/ring-handler
- router
- (ring/routes
- (ring/create-resource-handler {:path "/"})
- (ring/create-default-handler
- {:not-found (constantly {:status 404 :body "Not found"})}))
- {:middleware [wrap-params
- [wrap-json-response {:pretty true}]
- wrap-reload]}))