-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusicrack.py
97 lines (80 loc) · 2.44 KB
/
musicrack.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
# -*- coding: utf-8 -*-
import uuid
import datetime
from flask import Flask, render_template, session, request, g
from flaskext.mysql import MySQL
from flask.ext.cache import Cache
from views.session import session as v_session
from views.files import files as v_files
DEBUG = True
app = Flask(__name__)
# Configure
app.config['STORAGE_PATH'] = 'D:\\MusicRack\\store'
app.config['ALLOWED_EXTENSIONS'] = ('mp3', 'png', 'mp4')
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = DEBUG
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
app.config['MYSQL_DATABASE_PORT'] = 3306
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = None
app.config['MYSQL_DATABASE_DB'] = 'musicrack'
app.config['MYSQL_DATABASE_CHARSET'] = 'utf8'
app.config['SESSION_TIMEOUT'] = datetime.timedelta(7)
app.config['CACHE_CONFIG'] = {
'CACHE_TYPE': 'simple',
'CACHE_DEFAULT_TIMEOUT': 600
}
app.config['SECRET_KEY'] = 'rT4EgGRZ4uSrjPeU_ND9FwAdLD7EtAZw'
# DB Initialize
db = MySQL()
db.init_app(app)
# Cache Initialize
cache = Cache(config=app.config['CACHE_CONFIG'])
cache.init_app(app)
# Register session API
app.register_blueprint(v_session, url_prefix='/session')
# Register directory API
app.register_blueprint(v_files, url_prefix='/files')
@app.route('/')
def index():
return render_template('/index.html')
@app.route('/test')
def test():
return render_template('test/index.html')
@app.route('/initdb')
def initdb():
with app.open_resource('schema.sql', mode='r') as f:
script = f.read()
g.db.get_db().cursor().executemany(script, None)
return script
# Session handling
@app.before_request
def before_request():
# Session ID
g.db = db
g.cache = cache
import models
g.models = models
if 'sessionid' in request.cookies:
# Session ID exists
session_id = request.cookies['sessionid']
(verified, userid, groups, loggedin) = models.verify_session(session_id, request.remote_addr)
if not verified:
session_id = str(uuid.uuid4())
else:
g.user_id = userid
g.groups = groups
g.loggedin = loggedin
else:
# Not exists
session_id = str(uuid.uuid4())
g.session_id = session_id
@app.after_request
def after_request(response):
response.set_cookie('sessionid', value=g.session_id, max_age=app.config['SESSION_TIMEOUT'])
g.models.register_session(g.session_id,
request.remote_addr,
datetime.datetime.now() + app.config['SESSION_TIMEOUT'],
g.user_id, g.groups, g.loggedin)
return response
if __name__ == '__main__':
app.run(debug=True)