├── .gitignore ├── .gitmodules ├── README.md ├── config └── nginx │ └── local.conf ├── docker-compose.yml └── env.example /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | .vscode/ 3 | .idea/ 4 | .ropeproject/ 5 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "frontend"] 2 | path = frontend 3 | url = git@github.com:Pk13055/svelte-template.git 4 | [submodule "backend"] 5 | path = backend 6 | url = git@github.com:Pk13055/fastapi-template.git 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Async FastAPI-based application 2 | 3 | - NoSQL support (motor asyncio) 4 | - Pydantic-based models 5 | - Fast 6 | 7 | ## Installation 8 | 9 | Run using docker to checkout mongo support 10 | 11 | ``` 12 | cp -rv env.example .env # populate with your own values 13 | docker-compose up -d 14 | ``` 15 | 16 | ## Routes 17 | 18 | ### Static files 19 | 20 | The `nginx` service serves both static files from the front and backend. 21 | 22 | Add staticfiles for 23 | - frontend: to the `rollup` build process OR to 24 | `frontend/dist/` 25 | - backend: to `backend/static` 26 | 27 | ### API 28 | 29 | The `backend` service is to be accessed from the frontend using `/api` prefix. 30 | 31 | Thus any backend routes are now `/api/route-to-be-accessed`. Because of this, 32 | the API docs are hosted at `/api/docs`. 33 | 34 | ### DB Interface 35 | 36 | For easy debugging purposes, there is an express mongodb interface provided at 37 | `/db_interface/`. 38 | -------------------------------------------------------------------------------- /config/nginx/local.conf: -------------------------------------------------------------------------------- 1 | upstream backend_server { 2 | server backend:8080; 3 | } 4 | 5 | upstream frontend_server { 6 | server frontend:5000; 7 | } 8 | 9 | upstream db_interface { 10 | server db-interface:8081; 11 | } 12 | 13 | # now we declare our main server 14 | server { 15 | 16 | listen 80; 17 | server_name localhost; 18 | client_max_body_size 8M; 19 | 20 | location /static/ { 21 | proxy_pass http://backend_server/static/; 22 | } 23 | 24 | location /db_interface/ { 25 | proxy_pass http://db_interface; 26 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 27 | proxy_set_header Host $host; 28 | proxy_redirect off; 29 | } 30 | 31 | location /api/ { 32 | proxy_pass http://backend_server/; 33 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 34 | proxy_set_header Host $host; 35 | proxy_redirect off; 36 | } 37 | 38 | location / { 39 | proxy_pass http://frontend_server/; 40 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 41 | proxy_set_header Host $host; 42 | proxy_redirect off; 43 | } 44 | 45 | 46 | } 47 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "3.1" 2 | services: 3 | db: 4 | image: mongo 5 | restart: always 6 | networks: 7 | - db_network # for API reqs 8 | environment: 9 | MONGO_INITDB_ROOT_USERNAME: $DB_USERNAME 10 | MONGO_INITDB_ROOT_PASSWORD: $DB_PASSWORD 11 | MONGO_INITDB_DATABASE: $MONGO_INITDB_DATABASE 12 | volumes: 13 | - db_vol:/data/db 14 | 15 | db-interface: 16 | image: mongo-express 17 | restart: always 18 | environment: 19 | ME_CONFIG_MONGODB_ADMINUSERNAME: $DB_USERNAME 20 | ME_CONFIG_MONGODB_ADMINPASSWORD: $DB_PASSWORD 21 | ME_CONFIG_SITE_BASEURL: /db_interface/ 22 | ME_CONFIG_MONGODB_SERVER: db 23 | networks: 24 | - db_network # access db 25 | - web_network # access the interface 26 | depends_on: 27 | - db 28 | 29 | backend: 30 | build: ./backend 31 | restart: always 32 | volumes: 33 | - ./backend:/app 34 | environment: 35 | PORT: 8080 36 | DEBUG: $DEBUG 37 | SECRET_KEY: $SECRET_KEY 38 | DATABASE_URL: $DATABASE_URL 39 | depends_on: 40 | - db 41 | networks: 42 | - db_network # for data fetching/upload 43 | - web_network # for web API 44 | 45 | frontend: 46 | build: ./frontend 47 | entrypoint: ./entrypoint.sh 48 | restart: unless-stopped 49 | environment: 50 | NODE_ENV: development # OR [production] 51 | volumes: 52 | - web-root:/home/app/dist 53 | - ./frontend:/home/app/ 54 | - /home/app/node_modules 55 | environment: 56 | PORT: 5000 57 | DEBUG: $DEBUG 58 | depends_on: 59 | - backend 60 | networks: 61 | - web_network 62 | 63 | nginx: 64 | image: nginx:latest 65 | ports: 66 | - 8000:80 67 | volumes: 68 | - web-root:/var/www/html 69 | - ./config/nginx:/etc/nginx/conf.d/ 70 | depends_on: 71 | - backend 72 | - db-interface 73 | networks: 74 | - web_network 75 | 76 | networks: 77 | web_network: 78 | driver: bridge 79 | db_network: 80 | driver: bridge 81 | 82 | volumes: 83 | web-root: 84 | db_vol: 85 | -------------------------------------------------------------------------------- /env.example: -------------------------------------------------------------------------------- 1 | COMPOSE_PROJECT_NAME='fastapi-template' 2 | DB_USERNAME=root 3 | DB_PASSWORD=password 4 | MONGO_INITDB_DATABASE=core 5 | DATABASE_URL=mongodb://root:password@db:27017 6 | DEBUG=1 7 | SECRET_KEY="change-this-key" 8 | --------------------------------------------------------------------------------