-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·51 lines (40 loc) · 1.38 KB
/
main.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from services import day_ahead_prices
from flask import Flask, Blueprint
import logging
import os
import threading
import time
DEFAULT_PORT = 5000
main_api = Blueprint('main_api', __name__)
@main_api.route("/")
def home():
return "<html>This is api.warp-charger.com.</br>This API is used by WARP Chargers to obtain relevant public information like day ahead prices.</html>"
def backend_tasks():
global running
day_ahead_prices.update()
running = True
while running:
time.sleep(5*60)
try:
day_ahead_prices.update()
except:
logging.error("Exception during day ahead price update", exc_info=True)
# Flask init
app = Flask(__name__)
app.register_blueprint(main_api)
app.register_blueprint(day_ahead_prices.day_ahead_prices_api)
app.config["JSON_SORT_KEYS"] = False
port = int(os.environ.get('PORT', DEFAULT_PORT))
running = False
backend_thread = threading.Thread(target=backend_tasks)
logging.basicConfig(filename='debug.log', level=logging.DEBUG, format="[%(asctime)s %(levelname)-8s%(filename)s:%(lineno)s] %(message)s", datefmt='%Y-%m-%d %H:%M:%S')
backend_thread.start()
# Wait for first data update
while not running:
time.sleep(1)
if __name__ == "__main__":
app.run(debug=True, use_reloader=False, host="0.0.0.0", port=port)
running = False
backend_thread.join()