diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +node_modules diff --git a/.env.example b/.env.example index 2acdb1c..a5f2f67 100644 --- a/.env.example +++ b/.env.example @@ -1,4 +1,6 @@ NEWS_API_KEY= THE_MOVIE_DB_API_READ_ACCESS_TOKEN= -PORT= -STEP_ZEN_KEY= +STEP_ZEN_KEY= # only needed if you want to generate documentation from the graphql operations +NODEJS_SERVER_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 diff --git a/.husky/pre-commit b/.husky/pre-commit index 7916f17..7f632b7 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -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 . diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ccd4001 --- /dev/null +++ b/Dockerfile @@ -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"] diff --git a/README.md b/README.md index 3f9af6e..2071c3c 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: > I'll be using npm as package-manager to run the tasks described below, but you can use yarn or pnpm. diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..d026e6f --- /dev/null +++ b/docker-compose.yaml @@ -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} diff --git a/src/app.ts b/src/app.ts index b1d1eef..27c22db 100644 --- a/src/app.ts +++ b/src/app.ts @@ -16,10 +16,10 @@ const server = new ApolloServer({ }); (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()), @@ -27,7 +27,7 @@ const server = new ApolloServer({ 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}!`); diff --git a/src/utils/cache/redis/redis-cache-handler.ts b/src/utils/cache/redis/redis-cache-handler.ts index 9d49060..37a38ea 100644 --- a/src/utils/cache/redis/redis-cache-handler.ts +++ b/src/utils/cache/redis/redis-cache-handler.ts @@ -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(key: string) {