diff --git a/.gitignore b/.gitignore index f5ee73d..35151f6 100644 --- a/.gitignore +++ b/.gitignore @@ -123,4 +123,4 @@ yarn-debug.log* yarn-error.log* # Redis -worker/data +data diff --git a/api/.dockerignore b/api/.dockerignore new file mode 100644 index 0000000..643ce4b --- /dev/null +++ b/api/.dockerignore @@ -0,0 +1,6 @@ +.git +*Dockerfile* +*docker-compose* +node_modules +npm-debug.log +logs/* diff --git a/api/Dockerfile b/api/Dockerfile new file mode 100644 index 0000000..fa67d56 --- /dev/null +++ b/api/Dockerfile @@ -0,0 +1,11 @@ +FROM node:alpine + +WORKDIR /usr/src/app + +COPY package.json yarn.lock ./ +RUN yarn install + +COPY . . + +EXPOSE 5000 +CMD ["yarn", "start"] diff --git a/api/index.js b/api/index.js index fdf3bc2..7f3db6d 100644 --- a/api/index.js +++ b/api/index.js @@ -1,6 +1,9 @@ const express = require('express'); const redis = require('redis'), - client = redis.createClient(); + client = redis.createClient( + 6379, + process.env.NODE_ENV === 'docker' ? 'redis' : 'localhost' + ); const {promisify} = require('util'); const getAsync = promisify(client.get).bind(client); diff --git a/api/package.json b/api/package.json index 040749c..7593301 100644 --- a/api/package.json +++ b/api/package.json @@ -12,4 +12,4 @@ "express": "4.17.1", "redis": "2.8.0" } -} +} \ No newline at end of file diff --git a/client/.dockerignore b/client/.dockerignore new file mode 100644 index 0000000..643ce4b --- /dev/null +++ b/client/.dockerignore @@ -0,0 +1,6 @@ +.git +*Dockerfile* +*docker-compose* +node_modules +npm-debug.log +logs/* diff --git a/client/Dockerfile b/client/Dockerfile new file mode 100644 index 0000000..8e21ff5 --- /dev/null +++ b/client/Dockerfile @@ -0,0 +1,14 @@ +FROM node:alpine AS builder + +WORKDIR /app + +COPY package.json yarn.lock ./ +RUN yarn install + +COPY . . +RUN npm run build + +FROM nginx:1.15.10-alpine +COPY --from=builder /app/build /var/www +COPY ./nginx.conf /etc/nginx/nginx.conf +CMD ["nginx", "-g", "daemon off;"] diff --git a/client/nginx.conf b/client/nginx.conf new file mode 100644 index 0000000..56b605f --- /dev/null +++ b/client/nginx.conf @@ -0,0 +1,24 @@ +events { + worker_connections 4096; ## Default: 1024 +} +http { + server { + listen 80; + server_name localhost; + include /etc/nginx/mime.types; + # root /var/www; + # index index.html index.htm; + location /api { + resolver 127.0.0.11; + proxy_set_header X-Forwarded-Host $host; + proxy_set_header X-Forwarded-Server $host; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_pass http://api:5000$request_uri; + } + location / { + root /usr/share/nginx/html; + index index.html index.htm; + try_files $uri $uri/ =404; + } +} +} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..789583c --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,50 @@ +version: '3.7' +services: + client: + image: ajreddevil/client-job-board + restart: always + container_name: client + ports: + - '80:80' + links: + - api + depends_on: + - api + networks: + - webappnetwork + api: + image: ajreddevil/api-job-board + restart: always + container_name: api + ports: + - '5000:5000' + environment: + NODE_ENV: 'docker' + depends_on: + - redis + networks: + - webappnetwork + worker: + image: ajreddevil/worker-job-board + restart: always + container_name: worker + environment: + NODE_ENV: 'docker' + depends_on: + - redis + networks: + - webappnetwork + redis: + image: redis + restart: always + container_name: redis + volumes: + - ${PWD}/data:/data + ports: + - 6379:6379 + command: redis-server --appendonly yes + networks: + - webappnetwork +networks: + webappnetwork: + driver: bridge diff --git a/makefile b/makefile new file mode 100644 index 0000000..fb61fb9 --- /dev/null +++ b/makefile @@ -0,0 +1,78 @@ +# API +dockerize-api: + docker build -t ajreddevil/api-job-board api/ + +start-api: + docker run --name api \ + -p 5000:5000 \ + --restart unless-stopped \ + --link redis:redis \ + -e NODE_ENV=docker \ + -d ajreddevil/api-job-board + +stop-api: + docker stop api + docker rm api + +view-api-logs: + docker logs -f api + +# Client +dockerize-client: + docker build -t ajreddevil/client-job-board client/ + +start-client: + docker run --name client \ + -p 3000:3000 \ + --restart unless-stopped \ + -d ajreddevil/client-job-board + +stop-client: + docker stop client + docker rm client + +view-client-logs: + docker logs -f client + +# worker +dockerize-worker: + docker build -t ajreddevil/worker-job-board worker/ + +start-worker: + docker run --name worker \ + --restart unless-stopped \ + --link redis:redis \ + -e NODE_ENV=docker \ + -d ajreddevil/worker-job-board + +stop-worker: + docker stop worker + docker rm worker + +view-worker-logs: + docker logs -f worker + +# Redis +download-redis: + docker pull redis + +setup-volume: + mkdir data + +start-redis: + docker run --name redis \ + --restart unless-stopped \ + -v ${PWD}/data:/data\ + -p 6379:6379 \ + -d redis redis-server --appendonly yes + +stop-redis: + docker stop redis + docker rm redis + +# RUN cmd +run-job-board: + docker-compose -p job-board up -d + +stop-job-board: + docker-compose -p job-board down \ No newline at end of file diff --git a/worker/.dockerignore b/worker/.dockerignore new file mode 100644 index 0000000..643ce4b --- /dev/null +++ b/worker/.dockerignore @@ -0,0 +1,6 @@ +.git +*Dockerfile* +*docker-compose* +node_modules +npm-debug.log +logs/* diff --git a/worker/Dockerfile b/worker/Dockerfile new file mode 100644 index 0000000..d51d8bd --- /dev/null +++ b/worker/Dockerfile @@ -0,0 +1,10 @@ +FROM node:alpine + +WORKDIR /usr/src/app + +COPY package.json yarn.lock ./ +RUN yarn install + +COPY . . + +CMD ["yarn", "start"] diff --git a/worker/makefile b/worker/makefile deleted file mode 100644 index 2e713bd..0000000 --- a/worker/makefile +++ /dev/null @@ -1,16 +0,0 @@ -download-redis: - docker pull redis - -setup-volume: - mkdir data - -start-redis: - docker run --name redis-job-board \ - -p 6379:6379 \ - --restart unless-stopped \ - -v ${PWD}/data:/data\ - -d redis redis-server --appendonly yes - -stop-redis: - docker stop redis-job-board - docker rm redis-job-board \ No newline at end of file diff --git a/worker/package.json b/worker/package.json index 252a273..1d4d7c4 100644 --- a/worker/package.json +++ b/worker/package.json @@ -5,9 +5,12 @@ "main": "index.js", "author": "Ajan Lal Shrestha", "license": "MIT", + "scripts": { + "start": "node index.js" + }, "dependencies": { "cron": "1.7.2", "node-fetch": "2.6.0", "redis": "2.8.0" } -} +} \ No newline at end of file diff --git a/worker/tasks/fetch-github.js b/worker/tasks/fetch-github.js index 39a4b2e..9d0dbbf 100644 --- a/worker/tasks/fetch-github.js +++ b/worker/tasks/fetch-github.js @@ -1,6 +1,9 @@ const fetch = require('node-fetch'); const redis = require('redis'), - client = redis.createClient(); + client = redis.createClient( + 6379, + process.env.NODE_ENV === 'docker' ? 'redis' : 'localhost' + ); const {promisify} = require('util'); const setAsync = promisify(client.set).bind(client); @@ -21,6 +24,8 @@ const fetchGithub = async () => { onPage = 0; const allJobs = []; + console.log('fetching jobs from Github'); + while (resultCount > 0) { const res = await fetch(`${baseURL}?page=${onPage}`); const jobs = await res.json();