Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/setup docker compose #104

Merged
merged 4 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
6 changes: 4 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
NEWS_API_KEY=<YOUR_NEWS_API_KEY>
THE_MOVIE_DB_API_READ_ACCESS_TOKEN=<YOUR_THE_MOVIE_DB_API_READ_ACCESS_TOKEN>
PORT=<PORT>
STEP_ZEN_KEY=<YOUR_STEP_ZEN_KEY>
STEP_ZEN_KEY=<YOUR_STEP_ZEN_KEY> # only needed if you want to generate documentation from the graphql operations
NODEJS_SERVER_PORT=<NODEJS_SERVER_PORT>
REDIS_HOST_PORT=<REDIS_HOST_PORT>
REDIS_HOST_NAME=cache # this value must have the same name of the "redis" service defined on docker-compose - you can change it as long as both values are the same
4 changes: 0 additions & 4 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,3 @@ npm run prettier:fix
npm run lint:fix
npm run build
npm run test
# rm -rf docs
# mkdir docs
# npx spectaql spectaql-config.yaml -t ./docs
# git add -A .
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM node:18-alpine3.18

WORKDIR /usr/app

COPY package.json ./package.json
COPY package-lock.json ./package-lock.json

RUN npm install

COPY . /usr/app

CMD ["npm", "run", "start:dev"]
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ _Example of request flow - user requesting details about a Movie_

You can check the documentation with the possible queries [here](https://steniowagner.github.io/cine-tasty-server/).

This documentation is created using [github-pages](https://pages.github.com/).

Also, you can check how to generate it [here](https://github.com/steniowagner/cine-tasty-server/blob/development/README.md#generating-documentation).

## Getting Started
Expand Down Expand Up @@ -85,17 +83,19 @@ The `STEP_ZEN_KEY` is used by [spectaql](https://github.com/anvilco/spectaql), t

The [.env.example](https://github.com/steniowagner/cine-tasty-server/blob/development/.env.example) file shows all the enviroment variables that you'll need to set in order to run the app (except for the `STEP_ZEN_KEY`).

To setup the environment variables, create a new `.env` file and add the mandatory variables (`NEWS_API_KEY` and `THE_MOVIE_DB_API_READ_ACCESS_TOKEN`) with their respectives values.
To setup the environment variables, create a new `.env` file and add their respectives values.

### Running

To run the app, you just need start the containers using `docker compose`
At this point, you should already have your environment variables declared in a .env file.

To run the app, you just need start the containers using `docker compose`.

```
$ docker-compose up -d
$ docker-compose --env-file .env up --build
```

You'll find the app runing at your [localhost:3000](http://localhost:3000/).
You'll find the app runing at your localhost:<NODEJS_SERVER_PORT>

> I'll be using npm as package-manager to run the tasks described below, but you can use yarn or pnpm.

Expand Down
39 changes: 39 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
version: "3"

networks:
cine-tasty-net:
driver: bridge

volumes:
node_modules:

services:
cache:
container_name: cine-tasty-cache
image: redis:7.2.4-alpine
restart: unless-stopped
ports:
- "${REDIS_HOST_PORT}:6379"
networks:
- cine-tasty-net

server:
container_name: cine-tasty-server
build: .
depends_on:
- cache
restart: unless-stopped
ports:
- "${NODEJS_SERVER_PORT}:3000"
networks:
- cine-tasty-net
volumes:
- .:/usr/app
- node_modules:/usr/app/node_modules
environment:
- NEWS_API_KEY=${NEWS_API_KEY}
- THE_MOVIE_DB_API_READ_ACCESS_TOKEN=${THE_MOVIE_DB_API_READ_ACCESS_TOKEN}
- NODEJS_SERVER_PORT=${NODEJS_SERVER_PORT}
- STEP_ZEN_KEY=${STEP_ZEN_KEY}
- REDIS_HOST_NAME=${REDIS_HOST_NAME}
- REDIS_HOST_PORT=${REDIS_HOST_PORT}
6 changes: 3 additions & 3 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
});

(async () => {
const cacheHandler = new RedisCacheHandler();
await cacheHandler.init();
const { url } = await startStandaloneServer(server, {
async context() {
const cacheHandler = new RedisCacheHandler();
await cacheHandler.init();
return {
openTriviaAPI: new OpenTriviaAPI(),
newsAPI: new NewsAPI(new Date()),
tmdbAPI: new TMDBApi(),
cacheHandler,
};
},
listen: { port: parseInt(process.env.PORT! as string) },
listen: { port: parseInt(process.env.NODEJS_SERVER_PORT! as string) },
});
if (process.env.NODE_ENV !== "production") {
console.log(`UHUL! Cine-Tasty-API is running at ${url}!`);

Check warning on line 33 in src/app.ts

View workflow job for this annotation

GitHub Actions / Code Check

Unexpected console statement
}
})();
7 changes: 6 additions & 1 deletion src/utils/cache/redis/redis-cache-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ export default class RedisCacheHandler implements CacheHandler {
private client: Client;

constructor() {
this.client = createClient();
this.client = createClient({
socket: {
port: parseInt(process.env.REDIS_HOST_PORT! as string),
host: process.env.REDIS_HOST_NAME! as string,
},
});
}

async get<TData>(key: string) {
Expand Down
Loading