-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroutes.lisp
More file actions
74 lines (62 loc) · 2.37 KB
/
routes.lisp
File metadata and controls
74 lines (62 loc) · 2.37 KB
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
(asdf:operate 'asdf:load-op :cl-who)
(asdf:operate 'asdf:load-op :restas)
(asdf:operate 'asdf:load-op :cl-ppcre)
(import :storage)
(restas:define-module :restas.routes
(:use :cl))
(in-package :restas.routes)
(restas:debug-mode-on)
(defparameter *response* "")
(setf (who:html-mode) :html5)
(restas:define-route main ("")
(pathname "~/workbase/suburl/res/index.html"))
(restas:define-route css ("index.css")
(pathname "~/workbase/suburl/res/index.css"))
(restas:define-route favicon ("favicon.ico")
(pathname "~/workbase/suburl/res/favicon.ico"))
(restas:define-route loader ("loader.gif")
(pathname "~/workbase/suburl/res/loader.gif"))
(restas:define-route urlSubmit ("" :method :post)
(setf inputShort (string-trim " " (hunchentoot:post-parameter "shortURL")))
(setf inputLong (string-trim " " (hunchentoot:post-parameter "longURL")))
(cond
((= (length inputShort) 0)
(setf *response* "The short URL cannot be empty."))
((not (storage::scanUrl inputLong))
(setf *response* "Invalid URL (Missing Protocol http/https/ftp/file)."))
((or (not (storage::validateUrl "((?:[A-Za-z0-9_]*))"
inputShort))
(not (< (length inputShort) 11)))
(setf *response* "Short URLs can contain only alphanumeric characters and underscore,
and cannot be more than 10 characters in length."))
((storage::longUrlExists inputLong)
(setf *response*
(who:with-html-output-to-string (out)
(:p "Link already exists at Short URL: ")
(:a :href
(storage::urlEncode inputLong)
(who:str (concatenate 'string "u.onloop.net/"
(storage::shortUrl (storage::getlongUrl inputLong))))))))
((storage::shortUrlExists inputShort)
(setf *response* "Short URL exists. Try another."))
(t (progn
(storage::addPair inputLong inputShort)
(setf *response* (who:with-html-output-to-string (out)
(:a :href inputShort
(who:str
(concatenate 'string "u.onloop.net/" inputShort))))))))
*response*)
(restas:define-route redir (":(input)/*params")
(if (not (storage::getshortUrl input))
(progn
(setf *response* "Invalid URL.")
*response*)
(progn
(setf *response*
(storage::concatList
(storage::mergeListItems
(ppcre:split "\\[\\*\\]" (storage::longUrl (storage::getshortUrl input)))
(storage::stringSplit (first params) #\,)
"")))
(restas:redirect *response*))))
(restas:start :restas.routes :port 8081)