Skip to content

Commit

Permalink
Remove serverless and dockerize api (#7)
Browse files Browse the repository at this point in the history
* Remove AWS dynamo DB

* Remove serverless, dockerize and fix API refactor bugs

* Minor README changes

* Docker comments

* Allow configuring server port via docker

* Fix symlinks

* Fix PR comments
  • Loading branch information
Siegrift authored Sep 19, 2023
1 parent 317f08d commit 0a13bb3
Show file tree
Hide file tree
Showing 20 changed files with 201 additions and 361 deletions.
18 changes: 18 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# NOTE: Keep in sync with .gitignore.
#
# From: https://shisho.dev/blog/posts/how-to-use-dockerignore/
#
# In .gitignore, the file or directory name is ignored in any hierarchy below the .gitignore file, but in .dockerignore,
# all paths must be relative to the way where .dockerignore is located. However, in .dockerignore, all paths must be
# listed relative to the path.
**/.build
**/.env
**/.idea
**/.log
**/.serverless
**/.tsbuildinfo
**/.vscode
**/build
**/dist
**/node_modules
**/coverage
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# NOTE: Keep in sync with .dockerignore
.build
.env
.idea
.log
.serverless
.tsbuildinfo
.vscode
.vscode
build
dist
docker
node_modules
coverage
4 changes: 2 additions & 2 deletions packages/api/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
HTTP_API_ID=
MAX_BATCH_SIZE=
MAX_BATCH_SIZE=10
PORT=8090
2 changes: 1 addition & 1 deletion packages/api/.eslintignore
2 changes: 1 addition & 1 deletion packages/api/.prettierignore
50 changes: 12 additions & 38 deletions packages/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,11 @@ A service for storing and accessing signed data. It provides endpoints to handle

## Configuration

1. Copy `.env` from the `example.env` file.
2. Open the `.env` file and update the environment variables:
- `HTTP_API_ID`: The ID of the HTTP API in AWS API Gateway.
Copy `.env` from the `example.env` file and optionally change the defaults.

## Deployment

To deploy infrastructure to AWS:

```bash
pnpm run deploy
```

To remove deployment:

```bash
pnpm run removeDeployment
```

## Public Endpoint

The API is publicly accessible at the following endpoint:

- https://pool.nodary.io
TODO: Write example how to deploy on AWS (and maybe other cloud providers as well).

## Usage

Expand All @@ -39,29 +21,27 @@ The API provides the following endpoints:

## Local development

Start local dynamodb server:
Spin up local `express` server:

```bash
pnpm run dynamodb-local-up
pnpm run dev
```

Initialize tables:

```bash
pnpm run init-tables
```
## Docker

Spin up local `express` server to mimic AWS API gateway:
The API is also dockerized. In order to run the API from a docker, run:

```bash
pnpm run start-local
pnpm run docker:start
# or in a detached mode
pnpm run docker:detach:start
# optionally specify port
PORT=5123 pnpm run docker:start
```

You can use following valid examples in the next section to test server.

### Examples

Here are some examples of how to use the API with `curl`:
Here are some examples of how to use the API with `curl`. Note, the port may differ based on the `.env` value.

```bash
# Upsert signed data (HTTP PUT)
Expand Down Expand Up @@ -102,10 +82,4 @@ curl --location 'http://localhost:8090/0xc52EeA00154B4fF1EbbF8Ba39FDe37F1AC3B9Fd
# List available airnode addresses (HTTP GET)
curl --location 'http://localhost:8090' \
--header 'Content-Type: application/json'

```

## References

- To configure Cloudflare for caching, AWS API Gateway for custom domain support see
[the page](https://kylebarron.dev/blog/caching-lambda-functions-cloudflare).
9 changes: 0 additions & 9 deletions packages/api/docker-compose.dynamodb-local.yml

This file was deleted.

30 changes: 30 additions & 0 deletions packages/api/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Step 1: Build the TypeScript application.
FROM node:18-alpine AS builder

WORKDIR /usr/src/app
# Assumes the context is the root of the monorepo. Copies all of the contents (without files listed in .dockerignore) of
# the monorepo into the image.
COPY . .
RUN npm install -g pnpm
# Installs all dependencies only for the api package.
RUN pnpm install --recursive --filter api
# Builds the api package.
RUN pnpm run --filter api tsc

# Step 2: Run the built application.
FROM node:18-alpine

WORKDIR /usr/src/app/packages/api
# Copies the built application from the builder image.
COPY --from=builder /usr/src/app/packages/api/dist ./dist
# Copies the package.json from the builder image.
COPY --from=builder /usr/src/app/packages/api/package.json .
# This Dockerfile copies the pnpm-lock.yaml file from the monorepo root to install locked dependency versions. This
# guarantees consistency by utilizing identical sets of dependencies.
COPY pnpm-lock.yaml .
RUN npm install -g pnpm
# Only install dependencies for production (ignore "devDependecies" section in package.json).
RUN pnpm install --prod

EXPOSE 3000
CMD [ "node", "dist/src/local-server.js" ]
12 changes: 12 additions & 0 deletions packages/api/docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.8'

services:
express-server:
build:
context: ../../../
dockerfile: ./packages/api/docker/Dockerfile
ports:
- '${PORT:-4000}:${PORT:-4000}'
environment:
- NODE_ENV=production
- PORT=${PORT:-4000}
14 changes: 5 additions & 9 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@
},
"main": "index.js",
"scripts": {
"deploy": "serverless deploy",
"removeDeployment": "serverless remove",
"dynamodb-local-up": "docker compose -f docker-compose.dynamodb-local.yml up -d ",
"dynamodb-local-down": "docker compose -f docker-compose.dynamodb-local.yml down ",
"init-tables": "LOCAL_DEV=true npx ts-node scripts/init-tables.ts",
"start-local": "LOCAL_DEV=true npx ts-node src/local-server.ts",
"dev": "pnpm ts-node src/local-server.ts",
"docker:build": "docker compose --file docker/docker-compose.yml build",
"docker:start": "docker compose --file docker/docker-compose.yml up",
"docker:start:detach": "docker compose --file docker/docker-compose.yml up --detach",
"docker:stop": "docker compose --file docker/docker-compose.yml down",
"prettier:check": "prettier --check \"./**/*.{js,ts,md,json}\"",
"prettier:fix": "prettier --write \"./**/*.{js,ts,md,json}\"",
"eslint:check": "eslint . --ext .js,.ts --max-warnings 0",
Expand All @@ -22,16 +21,13 @@
},
"license": "MIT",
"devDependencies": {
"@types/aws-lambda": "^8.10.115",
"@types/express": "^4.17.17",
"@types/lodash": "^4.14.194",
"serverless-plugin-typescript": "^2.1.4",
"serverless": "^3.30.1"
},
"dependencies": {
"@api3/promise-utils": "0.3.0",
"aws-lambda": "^1.0.7",
"aws-sdk": "^2.1370.0",
"dotenv": "^16.0.3",
"ethers": "^5.7.2",
"express": "^4.18.2",
Expand Down
44 changes: 0 additions & 44 deletions packages/api/scripts/init-tables.ts

This file was deleted.

91 changes: 0 additions & 91 deletions packages/api/serverless.yml

This file was deleted.

Loading

0 comments on commit 0a13bb3

Please sign in to comment.