File tree 8 files changed +151
-0
lines changed
8 files changed +151
-0
lines changed Original file line number Diff line number Diff line change
1
+ .idea /
2
+ .vscode /
3
+
4
+ __pycache__ /
5
+ * .py [cod ]
6
+ * $py.class
7
+ venv /
8
+ env /
9
+
10
+ .DS_Store
Original file line number Diff line number Diff line change
1
+ FROM python:3.9-buster
2
+
3
+ WORKDIR /app
4
+
5
+ COPY requirements.txt .
6
+ RUN pip install --no-cache-dir -r requirements.txt
7
+
8
+ COPY . .
9
+
10
+ CMD [ "python" , "app.py" ]
Original file line number Diff line number Diff line change
1
+ from flask import Flask
2
+ from flask_restful import Api
3
+ from flask_apispec import FlaskApiSpec
4
+
5
+ from resources import HelloWorld , Car
6
+ from models import db
7
+
8
+
9
+ def create_app ():
10
+ app = Flask (__name__ )
11
+
12
+ # Config
13
+ app .config ['SQLALCHEMY_ECHO' ] = False
14
+ app .config ['SECRET_KEY' ] = 'super_secret_key'
15
+ app .config ['SQLALCHEMY_DATABASE_URI' ] = 'postgresql://postgres:the_pass@db:5432/the_db'
16
+ app .config ['SQLALCHEMY_TRACK_MODIFICATIONS' ] = False
17
+
18
+ # DB
19
+ db .app = app
20
+ db .init_app (app )
21
+ # just for demo :)
22
+ db .drop_all ()
23
+ db .create_all ()
24
+
25
+ # API
26
+ api = Api (app )
27
+ api .add_resource (HelloWorld , '/' )
28
+ api .add_resource (Car , '/car' )
29
+
30
+ # Spec (/swagger & /swagger-ui)
31
+ docs = FlaskApiSpec (app )
32
+ docs .register (Car )
33
+
34
+ return app
35
+
36
+
37
+ if __name__ == '__main__' :
38
+ app = create_app ()
39
+ app .run (port = 7654 , host = '0.0.0.0' )
Original file line number Diff line number Diff line change
1
+ from flask_sqlalchemy import SQLAlchemy
2
+
3
+ db = SQLAlchemy ()
4
+
5
+
6
+ class CarsModel (db .Model ):
7
+ __tablename__ = 'cars'
8
+
9
+ id = db .Column (db .Integer , primary_key = True )
10
+ name = db .Column (db .String ())
11
+ doors = db .Column (db .Integer ())
12
+
13
+ def __init__ (self , name , doors ):
14
+ self .name = name
15
+ self .doors = doors
16
+
17
+ def __repr__ (self ):
18
+ return f"<Car { self .name } >"
Original file line number Diff line number Diff line change
1
+ Flask == 1.1.2
2
+ flask-apispec == 0.11.0
3
+ Flask-RESTful == 0.3.8
4
+ Flask-SQLAlchemy == 2.5.1
5
+ psycopg2-binary == 2.8.6
Original file line number Diff line number Diff line change
1
+ from flask_restful import Resource
2
+ from flask_apispec import MethodResource , marshal_with , use_kwargs
3
+ import marshmallow as ma
4
+
5
+ from models import db , CarsModel
6
+
7
+
8
+ class HelloWorld (Resource ):
9
+ def get (self ):
10
+ return {'hello' : 'world' }
11
+
12
+
13
+ class CarSchema (ma .Schema ):
14
+ name = ma .fields .Str ()
15
+ doors = ma .fields .Int ()
16
+
17
+
18
+ class MessageSchema (ma .Schema ):
19
+ message = ma .fields .Str ()
20
+
21
+
22
+ class Car (MethodResource , Resource ):
23
+ @marshal_with (CarSchema (many = True ), code = 200 )
24
+ def get (self ):
25
+ cars = CarsModel .query .all ()
26
+ return [
27
+ {
28
+ "name" : car .name ,
29
+ "doors" : car .doors
30
+ } for car in cars ]
31
+
32
+ @use_kwargs (CarSchema )
33
+ @marshal_with (MessageSchema , code = 200 )
34
+ def post (self , ** kwargs ):
35
+ if any (param not in kwargs for param in ["name" , "doors" ]):
36
+ return {"message" : "missed an argument" }, 400
37
+ new_car = CarsModel (name = kwargs ["name" ], doors = kwargs ["doors" ])
38
+ db .session .add (new_car )
39
+ db .session .commit ()
40
+ return {"message" : f"car { new_car .name } has been created successfully." }
41
+
Original file line number Diff line number Diff line change
1
+ version : ' 3'
2
+
3
+ services :
4
+ app :
5
+ build : ' ./app'
6
+ restart : always
7
+ ports :
8
+ - 7654:7654
9
+ # we should use something like `wait-for-it.sh` in real life
10
+
11
+ db :
12
+ image : ' postgres:13-alpine'
13
+ environment :
14
+ POSTGRES_DB : ' the_db'
15
+ POSTGRES_USER : ' postgres'
16
+ POSTGRES_PASSWORD : ' the_pass'
17
+ expose :
18
+ - 5432
19
+ volumes :
20
+ - db_data:/var/lib/postgresql/data
21
+
22
+ volumes :
23
+ db_data:
Original file line number Diff line number Diff line change
1
+ # Run
2
+ docker-compose up --build
3
+
4
+ # Documentation & Demo
5
+ http://localhost:7654/swagger-ui
You can’t perform that action at this time.
0 commit comments