git.s-ol.nu redirectly / f20a09d
initial commit s-ol 2 months ago
4 changed file(s) with 64 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
1 .lein-repl-history
2 target
1 (defproject redirectly "0.1.0-SNAPSHOT"
2 :description "mini redirect service"
3 :url "https://s-ol.nu/redirectly"
4 :plugins [[lein-ring "0.12.5"]]
5 :ring {:handler redirectly.core/handler
6 :destroy redirectly.core/destroy}
7 :dependencies [[org.clojure/clojure "1.10.0"]
8 [sonian/carica "1.2.2"]
9 [ring/ring-core "1.7.1"]
10 [ring/ring-jetty-adapter "1.7.1"]
11 [ring-logger "1.0.1"]])
1 {:routes {"" {:to [:mmm]}
2 :plonat-atek {:to [:mmm "/games/plonat_atek"]}
3 :iii-telefoni {:to [:mmm "/projects/iii-telefoni"]}}
4 :404 "
5 <html>
6 <head>
7 <meta charset=\"UTF-8\">
8 <title>not found</title>
9 </head>
10 <body>
11 <h3>entry not found :(</h3>
12 <p>
13 if you followed a link here, please let me know at s+missing <i>&lt;ät&gt;</i> s-ol <i>&lt;döt&gt;</i> nu.
14 </p>
15 <p>
16 in the meantime, you may find what you were looking for at <a href=\"mmm.s-ol.nu\">mmm.s-ol.nu</a>.
17 </p>
18 </body>
19 </html>"}
1 (ns redirectly.core
2 (:require [carica.core :refer [config clear-config-cache!]]
3 [ring.util.response :as response]))
4
5 (def routes (config :routes))
6
7 (defn matches? [[slug route] uri]
8 (when (= uri (str "/" (name slug)))
9 route))
10
11 (defn status [route]
12 (or (:status route) 307))
13
14 (defn url [route]
15 (:to route))
16
17 (defmulti url (fn [{to :to}]
18 (if (vector? to)
19 (first to)
20 :url)))
21
22 (defmethod url :url [{to :to}] to)
23 (defmethod url :mmm [{[_ path] :to}] (str "//mmm.s-ol.nu" path))
24
25 (defn handler [req]
26 (if-let [route (some #(matches? % (:uri req)) routes)]
27 (response/redirect (url route) (status route))
28 (-> (response/not-found (config :404))
29 (response/content-type "text/html"))))
30
31 (defn destroy []
32 (clear-config-cache!))