-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
48 lines (40 loc) · 1.43 KB
/
server.py
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
import string
from os.path import abspath, dirname
from sakura import HTTPError, HTTPRedirect
from sakura.http import baseServer
import random
server = baseServer.BaseServer
PATH = dirname(abspath(__file__))
class Ada(server):
features = {"errors": {404: "/static/404.html"}, "orm": True}
@server.expose
def index(self):
return self.file(PATH + "/static/home.html")
@server.expose
def default(self, target, **kwargs):
target = target.strip('/')
res = self.db.getSomething("url",target)
if res and res != []:
raise HTTPRedirect(self.response,res["og"])
else:
raise HTTPError(self.response,404,"Not Found")
@server.expose
def addLink(self, link, pref=None, **kwargs):
if not pref:
uid = ''.join(random.sample(B62, 7))
while 1:
if self.db.getSomething("url",uid):
uid = ''.join(random.sample(B62, 7))
else:
self.db.insertDict("url",{"id": uid,"og":link})
return uid
else:
uid=pref
while 1:
if self.db.getSomething("url",uid):
uid = pref + ''.join(random.sample(B62, 3))
else :
self.db.insertDict("url",{"id": uid,"og":link})
return uid
B62 = string.digits + string.ascii_letters
Ada(path=PATH, configFile="/server.ini")