aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMiguel Ángel Moreno <mail@migalmoreno.com>2022-12-26 21:49:13 +0100
committerMiguel Ángel Moreno <mail@migalmoreno.com>2022-12-26 21:49:13 +0100
commitdce9e36b352509665887ea1ca2e6f81904038a11 (patch)
tree2ba797188500e2677e3182f38d2334307be3e295 /src
parent9361d6444548748445db1548d0e3a6291d802bd3 (diff)
feat(backend): Modularize components
Diffstat (limited to 'src')
-rw-r--r--src/backend/tau/handler.clj71
-rw-r--r--src/backend/tau/http.clj102
-rw-r--r--src/backend/tau/router.clj55
3 files changed, 134 insertions, 94 deletions
diff --git a/src/backend/tau/handler.clj b/src/backend/tau/handler.clj
new file mode 100644
index 0000000..3dc5219
--- /dev/null
+++ b/src/backend/tau/handler.clj
@@ -0,0 +1,71 @@
+(ns tau.handler
+ (:require
+ [clojure.string :as str]
+ [hiccup.page :as hiccup]
+ [ring.util.response :refer [response]]
+ [tau.api.stream :as stream]
+ [tau.api.search :as search]
+ [tau.api.channel :as channel]
+ [tau.api.playlist :as playlist]
+ [tau.api.comment :as comment]
+ [tau.api.kiosk :as kiosk]
+ [tau.api.service :as service]))
+
+(defn index
+ [_]
+ (response
+ (hiccup/html5
+ [:head
+ [:meta {:charset "UTF-8"}]
+ [:meta {:name "viewport" :content "width=device-width, initial-scale=1"}]
+ [:title "Tau"]
+ (hiccup/include-css "/css/tau.css")]
+ [:body
+ [:div#app]
+ (hiccup/include-js "/js/main.js")
+ [:script "tau.core.init();"]])))
+
+(defn search
+ [{:keys [parameters] :as req}]
+ (let [{:keys [service-id]} (:path parameters)
+ {:keys [q]} (:query parameters)
+ {:strs [contentFilters sortFilter nextPage]} (:query-params req)
+ content-filters (and contentFilters (str/split contentFilters #","))]
+ (response (if nextPage
+ (search/get-info service-id q contentFilters sortFilter nextPage)
+ (search/get-info service-id q contentFilters sortFilter)))))
+
+(defn channel
+ [{{:keys [url]} :path-params {:strs [nextPage]} :query-params}]
+ (response (if nextPage
+ (channel/get-info url nextPage)
+ (channel/get-info url))))
+
+(defn playlist
+ [{{:keys [url]} :path-params {:strs [nextPage]} :query-params}]
+ (response (if nextPage
+ (playlist/get-info url nextPage)
+ (playlist/get-info url))))
+
+(defn comments
+ [{{:keys [url]} :path-params {:strs [nextPage]} :query-params}]
+ (response (if nextPage
+ (comment/get-info url nextPage)
+ (comment/get-info url))))
+
+(defn services
+ [_]
+ (response (service/get-services)))
+
+(defn kiosks
+ [{{{:keys [service-id]} :path} :parameters}]
+ (response (kiosk/get-kiosks service-id)))
+
+(defn kiosk
+ [{{{:keys [kiosk-id service-id]} :path} :parameters {:strs [nextPage]} :query-params}]
+ (response (if nextPage
+ (kiosk/get-info kiosk-id service-id nextPage)
+ (kiosk/get-info kiosk-id service-id))))
+
+(defn stream [{{:keys [url]} :path-params}]
+ (response (stream/get-info url)))
diff --git a/src/backend/tau/http.clj b/src/backend/tau/http.clj
index 1469f77..cbb5896 100644
--- a/src/backend/tau/http.clj
+++ b/src/backend/tau/http.clj
@@ -1,110 +1,24 @@
(ns tau.http
(:require
[org.httpkit.server :refer [run-server]]
- [ring.middleware.reload :refer [wrap-reload]]
- [ring.middleware.params :refer [wrap-params]]
- [ring.middleware.json :refer [wrap-json-response]]
- [ring.middleware.cors :refer [wrap-cors]]
- [ring.util.response :refer [response]]
- [reitit.ring :as ring]
- [reitit.coercion :as coercion]
- [reitit.ring.coercion :as rrc]
- [reitit.coercion.malli]
- [clojure.string :as str]
- [malli.experimental.lite :as l]
- [hiccup.page :as hiccup]
- [tau.api.stream :as stream]
- [tau.api.search :as search]
- [tau.api.channel :as channel]
- [tau.api.playlist :as playlist]
- [tau.api.comment :as comment]
- [tau.api.kiosk :as kiosk]
- [tau.api.service :as service])
+ [tau.router :as router])
(:import
tau.DownloaderImpl
org.schabi.newpipe.extractor.NewPipe
org.schabi.newpipe.extractor.localization.Localization))
-(defn index-html
- []
- (hiccup/html5
- [:head
- [:meta {:charset "UTF-8"}]
- [:meta {:name "viewport" :content "width=device-width, initial-scale=1"}]
- [:title "Tau"]
- (hiccup/include-css "/css/output.css")]
- [:body
- [:div#app]
- (hiccup/include-js "/js/main.js")
- [:script "tau.core.init();"]]))
-
(defonce server (atom nil))
-(def router
- (ring/router
- [["/" (constantly (response (index-html)))]
- ["/search" (constantly (response (index-html)))]
- ["/stream" (constantly (response (index-html)))]
- ["/channel" (constantly (response (index-html)))]
- ["/api"
- ["/stream" {:get (fn [{{:strs [url]} :query-params}]
- (response (stream/get-info url)))}]
- ["/search" {:get {:coercion reitit.coercion.malli/coercion
- :parameters {:query {:serviceId int?
- :q string?
- ;; :sortFilter (l/maybe string?)
- ;; :contentFilters (l/maybe string?)
- ;; :nextPage (l/maybe string?)
- }}
- :handler (fn [{:keys [parameters]}]
- (let [{:keys [contentFilters serviceId q sortFilter nextPage]} (:query parameters)
- content-filters (and contentFilters (str/split contentFilters #","))]
- (response (apply search/get-info serviceId q
- content-filters sortFilter
- (or nextPage '())))))}}]
- ["/channel" {:get (fn [{{:keys [url nextPage]} :query-params}]
- (response (channel/get-info url nextPage)))}]
- ["/playlist" {:get (fn [{{:keys [url nextPage]} :query-params}]
- (response (playlist/get-info url nextPage)))}]
- ["/comments" {:get (fn [{{:keys [url nextPage]} :query-params}]
- (response (apply comment/get-info url (or nextPage '()))))}]
- ["/services" {:get (constantly (response (service/get-services)))}]
- ["/kiosks"
- ["" {:coercion reitit.coercion.malli/coercion
- :parameters {:query {:serviceId int?}}
- :get (fn [{:keys [parameters]}]
- (println parameters)
- (response (kiosk/get-kiosks (-> parameters :query :serviceId))))}]
- ["/:kioskId" {:get {:coercion reitit.coercion.malli/coercion
- :parameters {:query {:serviceId int?}}
- :handler (fn [{{:keys [kioskId serviceId nextPage]} :query-params}]
- (response (kiosk/get-info kioskId serviceId nextPage)))}}]]]]
- ;;{:compile coercion/compile-request-coercers}
- {: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]}))
-
-(defn stop-server!
- []
- (when @server
- (@server :timeout 100)
- (reset! server nil)))
-
(defn start-server!
([]
(start-server! 3000))
([port]
(NewPipe/init (DownloaderImpl/init) (Localization. "en" "GB"))
- (reset! server (run-server #'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/tau/router.clj b/src/backend/tau/router.clj
new file mode 100644
index 0000000..d6573e9
--- /dev/null
+++ b/src/backend/tau/router.clj
@@ -0,0 +1,55 @@
+(ns tau.router
+ (:require
+ [malli.experimental.lite :as l]
+ [reitit.ring :as ring]
+ [reitit.coercion :as coercion]
+ [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]]
+ [ring.middleware.cors :refer [wrap-cors]]
+ [tau.handler :as handler]))
+
+(def router
+ (ring/router
+ [["/" handler/index]
+ ["/search" handler/index]
+ ["/stream" handler/index]
+ ["/channel" handler/index]
+ ["/playlist" handler/index]
+ ["/kiosk" handler/index]
+ ["/api"
+ ["/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/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}]
+ ["/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]}))