You can open this sample in the Dev Environments feature of Docker Desktop version 4.12 or later.
Open in Docker Dev Environments
-- Project structure:
├── db
│ └── password.txt
├── app
│ ├── main.py
│ ├── create_tables.py
│ ├── ...
│ └── Dockerfile
├── compose.yaml
├── requirements.txt
└── README.md
services:
db:
phpmyadmin:
api:
build: .
container_name: fastapi-application
environment:
PORT: 8000
ports:
- '8000:8000'
restart: "no"
docker-compose up -d --builddocker-compose psdocker-compose logs -f;Listing containers must show one container running and the port mapping as below:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7087a6e79610 5c1778a60cf8 "/start.sh" About a minute ago Up About a minute 80/tcp, 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp fastapi-application
After the application starts, navigate to http://localhost:8000 in your web browser and you should see the following json response:
{
"message": "OK"
}
-
Enter the container
docker-compose exec -it api /bin/bash -
Create and Populate the tables by executing
python create_tables.py -
Verify the creation by log in phpmyadmin http://localhost:8001/, username:root, password :root OR executing
docker exec -it fastapi-db-1 mysql -u root -p,key in root when asked password,USE example,show tables;,SELECT * FROM ohlc_history; -
Execute
pytestto run ALL the tests Troubleshoot tips: if there are erros, run each test alonepython connect_test.pytest DB connectionpython utils_test.pytest on functions in utils_pypython connect_test.pytest DB managerpython intg_test.pyintegration test on POST /data endpoint-- By right, it should insert to test DB, but here we insert into PROD DB for convenience -
Test service by trying GET /, expected result is {"message": "OK"}
-
Before EVERYTHING...please authenticate yourself bu executing POST /token. For now key in usrname and password as you like.
-
If you have run pytest in step 4, when you test POST/data endpoint, please make sure the data you are uploading here has no overlap of the data in the ohlv.csv or any in create_tables.populate_table(). Otherwise it would be inserting the date twice, violating primary key.
-
verify the result by querying GET /data
-
Test GET with filer, pagination endpoints or sort: run tests on postman OR browser
- step authenticate client key in usename and password. For now it is not enforced, as long as you key in something, it will work
- Try out the endpoints you like
Stop and remove the containers
$ docker compose down
sort: order by aggregation : group by