-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
59 lines (49 loc) · 1.57 KB
/
app.py
File metadata and controls
59 lines (49 loc) · 1.57 KB
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
from flask import Flask, jsonify
from flask_restful import Resource, Api
from flask_sqlalchemy import SQLAlchemy
from flask_swagger_ui import get_swaggerui_blueprint
import json
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
api = Api(app)
# Define a sample resource
class HelloWorld(Resource):
def get(self):
return jsonify({'message': 'Hello, World!'})
# Define a User model
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
def __repr__(self):
return f'<User {self.name}>'
# Define a resource to return a list of users
class Users(Resource):
def get(self):
users = User.query.all()
user_list = [{'id': user.id, 'name': user.name, 'email': user.email} for user in users]
return jsonify(user_list)
# Add the resources to the API
api.add_resource(HelloWorld, '/')
api.add_resource(Users, '/users')
# Configure Swagger UI
SWAGGER_URL = '/swagger1'
API_URL = '/swagger1.json'
swaggerui_blueprint = get_swaggerui_blueprint(
SWAGGER_URL,
API_URL,
config={
'app_name': "Sample API"
}
)
app.register_blueprint(swaggerui_blueprint, url_prefix=SWAGGER_URL)
@app.route('/swagger1.json')
def swagger():
with open('swagger1.json', 'r') as f:
return jsonify(json.load(f))
if __name__ == '__main__':
with app.app_context():
db.create_all()
app.run(debug=True)