From bdb1d78fbf891c8374b22d913167947af2690d8c Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Mon, 25 Mar 2024 17:15:45 +0100 Subject: [PATCH] Add README and one compose --- README.md | 76 ++++++++++++++++++++ api/README.md | 13 ++++ build.sh | 21 ++++++ api/docker-compose.yml => docker-compose.yml | 7 ++ ui/Dockerfile | 19 +++++ ui/README.md | 9 +++ 6 files changed, 145 insertions(+) create mode 100644 README.md create mode 100644 api/README.md create mode 100755 build.sh rename api/docker-compose.yml => docker-compose.yml (78%) create mode 100644 ui/Dockerfile create mode 100644 ui/README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..99b0ff4 --- /dev/null +++ b/README.md @@ -0,0 +1,76 @@ +# ACME Sky - Bank Service + +This repo refers to the Bank service used by ACME Sky. + +## `./api` folder + +It exposes a REST API with three endpoints: + +- `POST /payments/` used to create new payments. An example is: + +``` +curl -X POST http://localhost:8080/payments/ -H 'content-type: application/json' -H 'accept: application/json, */*;q=0.5' -d '{"owner":"John Doe","amount":30.4,"description":"Flight to CTA"}' +``` + +- `GET /payments//` used to get info about a payment. An example is: + +``` +curl http://localhost:8080/payments/20e8f98d-f67e-4e40-9ddb-77b786e61560/ + +HTTP/1.1 200 OK +Access-Control-Allow-Headers: Content-Type +Access-Control-Allow-Methods: OPTIONS, GET, HEAD, POST +Access-Control-Allow-Origin: * +Access-Control-Max-Age: 86400 +Allow: OPTIONS, GET, HEAD, POST +Content-Length: 162 +Content-Type: application/json + +{ + "id": "20e8f98d-f67e-4e40-9ddb-77b786e61560", + "owner": "John Doe", + "amount": 30.4, + "description": "Flight to BLQ", + "paid": true, + "created_at": "2024-03-25 15:34:45.093465" +} +``` + +- `POST /payments//pay/` used to change `paid` status. It is a fake: + card information are ignored. Payload can be empty. + +> [!NOTE] +> Do not forget to create a PostgreSQL database and link it to the API through +> `DATABASE_URL` environment variable. + +## `./ui` folder + +A simple frontend built in Vue+Vite. You must define `VITE_BACKEND_URL` which +points to the API. + +## Build + +> [!TIP] +> You can use `./build.sh` for a step-by-step guide for deploying. + + +You need to set up +``` +POSTGRES_USER=user +POSTGRES_PASSWORD=pass +POSTGRES_DB=db +DATABASE_URL=postgres://user:pass@bankservice-postgres:5432/db +``` + +and build + +``` +docker build -t acmesky-bankservice-api api +docker build -t acmesky-bankservice-ui --build-arg VITE_BACKEND_URL=http://localhost:8080 ui +``` + +after that you can put everything up + +``` +docker compose up +``` diff --git a/api/README.md b/api/README.md new file mode 100644 index 0000000..4891764 --- /dev/null +++ b/api/README.md @@ -0,0 +1,13 @@ +# API + +Steps for development + +1. Install Linux deps `ocaml libev-dev libpq-dev pkg-config` + +2. Install project deps `opam install --deps-only .` + +3. Build `opam exec -- dune build` + +4. Set up `DATABASE_URL` variable. It should be a PostgreSQL uri. + +5. Run `dune run acmebank` diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..b4fc3b6 --- /dev/null +++ b/build.sh @@ -0,0 +1,21 @@ +read -p "POSTGRES_USER [acme]: " pg_user +pg_user=${pg_user:-'acme'} + +read -p "POSTGRES_PASSWORD [pass]: " pg_pass +pg_pass=${pg_pass:-'pass'} + +read -p "POSTGRES_DB [db]: " pg_db +pg_db=${pg_db:-'db'} + +read -p "VITE_BACKEND_URL [http://localhost:8080]: " bank_api +bank_api=${bank_api:-'http://localhost:8080'} + +export POSTGRES_USER="$pg_user" +export POSTGRES_PASSWORD="$pg_pass" +export POSTGRES_DB="$pg_db" +export DATABASE_URL="postgres://$pg_user:$pg_pass@bankservice-postgres:5432/$pg_db" + +docker build -t acmesky-bankservice-api api +docker build -t acmesky-bankservice-ui --build-arg VITE_BACKEND_URL="$bank_api" ui + +docker compose up diff --git a/api/docker-compose.yml b/docker-compose.yml similarity index 78% rename from api/docker-compose.yml rename to docker-compose.yml index 629a27c..9941ef3 100644 --- a/api/docker-compose.yml +++ b/docker-compose.yml @@ -10,6 +10,7 @@ services: - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} volumes: - postgres-data:/var/lib/postgresql/data + - ./api/schema:/docker-entrypoint-initdb.d networks: - default @@ -24,5 +25,11 @@ services: - "8080:8080" restart: unless-stopped + bankservice-ui: + image: acmesky-bankservice-ui + container_name: bankservice-ui + ports: + - "8081:80" + volumes: postgres-data: diff --git a/ui/Dockerfile b/ui/Dockerfile new file mode 100644 index 0000000..22c0bd3 --- /dev/null +++ b/ui/Dockerfile @@ -0,0 +1,19 @@ +# build stage +FROM node:16-alpine as build-stage +WORKDIR /app +COPY package*.json ./ + +ARG VITE_APP_BACKEND_URL + +RUN npm install +RUN npx browserslist@latest --update-db +COPY . . +RUN npm run build + +# production stage +FROM nginx:stable-alpine as production-stage +COPY ./nginx.conf /temp/prod.conf +RUN envsubst /app < /temp/prod.conf > /etc/nginx/conf.d/default.conf +COPY --from=build-stage /app/dist/ /usr/share/nginx/html +EXPOSE 80 +CMD ["nginx", "-g", "daemon off;"] diff --git a/ui/README.md b/ui/README.md new file mode 100644 index 0000000..6c40f46 --- /dev/null +++ b/ui/README.md @@ -0,0 +1,9 @@ +# UI + +Steps for development + +1. Install deps `npm i` + +2. Set up `VITE_BACKEND_URL` + +3. Run `npm run dev`