-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
70 lines (58 loc) · 2.18 KB
/
run.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
import os
from flask import send_from_directory, request, jsonify, make_response
from flask_cors import CORS
from urllib.parse import urlparse
from config import Config, FilesystemDBConfig, ComposeConfig
from src.app import CollectionsAPI
from src.collections.routes import routes as r_collections
from src.members.routes import routes as r_members
from src.service.routes import routes as r_service
from src.utils.base.swagger import swagger
from src.utils.base.errors import activate
from src.utils.conversions.json import *
app = CollectionsAPI(__name__)
app.config.from_object({
'filesystem': FilesystemDBConfig,
'docker-compose': ComposeConfig,
'default': Config
}.get(os.environ.get('COLLECTIONS_API_ENV'), Config))
if os.environ.get('COLLECTIONS_API_SETTINGS'):
app.config.from_envvar('COLLECTIONS_API_SETTINGS')
app.initialize(app.config)
CORS(app)
app.json_encoder = RDAJSONEncoder
app.json_decoder = RDAJSONDecoder
activate(app)
for (url, kwargs) in r_service + r_collections + r_members:
app.add_url_rule(url, **kwargs)
def index():
i = swagger.index(request.url+"apidocs" if request.url.endswith("/") else request.url+"/apidocs")
r = make_response(i)
r.mimetype = 'text/html'
return r
app.add_url_rule("/", methods=["GET"], view_func=index)
def apidocs():
url = urlparse(request.url)
if ":" in url.netloc:
host, port = url.netloc.split(":")
else:
host = url.netloc
port = "80"
base_path = url.path.replace('/apidocs','') if url.path != "/apidocs" else "/"
schemes = [url.scheme]
other_scheme = "https" if url.scheme is "http" else "http"
try:
if request.get(other_scheme+"://"+url.netloc+url.path.replace('/apidocs','')+"/scheme").status_code is 200:
schemes += [other_scheme]
except:
pass
r = make_response(swagger.json(schemes, host, port, base_path))
r.mimetype = 'application/json'
return r
# return send_from_directory("www","swagger.json")
app.add_url_rule("/apidocs", methods=["GET"], view_func=apidocs)
def scheme():
return jsonify(""), 200
app.add_url_rule("/scheme", methods=["GET"], view_func=scheme)
if __name__ == '__main__':
app.run(host='0.0.0.0')