aboutsummaryrefslogtreecommitdiff
path: root/src/backend/tubo/router.clj
blob: 8a031b54352eff3ec0be697faa78659d16d0c09b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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]}))