aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2019-03-14 23:40:35 +0000
committers-ol <s-ol@users.noreply.github.com>2019-03-14 23:42:29 +0000
commitf20a09da3d09f54842254706b0cf2e3d05a82ca1 (patch)
treec3e97328e4ff37f023a95c6032910ed718daf29f
downloadredirectly-f20a09da3d09f54842254706b0cf2e3d05a82ca1.tar.gz
redirectly-f20a09da3d09f54842254706b0cf2e3d05a82ca1.zip
initial commit
-rw-r--r--.gitignore2
-rw-r--r--project.clj11
-rw-r--r--resources/config.edn19
-rw-r--r--src/redirectly/core.clj32
4 files changed, 64 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c9a630f
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.lein-repl-history
+target
diff --git a/project.clj b/project.clj
new file mode 100644
index 0000000..9ae0b54
--- /dev/null
+++ b/project.clj
@@ -0,0 +1,11 @@
+(defproject redirectly "0.1.0-SNAPSHOT"
+ :description "mini redirect service"
+ :url "https://s-ol.nu/redirectly"
+ :plugins [[lein-ring "0.12.5"]]
+ :ring {:handler redirectly.core/handler
+ :destroy redirectly.core/destroy}
+ :dependencies [[org.clojure/clojure "1.10.0"]
+ [sonian/carica "1.2.2"]
+ [ring/ring-core "1.7.1"]
+ [ring/ring-jetty-adapter "1.7.1"]
+ [ring-logger "1.0.1"]])
diff --git a/resources/config.edn b/resources/config.edn
new file mode 100644
index 0000000..7443162
--- /dev/null
+++ b/resources/config.edn
@@ -0,0 +1,19 @@
+{:routes {"" {:to [:mmm]}
+ :plonat-atek {:to [:mmm "/games/plonat_atek"]}
+ :iii-telefoni {:to [:mmm "/projects/iii-telefoni"]}}
+ :404 "
+ <html>
+ <head>
+ <meta charset=\"UTF-8\">
+ <title>not found</title>
+ </head>
+ <body>
+ <h3>entry not found :(</h3>
+ <p>
+ 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.
+ </p>
+ <p>
+ in the meantime, you may find what you were looking for at <a href=\"mmm.s-ol.nu\">mmm.s-ol.nu</a>.
+ </p>
+ </body>
+ </html>"}
diff --git a/src/redirectly/core.clj b/src/redirectly/core.clj
new file mode 100644
index 0000000..8b7025f
--- /dev/null
+++ b/src/redirectly/core.clj
@@ -0,0 +1,32 @@
+(ns redirectly.core
+ (:require [carica.core :refer [config clear-config-cache!]]
+ [ring.util.response :as response]))
+
+(def routes (config :routes))
+
+(defn matches? [[slug route] uri]
+ (when (= uri (str "/" (name slug)))
+ route))
+
+(defn status [route]
+ (or (:status route) 307))
+
+(defn url [route]
+ (:to route))
+
+(defmulti url (fn [{to :to}]
+ (if (vector? to)
+ (first to)
+ :url)))
+
+(defmethod url :url [{to :to}] to)
+(defmethod url :mmm [{[_ path] :to}] (str "//mmm.s-ol.nu" path))
+
+(defn handler [req]
+ (if-let [route (some #(matches? % (:uri req)) routes)]
+ (response/redirect (url route) (status route))
+ (-> (response/not-found (config :404))
+ (response/content-type "text/html"))))
+
+(defn destroy []
+ (clear-config-cache!))