-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
251 lines (204 loc) · 7.88 KB
/
main.py
File metadata and controls
251 lines (204 loc) · 7.88 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os, sqlite3
from beaker.middleware import SessionMiddleware, CacheMiddleware
from beaker.cache import CacheManager, Cache
from beaker.session import Session
# from beaker.util import parse_cache_config_options
import jsend
# fix: RuntimeError: Bottle requires gevent.monkey.patch_all() (before import)
# import gevent.monkey; gevent.monkey.patch_all()
import bottle
from bottle import Bottle, request, response, run, redirect
from bottle import template, TEMPLATE_PATH, static_file, install
from bottle_sqlite import SQLitePlugin
from bottlejwt import JwtPlugin
# from bottle_cors_plugin import cors_plugin
from bottle_injector import InjectorPlugin
import setting
bottle.BaseRequest.MEMFILE_MAX = setting.wsgi_request_maxsize # type: ignore
tmpl_path = setting.tmpl_path
uploads_path = setting.uploads_path
downloads_path = setting.downloads_path
img_path :str = os.path.join(setting.public_path, 'img')
css_path :str = os.path.join(setting.public_path, 'css')
js_path :str = os.path.join(setting.public_path, 'js')
cache_control = setting.cache_control
session_path = setting.session_path
cache_path = setting.cache_path
TEMPLATE_PATH.insert(0, tmpl_path)
WHITELIST_USER = {}
WHITELIST_USERNAME = ''
root = Bottle()
app_session = SessionMiddleware(root, setting.session_opts)
app_cache = CacheMiddleware(app_session, setting.cache_opts)
apps = app_cache
# website hook, before_request
@root.hook('before_request')
def setup_request():
request.session = request.environ['beaker.session']
request.cache = request.environ['beaker.cache']
cacheManager: CacheManager = request.cache
request.dbmcache = cacheManager.get_cache('dbmcache', type='dbm')
request.memcache = cacheManager.get_cache('memcache', type='memory')
request.filecache = cacheManager.get_cache('filecache', type='file')
if request.urlparts.path.startswith('/admin'): # type: ignore
if not request.session.has_key('username'):
redirect('/login/')
# website img
@root.route('/img/<filename:path>') # type: ignore
def img(filename:str):
response = static_file(filename, root=img_path)
if cache_control:
response.set_header('Cache-Control', cache_control)
return response
# website css
@root.route('/css/<filename:path>') # type: ignore
def css(filename:str):
response = static_file(filename, root=css_path)
if cache_control:
response.set_header('Cache-Control', cache_control)
return response
# website js
@root.route('/js/<filename:path>') # type: ignore
def js(filename:str):
response = static_file(filename, root=js_path)
if cache_control:
response.set_header('Cache-Control', cache_control)
return response
# website uploads
@root.route('/uploads/<filename:path>') # type: ignore
def uploads(filename:str):
response = static_file(filename, root=uploads_path)
if cache_control:
response.set_header('Cache-Control', cache_control)
return response
# website uploads
@root.route('/downloads/<filename:path>') # type: ignore
def downloads(filename:str):
resposne = static_file(filename, root=downloads_path)
if cache_control:
response.set_header('Cache-Control', cache_control)
return response
# website index
@root.route('/') # type: ignore
def index():
return 'index'
# website submit username and password, generate JWT
@root.route('/signin', method='POST') # type: ignore
def signin():
response.headers['Content-Type'] = 'application/json'
params:dict[str:str|None] = request.params; # type: ignore
username = params.get('username')
password = params.get('password')
# print(username)
# print(password)
if username and password and (username in WHITELIST_USER) and (password == WHITELIST_USER[username]):
jwt = JwtPlugin.encode({'username':username})
data = {'jwt':jwt}
return jsend.success(data).stringify()
response.status = 409
return jsend.error('invalid username and password').stringify()
# website jwtinfo
@root.route('/jwtinfo', auth='username') # type: ignore
def jwtinfo(auth):
response.headers['Content-Type'] = 'application/json'
return auth
# website testdb
@root.route('/testdb') # type: ignore
def testdb(db:sqlite3.Connection):
sql = 'SELECT sqlite_version() AS version'
cx = db.execute(sql)
row = cx.fetchone()
output = row['version']
return output
# website testcache
@root.route('/testcache') # type: ignore
def testcache():
memcache:Cache = request.memcache
memcache.put('test1', 'value1')
memcache.put('test2', 'value2')
test1 = memcache.get('test1')
test2 = memcache.get('test2')
output = {
'test1':test1,
'test2':test2
}
return output
# website testsess
@root.route('/testsess') # type: ignore
def testsess():
session:Session = request.session
if not session.has_key('counter'):
request.session['counter'] = 0
else:
request.session['counter'] += 1
session.save()
counter = session.get('counter')
output = {
'counter':counter,
}
return output
@root.route('/hello/<name>') # type: ignore
def hello(name:str)->str:
tmpl = template('index.tpl', name=name, pageTitle='HelloPage')
return tmpl
# website JWT validation callback
def jwtValidation(auth:dict, name:str)->bool:
username = auth.get(name)
if username in WHITELIST_USER:
return True
return False
if __name__ == '__main__':
if not os.path.exists(uploads_path):
os.makedirs(uploads_path)
if not os.path.exists(downloads_path):
os.makedirs(downloads_path)
mkdocs_default_docs = os.path.join(setting.mkdocs_default_path, setting.mkdocs_default_site, 'docs')
from utils import init_mkdocs
init_mkdocs(mkdocs_default_docs)
from gai import prompt as gaiPrompt
from gai import content as qaiContent
from injector import injector as my_injector
dbfile = setting.app_setting.get('db.dbfile')
jwtkey = setting.app_setting.get('jwt.secret')
allitems = setting.app_setting.items()
prefix = 'user.'
for key, val in allitems:
if key.startswith(prefix):
key = key[len(prefix):]
WHITELIST_USER[key] = val
root.install(SQLitePlugin(dbfile=dbfile))
'''
prompt.prompt.install(SQLitePlugin(dbfile=dbfile))
content.content.install(SQLitePlugin(dbfile=dbfile))
'''
# root.install(cors_plugin('*'))
'''
prompt.prompt.install(cors_plugin('*'))
content.content.install(cors_plugin('*'))
'''
root.install(JwtPlugin(jwtValidation, jwtkey))
gaiPrompt.aiPrompt.install(JwtPlugin(jwtValidation, jwtkey))
qaiContent.aiContent.install(JwtPlugin(jwtValidation, jwtkey))
gaiPrompt.aiPrompt.install(InjectorPlugin(my_injector))
qaiContent.aiContent.install(InjectorPlugin(my_injector))
root.mount('/aiprompt', gaiPrompt.aiPrompt)
root.mount('/aicontent', qaiContent.aiContent)
from signin import signIn
signIn.install(InjectorPlugin(my_injector))
root.mount('/login', signIn)
from admin.index import admin
admin.install(InjectorPlugin(my_injector))
root.mount('/admin/', admin)
host = setting.app_setting.get('wsgi.host')
port = setting.app_setting.get('wsgi.port')
run(app=apps, host=host, port=port, debug=setting.wsgi_debug, reloader=setting.wsgi_reloader, quiet=setting.wsgi_quiet)
'''
# use gevent; pip install gevent;
logger = my_injector.get_instance('logger')
run(server='gevent', app=apps, host=host, port=port,
debug=setting.wsgi_debug, reloader=setting.wsgi_reloader, quiet=setting.wsgi_quiet,
log = logger, error_log = logger,
)
'''