From 25365baa9060769c6df37480916bb5b23fb879fd Mon Sep 17 00:00:00 2001 From: Miguel Ángel Moreno Date: Tue, 20 Dec 2022 00:38:35 +0100 Subject: feat: Set up reitit routes and serve static index --- src/backend/tau/core.clj | 5 +- src/backend/tau/http.clj | 110 ++++++++++++++++++++++++++++++++++++++ src/backend/tau/services/http.clj | 73 ------------------------- 3 files changed, 113 insertions(+), 75 deletions(-) create mode 100644 src/backend/tau/http.clj delete mode 100644 src/backend/tau/services/http.clj (limited to 'src/backend/tau') diff --git a/src/backend/tau/core.clj b/src/backend/tau/core.clj index 8fb8b07..662f5b9 100644 --- a/src/backend/tau/core.clj +++ b/src/backend/tau/core.clj @@ -1,10 +1,11 @@ (ns tau.core + (:gen-class) (:require - [tau.services.http :as http])) + [tau.http :as http])) (defn -main [& _] - (http/start-server! 3000)) + (http/start-server!)) (defn reset [] diff --git a/src/backend/tau/http.clj b/src/backend/tau/http.clj new file mode 100644 index 0000000..1469f77 --- /dev/null +++ b/src/backend/tau/http.clj @@ -0,0 +1,110 @@ +(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]) + (: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})) + (println "Server running in port" port))) diff --git a/src/backend/tau/services/http.clj b/src/backend/tau/services/http.clj deleted file mode 100644 index 6101b52..0000000 --- a/src/backend/tau/services/http.clj +++ /dev/null @@ -1,73 +0,0 @@ -(ns tau.services.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.util.response :refer [response]] - [compojure.route :as route] - [compojure.core :refer :all] - [compojure.coercions :refer [as-int]] - [clojure.string :as str] - [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]) - (:import - tau.DownloaderImpl - org.schabi.newpipe.extractor.NewPipe - org.schabi.newpipe.extractor.localization.Localization)) - -(defonce server (atom nil)) - -(defn stop-server! - [] - (when @server - (@server :timeout 100) - (reset! server nil))) - -(defroutes app-routes - (context "/api" [] - (GET "/stream" [url] - (response (stream/get-stream-info url))) - (GET "/search" [serviceId :<< as-int q sortFilter contentFilters nextPage] - (let [content-filters (when contentFilters (str/split contentFilters #","))] - (response (if nextPage - (search/get-search-info serviceId q content-filters sortFilter nextPage) - (search/get-search-info serviceId q content-filters sortFilter))))) - (GET "/channel" [url nextPage] - (if nextPage - (response (channel/get-channel-info url nextPage)) - (response (channel/get-channel-info url)))) - (GET "/playlist" [url nextPage] - (if nextPage - (response (playlist/get-playlist-info url nextPage)) - (response (playlist/get-playlist-info url)))) - (GET "/comments" [url nextPage] - (if nextPage - (response (comment/get-comments-info url nextPage)) - (response (comment/get-comments-info url)))) - (GET "/services" [] - (response (service/get-service-list-info))) - (context "/kiosks" [] - (GET "/" [serviceId :<< as-int] - (response (kiosk/get-kiosk-list-info serviceId))) - (GET "/:kioskId" [kioskId serviceId :<< as-int nextPage] - (if nextPage - (response (kiosk/get-kiosk-info kioskId serviceId nextPage)) - (response (kiosk/get-kiosk-info kioskId serviceId))))))) - -(defn make-handler - [] - (-> #'app-routes - wrap-params - (wrap-json-response {:pretty true}) - wrap-reload)) - -(defn start-server! - [port] - (NewPipe/init (DownloaderImpl/init) (Localization. "en" "GB")) - (reset! server (run-server (make-handler) {:port port}))) -- cgit v1.2.3