A simple 'hello world' app of FastAPI running on NGINX Unit App server using ASGI.
docker-compose up --build
After that you can test using curl
command.
curl http://localhost
# Run the docker server if you haven't started it yet then run below command for executing test cases
docker-compose exec api pytest /fastapi
- Create venv in your project directory and install fastapi
$ python3 -m venv apienv
$ source apienv/bin/activate
(venv)$ pip install --upgrade pip
(venv)$ pip install -r requirements.txt
$ deactivate
- Run the following command for each directory containing app code or data so Unit can access it:
(venv)$ sudo chown -R unit:unit /path/to/app/
- Create a JSON configuration file containing information about application.
// config.json
{
"listeners": {
"*:80": {
"pass": "applications/fastapi"
}
},
"applications": {
"fastapi": {
"type": "python 3.x",
"path": "/path/to/app/",
"home": "/path/to/app/venv/",
"module": "asgi",
"callable": "app"
}
}
}
- Run the server
(venv)$ unitd
- Push the configurations json file on server
(venv)$ sudo curl -X PUT --data-binary @config.json --unix-socket \
/path/to/control.unit.sock http://localhost/config/
- Testing (terminal)
(venv)$ pytest .