diff --git a/.env.sample b/.env.sample deleted file mode 100644 index 0ad1f8c..0000000 --- a/.env.sample +++ /dev/null @@ -1,5 +0,0 @@ -APP_NAME= -BROKER_URI= -FIREBASE_APP_NAME= -DATABASE_URL= -STORAGE_BUCKET= \ No newline at end of file diff --git a/README.md b/README.md index 0f03983..fb3ae01 100644 --- a/README.md +++ b/README.md @@ -18,36 +18,60 @@ FastAPI Server for Super Resolution Model. -## Installation +## How to start +### Using docker-compose(recommended) +1. Clone repository +```shell +git clone https://github.com/ainize-team/SR-FastAPI +cd SR-FastAPI +``` -1. Build FastAPI Docker Image +2. Edit [docker-compose.yml](./docker-compose.yml), [rabbitmq.env](./envs/rabbitmq.env.sample) and [fastapi.env](./envs/fastapi.env.sample) for your project. +3. Run containers +```shell +docker-compose up -d +``` + +4. (Optional) config rabbimq user setting +```shell +docker exec sr-rabbitmq -it /bin/bash +cd scripts +./init_rabbitmq +``` + +### Using docker +1. Clone repository ```shell git clone https://github.com/ainize-team/SR-FastAPI cd SR-FastAPI +``` + +2. Build docker image +```shell docker build -t sr-fastapi . ``` -2. Run Docker Image +3. Create docker container ```shell docker run -d --name -p 8000:8000 \ -e APP_NAME= \ -e BROKER_URI= \ -e FIREBASE_APP_NAME= \ --e DATABASE_URL= \ --e STORAGE_BUCKET= \ +-e FIREBASE_DATABASE_URL= \ +-e FIREBASE_STORAGE_BUCKET= \ -v :/app/key \ sr-fastapi ``` -Or, you can use the .env file to run as follows. +Or, you can use the [.env file](./envs/fastapi.env.sample) to run as follows. ```shell -docker run -d --name -p 8000:8000 \ ---env-file \ --v :/app/key \ -sr-fastapi +docker build -t sr-fastapi . +docker run -d --name sr-fastapi -p 8000:8000 \ + --env-file .env \ + -v :/app/key sr-fastapi ``` ## For Developers diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..2277c36 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,37 @@ +version: '3' + +services: + rabbitmq: + image: rabbitmq:3.11.2-management + container_name: sr-rabbitmq + ports: + - 5672:5672 + - 15672:15672 + env_file: + - ./envs/rabbitmq.env + volumes: + - ./scripts/init_rabbitmq.sh:/scripts/init_rabbitmq.sh + networks: + - dev + restart: unless-stopped + + api-server: + build: + context: . + dockerfile: Dockerfile + container_name: sr-fastapi + ports: + - 8000:8000 + env_file: + - ./envs/fastapi.env + volumes: + - :/app/key/ + networks: + - dev + restart: unless-stopped + depends_on: + - rabbitmq + +networks: + dev: + driver: bridge diff --git a/envs/fastapi.env.sample b/envs/fastapi.env.sample new file mode 100644 index 0000000..dffc903 --- /dev/null +++ b/envs/fastapi.env.sample @@ -0,0 +1,5 @@ +APP_NAME="Super Resolution FastAPI Dev Server" +BROKER_URI=amqp://:@:5672/ +FIREBASE_DATABASE_URL= +FIREBASE_STORAGE_BUCKET= +FIREBASE_APP_NAME=super-resolution diff --git a/envs/rabbitmq.env.sample b/envs/rabbitmq.env.sample new file mode 100644 index 0000000..5c5ef20 --- /dev/null +++ b/envs/rabbitmq.env.sample @@ -0,0 +1,3 @@ +RABBITMQ_DEFAULT_USER= +RABBITMQ_DEFAULT_PASS= +RABBITMQ_VHOSTS=vhost1,vhost2 \ No newline at end of file diff --git a/scripts/init_rabbitmq.sh b/scripts/init_rabbitmq.sh new file mode 100644 index 0000000..6c2980d --- /dev/null +++ b/scripts/init_rabbitmq.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +echo "Init Rabbitmq config..." + +USER=$RABBITMQ_DEFAULT_USER +VHOSTS=$RABBITMQ_VHOSTS + +# Split the vhosts into an array using a comma (,) as the delimiter. +IFS=',' read -ra VHOST_ARRAY <<< "$VHOSTS" + +echo ${VHOST_ARRAY} + +for vhost in "${VHOST_ARRAY[@]}" +do + rabbitmqctl add_vhost $vhost + rabbitmqctl set_permissions -p $vhost $USER ".*" ".*" ".*" +done diff --git a/src/event_handlers.py b/src/event_handlers.py index e0dd3ba..b2fbaf5 100644 --- a/src/event_handlers.py +++ b/src/event_handlers.py @@ -13,7 +13,7 @@ def _setup_firebase() -> None: cred = credentials.Certificate(firebase_settings.cred_path) firebase_admin.initialize_app( cred, - {"databaseURL": firebase_settings.database_url, "storageBucket": firebase_settings.storage_bucket}, + {"databaseURL": firebase_settings.firebase_database_url, "storageBucket": firebase_settings.firebase_storage_bucket}, ) diff --git a/src/settings.py b/src/settings.py index b1344ab..68be5ea 100644 --- a/src/settings.py +++ b/src/settings.py @@ -16,8 +16,8 @@ class CelerySettings(BaseSettings): class FirebaseSettings(BaseSettings): firebase_app_name: str = "super-resolution" cred_path: str = "./key/serviceAccountKey.json" - database_url: str - storage_bucket: str + firebase_database_url: str + firebase_storage_bucket: str server_settings = ServerSettings()