git.fiddlerwoaroof.com
fiddlerwoaroof authored on 18/03/2018 22:18:30
Showing 3 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1 @@
1
+*.fasl
0 2
new file mode 100644
... ...
@@ -0,0 +1,107 @@
1
+(defpackage :redirector.graph
2
+  (:use :closer-common-lisp :graph-db)
3
+  (:export
4
+   #:site
5
+   #:short-name
6
+   #:shortens
7
+   #:init
8
+   #:lookup-site-by-abbreviation
9
+   #:url
10
+   #:description
11
+   #:with-db
12
+   #:all-abbreviations
13
+   #:name))
14
+(in-package :redirector.graph)
15
+(log:config :all :sane :d :nopretty :thread :daily "/var/tmp/graph.log")
16
+
17
+(def-vertex site ()
18
+  ((url :type string)
19
+   (description :type string))
20
+  :fwoar-shortener)
21
+
22
+(def-vertex short-name ()
23
+  ((name :type string))
24
+  :fwoar-shortener)
25
+
26
+(def-edge shortens ()
27
+  ()
28
+  :fwoar-shortener)
29
+
30
+(defun init (path)
31
+  (let ((*graph* (or *graph*
32
+                     (graph-db:make-graph :fwoar-shortener path))))
33
+    (graph-db:def-view shortens-by-abbrev :lessp (redirector.graph::shortens :fwoar-shortener)
34
+                          (:map (lambda (shorten-edge)
35
+                                  (yield (name (lookup-vertex (from shorten-edge))) nil))))))
36
+
37
+(defun all-abbreviations ()
38
+  (select (:flat t)
39
+    ((?site ?abbrev))
40
+    (shortens ?abbrev ?site)))
41
+
42
+(defun lookup-site-by-abbreviation (abbrev)
43
+  (declare (special abbrev))
44
+  (select (:flat t)
45
+    (?site)
46
+    (lisp ?abbrev-name abbrev)
47
+    (shortens ?abbrev ?site)
48
+    (is ?abbrev-name (name ?abbrev))))
49
+
50
+(defmacro with-db ((path) &body body)
51
+  (alexandria:once-only (path)
52
+    `(let ((*graph* (or *graph*
53
+                        (lookup-graph :fwoar-shortener)
54
+                        (if (probe-file ,path)
55
+                            (open-graph :fwoar-shortener ,path)
56
+                            (init ,path)))))
57
+       ,@body)))
58
+
59
+(defpackage :redirector.main
60
+  (:import-from :araneus :define-controller :define-view :define-routes)
61
+  (:use :closer-common-lisp))
62
+(in-package :redirector.main)
63
+
64
+(define-controller abbreviation (params)
65
+  (redirector.graph:with-db ("/tmp/shortening-graph/")
66
+    (redirector.graph:lookup-site-by-abbreviation
67
+     (cdr (assoc :route params)))))
68
+
69
+(define-view redirect ((model list))
70
+  (if model
71
+      `(302 (:Location ,(redirector.graph:url (car model)))
72
+            (""))
73
+      '(404 () ("not found..."))))
74
+
75
+(define-controller list-abbreviations (params)
76
+  (redirector.graph:with-db ("/tmp/shortening-graph/")
77
+    (mapcar (fw.lu:destructuring-lambda ((site abbrev))
78
+              (with-accessors ((url redirector.graph:url)
79
+                               (description redirector.graph:description)) site
80
+                (with-accessors ((name redirector.graph:name)) abbrev
81
+                  (list url description name))))
82
+            (redirector.graph:all-abbreviations))))
83
+
84
+(araneus:define-view list-abbreviations ((abbreviation-list list))
85
+  (redirector.graph:with-db ("/tmp/shortening-graph/")
86
+    (spinneret:with-html-string
87
+      (:html
88
+       (:head)
89
+       (:body
90
+        (:ul.shortcuts 
91
+         (mapcar (fw.lu:destructuring-lambda ((url description name))
92
+                   (:li.shortcut (:a :href (format nil "/r/~a" name)
93
+                                     (:span.abbrev name)
94
+                                     (:span.description description)
95
+                                     (:span.url url))))
96
+                 abbreviation-list)))))))
97
+
98
+(define-routes shortener-routes
99
+  (("/" :method :GET) (araneus:as-route 'list-abbreviations))
100
+  (("/r/:route" :method :GET) (araneus:compose-route (abbreviation) redirect)))
101
+
102
+(defun main (&optional (path "/tmp/shortening-graph/"))
103
+  (redirector.graph:with-db (path)
104
+    (let ((app (make-instance 'ningle:<app>)))
105
+      (shortener-routes app)
106
+      (values (clack:clackup app)
107
+              app))))
0 108
new file mode 100644
... ...
@@ -0,0 +1,18 @@
1
+;;; -*- Mode:Lisp; Syntax:ANSI-Common-Lisp; Package: ASDF-USER -*-
2
+(in-package :asdf-user)
3
+
4
+(defsystem :redirector 
5
+    :description ""
6
+    :author "Ed L <edward@elangley.org>"
7
+    :license "MIT"
8
+    :depends-on (#:alexandria
9
+                 #:uiop
10
+                 #:serapeum
11
+                 #:araneus
12
+                 #:spinneret
13
+                 #:lquery
14
+                 #:graph-db
15
+                 #:fwoar.lisputils
16
+                 #:closer-mop)
17
+    :serial t
18
+    :components ((:file "main")))