81 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Python
		
	
	
			
		
		
	
	
			81 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Python
		
	
	
'''
 | 
						|
Main application file
 | 
						|
'''
 | 
						|
 | 
						|
import eventlet
 | 
						|
eventlet.monkey_patch()
 | 
						|
 | 
						|
from flask import Flask, jsonify
 | 
						|
from flask_socketio import SocketIO
 | 
						|
 | 
						|
from datetime import datetime
 | 
						|
import dbo
 | 
						|
 | 
						|
# region Logger
 | 
						|
import logging
 | 
						|
from debug import setup_logging
 | 
						|
 | 
						|
log = logger = logging.getLogger("default")
 | 
						|
setup_logging()
 | 
						|
# endregion
 | 
						|
 | 
						|
from configuration import read_config
 | 
						|
from peewee import fn
 | 
						|
 | 
						|
config = read_config()
 | 
						|
 | 
						|
app = Flask(__name__)
 | 
						|
app.config['SECRET_KEY'] = 'BLAAAA_GeneerateMeDynamicallyForBetterSecurity'
 | 
						|
 | 
						|
socketio = SocketIO(app, async_mode='eventlet')
 | 
						|
 | 
						|
 | 
						|
# app.jinja_env.filters['html_line_breaks'] = jinja_filters.html_line_breaks
 | 
						|
# app.jinja_env.filters['zfill'] = jinja_filters.zfill
 | 
						|
 | 
						|
@app.context_processor
 | 
						|
def inject_global_variables():
 | 
						|
    return dict(
 | 
						|
        config=config,
 | 
						|
        now=datetime.utcnow(),
 | 
						|
        Accounting=dbo.Accounting,
 | 
						|
        MonthlyArchive=dbo.MonthlyArchive,
 | 
						|
        fn=fn
 | 
						|
    )
 | 
						|
 | 
						|
 | 
						|
def add_background_task(task, interval):
 | 
						|
    def tsk():
 | 
						|
        while True:
 | 
						|
            try:
 | 
						|
                log.debug(f"Running background task {task.__name__}...")
 | 
						|
                task()
 | 
						|
                log.debug(f"Completed background task {task.__name__}!")
 | 
						|
            except Exception as e:
 | 
						|
                log.error(f"Can't run background task '{task.__name__}': {e}", exc_info=True)
 | 
						|
            socketio.sleep(interval)
 | 
						|
 | 
						|
    socketio.start_background_task(tsk)
 | 
						|
 | 
						|
@app.route("/api/")
 | 
						|
def api_index():
 | 
						|
    return jsonify(
 | 
						|
        {""}
 | 
						|
    )
 | 
						|
 | 
						|
if __name__ == '__main__':
 | 
						|
    config = read_config()
 | 
						|
 | 
						|
    try:
 | 
						|
        if config['host'] == "0.0.0.0":
 | 
						|
            host = 'localhost'
 | 
						|
        else:
 | 
						|
            host = config['host']
 | 
						|
        log.info(f"Running at http://{host}:{config['port']}")
 | 
						|
        socketio.run(app, debug=False, host=config['host'], port=config['port'])
 | 
						|
    except:
 | 
						|
        print("Unable to start", exc_info=True)
 | 
						|
 | 
						|
 | 
						|
 |