aboutsummaryrefslogtreecommitdiff
path: root/src/dionysus/web/admin.clj
blob: 28d83019d227edd24667b7f75685ad2bbcee4915 (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
(ns dionysus.web.admin
  (:require [reitit.ring :as rring]
            [org.httpkit.server :as http-server]
            [ring.middleware.oauth2 :as rmoauth2]
            [dotenv :as env]
            [ring.middleware.params :as rmparams]
            [ring.middleware.session :as rmsessions]
            [dionysus.utils :as dutils]

            [dionysus.web.admin.home :as dwahome]
            [dionysus.web.admin.home.blacklist :as dwahblacklist]
            [dionysus.web.admin.home.server-settings :as dwahserver-settings]))

(def ^:private routes
  [["/" {:get {:handler dwahome/handle}}]
   ["/blacklist"
    ["/" {:get {:handler dwahblacklist/handle}}]
    ["/add" {:post {:handler dwahblacklist/handle-add}}]
    ["/change/:index" {:post {:handler dwahblacklist/handle-change}}]
    ["/delete/:index" {:delete {:handler dwahblacklist/handle-delete}}]]
   ["/server-settings" {:post {:handler dwahserver-settings/handle}}]
   ["/assets/*" (rring/create-resource-handler)]])

(def ^:private handler
  (-> routes
      rring/router
      (rring/ring-handler (rring/redirect-trailing-slash-handler))
      (rmoauth2/wrap-oauth2 {:spotify {:authorize-uri    "https://accounts.spotify.com/authorize"
                                       :access-token-uri "https://accounts.spotify.com/api/token"
                                       :client-id        (env/env "SPOTIFY_CLIENT_ID")
                                       :client-secret    (env/env "SPOTIFY_CLIENT_SECRET")
                                       :scopes           ["user-read-private"
                                                          "user-modify-playback-state"
                                                          "user-read-currently-playing"]
                                       :launch-uri       "/auth"
                                       :redirect-uri     "/callback"
                                       :landing-uri      "/"}})
      rmsessions/wrap-session
      rmparams/wrap-params
      delay))

(def ^:private stop-fn (atom nil))

(defn start-server! []
  (when (fn? @stop-fn) (@stop-fn))
  (reset! stop-fn (http-server/run-server @handler {:port (-> (env/env "ADMIN_PORT")
                                                              (dutils/try-parse-int 8081))})))
#_(start-server!)