Bootstrap Flask application
This commit is contained in:
parent
2053e116a4
commit
203438ab7c
3 changed files with 60 additions and 7 deletions
52
app/server.py
Normal file
52
app/server.py
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
#!/usr/bin/python
|
||||||
|
# -*- coding: UTF-8 -*-
|
||||||
|
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import logging
|
||||||
|
from flask import Flask
|
||||||
|
from werkzeug.contrib.fixers import ProxyFix
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
# add current and parent path to syspath
|
||||||
|
currentPath = os.path.dirname(__file__)
|
||||||
|
parentPath = os.path.abspath(os.path.join(currentPath, os.path.pardir))
|
||||||
|
paths = [currentPath, parentPath]
|
||||||
|
for path in paths:
|
||||||
|
if path not in sys.path:
|
||||||
|
sys.path.insert(0, path)
|
||||||
|
|
||||||
|
# configure logging
|
||||||
|
|
||||||
|
import config
|
||||||
|
|
||||||
|
|
||||||
|
def configure_logging(level):
|
||||||
|
root_logger = logging.getLogger()
|
||||||
|
root_logger.setLevel(level)
|
||||||
|
ch = logging.StreamHandler()
|
||||||
|
ch.setLevel(level)
|
||||||
|
# create formatter
|
||||||
|
formatter = logging.Formatter('[%(asctime)s] %(name)s %(levelname)s %(message)s')
|
||||||
|
# add formatter to ch
|
||||||
|
ch.setFormatter(formatter)
|
||||||
|
# add ch to logger
|
||||||
|
root_logger.addHandler(ch)
|
||||||
|
|
||||||
|
logging_level = (20, 10)[config.DEBUG]
|
||||||
|
configure_logging(logging_level)
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
# initialize database
|
||||||
|
from app.services import database
|
||||||
|
database.setup()
|
||||||
|
|
||||||
|
app.wsgi_app = ProxyFix(app.wsgi_app)
|
||||||
|
|
||||||
|
logger.info("Start Stacosys application")
|
||||||
|
|
||||||
|
app.run(host=config.HTTP_ADDRESS,
|
||||||
|
port=config.HTTP_PORT,
|
||||||
|
debug=config.DEBUG, use_reloader=False)
|
|
@ -29,11 +29,7 @@ def hash(value):
|
||||||
|
|
||||||
@provide_db
|
@provide_db
|
||||||
def setup(db):
|
def setup(db):
|
||||||
from app.models.user import User
|
from app.models.site import Site
|
||||||
db.create_tables([User], safe=True)
|
from app.models.comment import Comment
|
||||||
|
|
||||||
# create admin user if user table is empty
|
db.create_tables([Site, Comment], safe=True)
|
||||||
if User.select().count() == 0:
|
|
||||||
admin_user = User(username='admin', password=hash('admin'),
|
|
||||||
displayname='Admin')
|
|
||||||
admin_user.save()
|
|
||||||
|
|
|
@ -1,3 +1,8 @@
|
||||||
# Configuration file
|
# Configuration file
|
||||||
|
|
||||||
|
DEBUG = True
|
||||||
|
|
||||||
DB_URL = "mysql://stacosys_user:stacosys_password@localhost:3306/stacosys"
|
DB_URL = "mysql://stacosys_user:stacosys_password@localhost:3306/stacosys"
|
||||||
|
|
||||||
|
HTTP_ADDRESS = "0.0.0.0"
|
||||||
|
HTTP_PORT = 8000
|
||||||
|
|
Loading…
Add table
Reference in a new issue