This repository has been archived by the owner on Oct 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
150 lines (122 loc) · 3.99 KB
/
app.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Manage root libs
import random, os, signal, sys, yaml
import logging, logging.config
# Registration to Consul
from consul import Consul
# Flask Stuff
from flask import Flask
from flask_cors import CORS
from flask_jwt import JWT
# Tornado WebServer
from tornado.web import Application, FallbackHandler
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from tornado.netutil import bind_sockets
# Conf Loader
from urllib.request import urlopen
from urllib.error import HTTPError
# Load db connector
from Models import db
# Loading blueprints
from Views import health
# Loading Authentication tools
from Utils.auth import authenticate
from Utils.auth import identity
from Utils import errors
# Setup logging
logging.config.fileConfig('logging.conf')
# Override
APP_IP_ADDR = os.getenv('EXPOSED_ADDRESS', '127.0.0.1')
APP_CONSUL_ADDR = os.getenv('CONSUL_ADDRESS', '127.0.0.1')
APP_NAME = os.getenv('APP_NAME', 'app')
# Boostrap flask app
app = Flask(APP_NAME)
app.uniq_id = "".join(random.choice("abcdef0123456789") for x in range(16))
app.service_id = '%s-%s' % (app.name, app.uniq_id)
# Default config
app.config.update(
DEBUG=True,
UPLOAD_FOLDER='/tmp/',
SECRET_KEY='changeme',
JSONIFY_PRETTYPRINT_REGULAR=False,
SQLALCHEMY_TRACK_MODIFICATIONS=False
)
# Init DB Stuff
db.init_app(app)
# Init CORS
cors = CORS()
cors.init_app(app)
# Init JWT Secure layer
jwt = JWT()
jwt.authentication_callback = authenticate
jwt.identity_callback = identity
jwt.init_app(app)
# Errors Init
errors.init_app(app)
# Registering pages
app.register_blueprint(health)
#############################################
def __register_to_consul():
global app
tags = app.config['consul']['tags'] if 'consul' in app.config and 'tags' in app.config['consul'] else []
csl = Consul(token="", host=APP_CONSUL_ADDR)
agent = Consul.Agent.Service(csl)
agent.register(name=app.name,
service_id=app.service_id,
port=app.app_port,
tags=tags,
check={
"id": "health-%s" % app.name,
"name": "Health %s" % app.name,
"http": "http://%s:%s/health/" % (APP_IP_ADDR, app.app_port),
"interval": "15s",
"timeout": "1s"
})
def __unregister_from_consul():
global app
csl = Consul(token="", host=APP_CONSUL_ADDR)
agent = Consul.Agent.Service(csl)
agent.deregister(service_id=app.service_id)
# Prevent zombies on Consul whn signaled
def __signaled_unregister_from_consul(signum, frame):
__unregister_from_consul()
sys.exit(0)
if __name__ == '__main__':
# Create application
tr = Application([
(r".*", FallbackHandler, dict(fallback=WSGIContainer(app)))
])
# Load Config
csl = Consul(token="", host=APP_CONSUL_ADDR)
_, cfgs = csl.catalog.service('config')
try:
with urlopen("http://%s:%s/resources/support/config.yml" % (cfgs[0]['Address'], cfgs[0]['ServicePort'])) as f:
conf = yaml.load(f)
app.config.update(conf)
except HTTPError as e:
logging.getLogger(__name__).error("Unable to find config file")
sys.exit(1)
finally:
csl = None
# Bind to Random socket
sockets = bind_sockets(0, '')
server = HTTPServer(tr)
server.add_sockets(sockets)
# Save port
for s in sockets:
app.app_port = s.getsockname()[:2][1]
# Register to consul & run !
try:
__register_to_consul()
signal.signal(signal.SIGTERM, __signaled_unregister_from_consul)
signal.signal(signal.SIGINT, __signaled_unregister_from_consul)
IOLoop.instance().start()
except KeyboardInterrupt:
__unregister_from_consul(None, None)
csl = Consul(token="")
agent = Consul.Agent.Service(csl)
agent.deregister(service_id=app.service_id)
IOLoop.instance().stop()