-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.py
145 lines (113 loc) · 4.83 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
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
# BSD 3-Clause License
#
# Copyright (c) 2018, Pruthvi Kumar All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
# following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this list of conditions and the following
# disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided with the distribution.
#
# Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import json
import falcon
from apispec import APISpec
from configuration import ProtonConfig
from falcon_apispec import FalconPlugin
from falcon_cors import CORS
from mic.iface.middlewares.iface_watch import Iface_watch
from mic.iface.middlewares.proton_prometheus import ProtonPrometheus
from mic.iface.middlewares.token_authenticator import TokenAuthenticator
from nucleus.iam.login import IctrlProtonLogin
from nucleus.iam.signup import IctrlProtonSignup
from nucleus.iam.reset import IctrlProtonPasswordReset
__author__ = "Pruthvi Kumar, [email protected]"
__copyright__ = "Copyright (C) 2018 Pruthvi Kumar | http://www.apricity.co.in"
__license__ = "BSD 3-Clause License"
__version__ = "1.0"
"""
PROTON executor: Point WSGI server to this file and reach out to available routes!
"""
class DefaultRouteHandler(object):
"""
PROTON's default route handler.
"""
def __init__(self):
super(DefaultRouteHandler, self).__init__()
def on_get(self, req, resp):
response = {
'message': 'PROTON is successfully initialized!',
'availableRoutes': []
}
response['availableRoutes'].append('/login')
response['availableRoutes'].append('/signup')
response['availableRoutes'].append('/reset')
response['availableRoutes'].append('/proton-prom')
response['availableRoutes'].append('/proton-grafana')
resp.body = json.dumps(response)
resp.status = falcon.HTTP_200
class FastServe(object):
"""
This is a sink to service dynamically routed entries from cache!
"""
def __init__(self):
super(FastServe, self).__init__()
def on_get(self, req, resp):
resp.status = falcon.HTTP_200
class RedirectToProm(object):
"""
This is a 301 re-direction to Prometheus UI
"""
def __init__(self):
super(RedirectToProm, self).__init__()
def on_get(self, req, resp):
resp.status = falcon.HTTP_301
resp.set_header('Location', 'http://localhost:9090')
class RedirectToGrafana(object):
"""
This is a 301 re-direction to Grafana UI
"""
def __init__(self):
super(RedirectToGrafana, self).__init__()
def on_get(self, req, resp):
resp.status = falcon.HTTP_301
resp.set_header('Location', 'http://localhost:3001')
prom = ProtonPrometheus()
cors = CORS(allow_all_origins=True,
allow_all_headers=True,
allow_all_methods=True,)
app = falcon.API(middleware=[TokenAuthenticator(), cors.middleware, Iface_watch(), prom])
app.add_route('/', DefaultRouteHandler())
app.add_route('/fast-serve', FastServe())
app.add_route('/login', IctrlProtonLogin())
app.add_route('/signup', IctrlProtonSignup())
app.add_route('/reset', IctrlProtonPasswordReset())
app.add_route('/metrics', prom)
app.add_route('/proton-prom', RedirectToProm())
app.add_route('/proton-grafana', RedirectToGrafana())
# Open API Specs
spec = APISpec(
title='PROTON STACK',
version='1.0.0',
openapi_version='2.0',
plugins=[
FalconPlugin(app),
],
)
# OPEN API specs will be generated during runtime.
with open('{}/mic/iface/openApi/specs.json'.format(ProtonConfig().ROOT_DIR), 'w+') as sjf:
sjf.write(json.dumps(spec.to_dict()))
with open('{}/mic/iface/openApi/specs.yaml'.format(ProtonConfig().ROOT_DIR), 'w+') as syf:
syf.write(spec.to_yaml())