-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
65 lines (42 loc) · 1.49 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
from flask import Flask, Blueprint, url_for, jsonify
from flask_restplus import Resource, Api, apidoc
from flask_restplus_relative_swagger import FlaskRestplusRelativeSwagger
import psycopg2
import os
app = Flask(__name__)
blueprint = Blueprint('api', __name__, url_prefix='/api')
class MyApi(Api):
@property
def specs_url(self):
"""Monkey patch for HTTPS"""
scheme = 'http' if '7070' in self.base_url else 'https'
return url_for(self.endpoint('specs'), _external=True, _scheme=scheme)
api = MyApi(blueprint, doc='/doc/', version='1.0', title='My api',
description="My api")
@api.documentation
def swagger_ui():
return apidoc.ui_for(api)
app.register_blueprint(blueprint)
@api.route('/hello')
class HelloWorld(Resource):
def get(self):
return {'hello': 'world'}
# Change Swagger relative path on server
DATABASE_URL = os.environ.get("DATABASE_URL")
@api.route('/testdb', methods=['GET'])
class postgres_test(Resource):
def get(self):
try:
conn = psycopg2.connect(DATABASE_URL)
conn.close()
return "Database Connected"
except:
return "Error connexion Database"
@api.route('/film', methods=['GET'])
class hello2(Resource):
def get(self):
str = "value1", "value2", "value3", "value4", "value5", "value6"
return jsonify(str)
api_doc = FlaskRestplusRelativeSwagger()
api_doc.init_app(app=app, url='/api/doc/')
app.run(debug=True, host='0.0.0.0', port=7070)