-
Notifications
You must be signed in to change notification settings - Fork 6
/
app.py
executable file
·125 lines (98 loc) · 2.92 KB
/
app.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
# local imports
import modules.admin as admin
import modules.irc as irc
import modules.paste as paste
import modules.sanity as sanity
import modules.utils as utils
import bottle
import configparser
import redis
# Load Settings
conf = configparser.ConfigParser({
'cache_host': 'localhost',
'cache_db': 0,
'cache_ttl': 360,
'port': 80,
'root_path': '.',
'url': '',
'relay_enabled': True,
'relay_chan': '',
'relay_port': 5050,
'relay_pass': 'nil',
'recaptcha_sitekey': '',
'recaptcha_secret': '',
'check_spam': False,
'admin_key': '',
'python_server': 'auto'})
conf.read('conf/settings.cfg')
app = application = bottle.Bottle()
cache = redis.StrictRedis(host=conf.get('bottle', 'cache_host'), db=int(conf.get('bottle', 'cache_db')))
@app.route('/static/<filename:path>')
def static(filename):
'''
Serve static files
'''
return bottle.static_file(filename, root='{}/static'.format(conf.get('bottle', 'root_path')))
@app.error(500)
@app.error(404)
def errors(code):
'''
Handler for errors
'''
return bottle.jinja2_template('error.html', code=code)
@app.route('/')
def new_paste():
'''
Display page for new empty post
'''
(data, template) = paste.new_paste(conf)
return bottle.jinja2_template('paste.html', data=data, tmpl=template)
@app.route('/f/<paste_id>')
def new_fork(paste_id):
'''
Display page for new fork from a paste
'''
(data, template) = paste.new_paste(conf, cache, paste_id)
return bottle.jinja2_template('paste.html', data=data, tmpl=template)
@app.route('/', method='POST')
def submit_paste():
'''
Put a new paste into the database
'''
return paste.submit_new(conf, cache)
@app.route('/<paste_id>')
def view_paste(paste_id):
'''
Return page with <paste_id>.
'''
data = paste.get_paste(cache, paste_id)
return bottle.jinja2_template('view.html', paste=data, pid=paste_id)
@app.route('/r/<paste_id>')
def view_raw(paste_id):
'''
View raw paste with <paste_id>.
'''
data = paste.get_paste(cache, paste_id, raw=True)
bottle.response.add_header('Content-Type', 'text/plain; charset=utf-8')
return data['code']
@app.route('/d/<orig>/<fork>')
def view_diff(orig, fork):
'''
View the diff between a paste and what it was forked from
'''
if not cache.exists('paste:' + orig) or not cache.exists('paste:' + fork):
return bottle.jinja2_template('error.html', code=200,
message='At least one paste could not be found.')
diff = paste.gen_diff(cache, orig, fork)
return bottle.jinja2_template('page.html', data=diff)
@app.post('/admin')
def exec_admin():
return admin.run_cmd(conf, cache)
@app.route('/about')
def show_info():
return bottle.jinja2_template('about.html')
if __name__ == '__main__':
app.run(
host='0.0.0.0',
port=conf.getint('bottle', 'port'))