1
0
mirror of https://github.com/sasjs/server.git synced 2025-12-11 03:34:35 +00:00

chore: docker configured for development api+web+mongodb

This commit is contained in:
Saad Jutt
2021-11-14 09:03:38 +05:00
parent d5024012c4
commit 4792f15c40
9 changed files with 62 additions and 27 deletions

View File

@@ -1,10 +0,0 @@
FROM node:lts-alpine
ENV NODE_ENV=production
WORKDIR /usr/src/app
COPY ["package.json", "package-lock.json*", "npm-shrinkwrap.json*", "./"]
RUN npm install --silent && mv node_modules ../
COPY . .
EXPOSE 5000
RUN chown -R node /usr/src/app
USER node
CMD ["npm", "start"]

6
api/.dockerignore Normal file
View File

@@ -0,0 +1,6 @@
build
coverage
node_modules
public
web
Dockerfile

View File

@@ -1,4 +1,5 @@
MODE=[server] MODE=[server]
CORS=[enable]
ACCESS_TOKEN_SECRET=<secret> ACCESS_TOKEN_SECRET=<secret>
REFRESH_TOKEN_SECRET=<secret> REFRESH_TOKEN_SECRET=<secret>
AUTH_CODE_SECRET=<secret> AUTH_CODE_SECRET=<secret>

9
api/Dockerfile Normal file
View File

@@ -0,0 +1,9 @@
FROM node:lts-alpine
WORKDIR /usr/server/api
COPY ["package.json","package-lock.json", "./"]
RUN npm ci
COPY . .
EXPOSE 5000
# RUN chown -R node /usr/server/api
# USER node
CMD ["npm","start"]

View File

@@ -13,8 +13,8 @@ dotenv.config()
const app = express() const app = express()
const { MODE } = process.env const { MODE, CORS } = process.env
if (MODE?.trim() !== 'server') { if (MODE?.trim() !== 'server' || CORS?.trim() === 'enable') {
console.log('All CORS Requests are enabled') console.log('All CORS Requests are enabled')
app.use(cors({ credentials: true, origin: 'http://localhost:3000' })) app.use(cors({ credentials: true, origin: 'http://localhost:3000' }))
} }
@@ -27,6 +27,10 @@ app.use('/', webRouter)
app.use('/SASjsApi', apiRouter) app.use('/SASjsApi', apiRouter)
app.use(express.json({ limit: '50mb' })) app.use(express.json({ limit: '50mb' }))
app.use(express.static(getWebBuildFolderPath())) try {
app.use(express.static(getWebBuildFolderPath()))
} catch (err) {
console.error('Unable to get web build')
}
export default connectDB().then(() => app) export default connectDB().then(() => app)

View File

@@ -1,22 +1,29 @@
import { readFile } from '@sasjs/utils' import { fileExists, readFile } from '@sasjs/utils'
import express from 'express' import express from 'express'
import path from 'path' import path from 'path'
import { getWebBuildFolderPath } from '../../utils' import { getWebBuildFolderPath } from '../../utils'
const webRouter = express.Router() const webRouter = express.Router()
const codeToInject = `
<script>
localStorage.setItem('accessToken', JSON.stringify('accessToken'))
localStorage.setItem('refreshToken', JSON.stringify('refreshToken'))
</script>`
webRouter.get('/', async (_, res) => { webRouter.get('/', async (_, res) => {
const indexHtmlPath = path.join(getWebBuildFolderPath(), 'index.html') let indexHtmlPath: string
try {
indexHtmlPath = path.join(getWebBuildFolderPath(), 'index.html')
} catch (err) {
return res.send('Web Build is not present')
}
const { MODE } = process.env const { MODE } = process.env
if (MODE?.trim() !== 'server') { if (MODE?.trim() !== 'server') {
const content = await readFile(indexHtmlPath) const content = await readFile(indexHtmlPath)
const codeToInject = `
<script>
localStorage.setItem('accessToken', JSON.stringify('accessToken'))
localStorage.setItem('refreshToken', JSON.stringify('refreshToken'))
</script>`
const injectedContent = content.replace('</head>', `${codeToInject}</head>`) const injectedContent = content.replace('</head>', `${codeToInject}</head>`)
res.setHeader('Content-Type', 'text/html') res.setHeader('Content-Type', 'text/html')

View File

@@ -1,20 +1,26 @@
version: '3.4' version: '3.4'
services: services:
server: sasjs_server_api:
image: server image: sasjs_server_api
build: build: ./api
context: .
dockerfile: ./Dockerfile
environment: environment:
NODE_ENV: production
DB_CONNECT: mongodb://mongodb:27017/sasjs DB_CONNECT: mongodb://mongodb:27017/sasjs
ports: ports:
- 5000:5000 - 5000:5000
volumes: volumes:
- .:/usr/src/app - ./api:/usr/server/api
links: links:
- mongodb - mongodb
sasjs_server_web:
image: sasjs_server_web
build: ./web
ports:
- 3000:3000
volumes:
- ./web:/usr/server/web
mongodb: mongodb:
image: mongo:latest image: mongo:latest
ports: ports:

3
web/.dockerignore Normal file
View File

@@ -0,0 +1,3 @@
build
node_modules
Dockerfile

9
web/Dockerfile Normal file
View File

@@ -0,0 +1,9 @@
FROM node:lts-alpine
WORKDIR /usr/server/web
COPY ["package.json","package-lock.json", "./"]
RUN npm ci
COPY . .
EXPOSE 3000
# RUN chown -R node /usr/server/api
# USER node
CMD ["npm","start"]