Add config.js, dockerfile

This commit is contained in:
wancat committed 2023-07-21 11:27:01 +08:00
1 parent 4ffdb5d627
commit 2dffd2f74d
5 files changed
+48 -4

No files matched your search

+19
View File
@@ -0,0 +1,19 @@
FROM node
WORKDIR /app
COPY package.json yarn.lock ./
COPY server/package.json ./server/
RUN yarn install --frozen-lockfile --production
WORKDIR /app/server
COPY server/src ./src
COPY server/quests ./quests
COPY server/swagger.yaml .
ENV NODE_ENV=production
ENV HOST=0.0.0.0
ENV PORT=3000
EXPOSE 3000
CMD [ "yarn", "start" ]
+11
View File
@@ -1,5 +1,16 @@
version: '3'
services:
app:
image: lancatlin/linux-odyssey-server
build: .
ports:
- '3000:3000'
links:
- db
volumes:
- /var/run/docker.sock:/var/run/docker.sock
environment:
- MONGO_URL=mongodb://db:27017/odyssey-test
db:
image: mongo
restart: always
+13
View File
@@ -0,0 +1,13 @@
function get(key, defaultValue) {
const value = process.env[key]
if (value != null) {
return value
}
return defaultValue
}
export default {
host: get('HOST', 'localhost'),
port: get('PORT', 3000),
db: get('MONGO_URL', 'mongodb://localhost:27017/odyssey-test'),
}
+2 -1
View File
@@ -1,8 +1,9 @@
import mongoose from 'mongoose'
import config from './config.js'
export default async function connectDB() {
try {
await mongoose.connect('mongodb://localhost:27017/odyssey-test')
await mongoose.connect(config.db)
console.log('Connected to MongoDB')
} catch (err) {
console.log(err)
+3 -3
View File
@@ -8,9 +8,9 @@ import socketServer from './api/socket.js'
import connectDB from './db.js'
import apiRouter from './api/routes/index.js'
import loadAndUpdateQuests from './utils/quest.js'
import config from './config.js'
async function main() {
const port = 3000
const app = express()
const server = http.createServer(app)
socketServer(server)
@@ -28,8 +28,8 @@ async function main() {
app.use('/api/v1', apiRouter)
server.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`)
server.listen(config.port, config.host, () => {
console.log(`Server listening at http://${config.host}:${config.port}`)
})
}