-
Notifications
You must be signed in to change notification settings - Fork 0
/
ada.py
65 lines (52 loc) · 1.99 KB
/
ada.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import random
from os.path import abspath, dirname
import cherrypy
from configparser import ConfigParser
from db import db_service as db
import string
class Ada(object):
@cherrypy.expose
def index(self):
return open(PATH + "/ressources/home.html")
@cherrypy.expose
def default(self, target, **kwargs):
res = db.getSomething("url",target)
if res:
raise cherrypy.HTTPRedirect(res["og"])
else:
raise cherrypy.HTTPError("404")
@cherrypy.expose
def addLink(self, link, pref=None, **kwargs):
if not pref:
uid = ''.join(random.sample(B62, 7))
while 1:
if db.getSomething("url",uid):
uid = ''.join(random.sample(B62, 7))
else:
db.insertDict("url",{"id": uid,"og":link})
return uid
else:
uid=pref
while 1:
if db.getSomething("url",uid):
uid = pref + ''.join(random.sample(B62, 3))
else :
db.insertDict("url",{"id": uid,"og":link})
return uid
config = ConfigParser()
PATH = dirname(abspath(__file__))
B62 = string.digits + string.ascii_letters
def importConf():
try:
config.read(PATH + '/ada.ini')
except:
print("please create a ada.ini file")
if __name__ == '__main__':
importConf()
db = db.DB(user=config.get('DB', 'DB_USER'), password=config.get('DB', 'DB_PASSWORD'),
host=config.get('DB', 'DB_HOST'), port=int(config.get('DB', 'DB_PORT')), db=config.get('DB', 'DB_NAME'))
cherrypy.config.update({'server.socket_host': config.get('server', 'IP'),
'server.socket_port': int(config.get('server', 'PORT')), 'tools.staticdir.on': True,
'tools.staticdir.dir': abspath(PATH + '/ressources'),
'error_page.404': PATH + "/ressources/404.html"})
cherrypy.quickstart(Ada())