Skip to content

novasush/fastapi-nginx-unit

Repository files navigation

fastapi-nginx-unit

A simple 'hello world' app of FastAPI running on NGINX Unit App server using ASGI.

Running on Docker

docker-compose up --build

After that you can test using curl command.

curl http://localhost

Running test cases (docker)

# 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

Running from terminal

  1. 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
  1. 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/
  1. 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"
        }
    }
}
  1. Run the server
(venv)$ unitd
  1. 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/
  1. Testing (terminal)
(venv)$ pytest .

Releases

No releases published

Packages

No packages published