feat: separate db models from server

This commit is contained in:
wancat committed 2023-10-14 15:34:46 +08:00
1 parent 784ae29ed0
commit 50bf514805
23 files changed
+90 -31

No files matched your search

+2
View File
@@ -5,11 +5,13 @@ COPY ./package.json yarn.lock ./
COPY server/package.json ./server/package.json
COPY packages/file-graph/package.json ./packages/file-graph/package.json
COPY packages/utils/package.json ./packages/utils/package.json
COPY packages/models/package.json ./packages/models/package.json
RUN yarn install --frozen-lockfile --production
COPY ./server ./server
COPY packages/file-graph ./packages/file-graph
COPY packages/utils ./packages/utils
COPY packages/models ./packages/models
COPY quests ./quests
WORKDIR /app/server
+1
View File
@@ -5,6 +5,7 @@ COPY package.json yarn.lock ./
COPY server/package.json ./server/package.json
COPY packages/file-graph/package.json ./packages/file-graph/package.json
COPY packages/utils/package.json ./packages/utils/package.json
COPY packages/models/package.json ./packages/models/package.json
RUN yarn install --frozen-lockfile
COPY server/nodemon.json ./server/nodemon.json
+1
View File
@@ -12,6 +12,7 @@
"dependencies": {
"@linux-odyssey/file-graph": "*",
"@linux-odyssey/utils": "*",
"@linux-odyssey/models": "*",
"bcrypt": "^5.1.1",
"commander": "^11.0.0",
"connect-mongo": "^5.0.0",
+1 -1
View File
@@ -1,5 +1,5 @@
import { isValidEmail, isValidUsername } from '@linux-odyssey/utils'
import User from '../../models/user.js'
import { User } from '@linux-odyssey/models'
import { genJWT, hashPassword } from '../../utils/auth.js'
export async function issueToken(req, res) {
@@ -1,6 +1,5 @@
/* eslint-disable import/prefer-default-export */
import Command from '../../models/command.js'
import Session from '../../models/session.js'
import { Command, Session } from '@linux-odyssey/models'
import CommandHandler from '../../game/commandHandler.js'
export async function newCommand(req, res) {
@@ -1,4 +1,4 @@
import Quest from '../../models/quest.js'
import { Quest } from '@linux-odyssey/models'
export async function getQuests(req, res) {
const quests = await Quest.find({}).sort({ order: 1 })
@@ -1,5 +1,4 @@
import Session from '../../models/session.js'
import Quest from '../../models/quest.js'
import { Session, Quest } from '@linux-odyssey/models'
import { createContainer } from '../../containers/docker.js'
import SessionHandler from '../../game/sessionHandler.js'
+1 -1
View File
@@ -1,6 +1,6 @@
import { Server } from 'socket.io'
import { Session } from '@linux-odyssey/models'
import { getAndStartContainer, attachContainer } from '../containers/docker.js'
import Session from '../models/session.js'
import SessionMiddleware from '../middleware/session.js'
import { genJWT } from '../utils/auth.js'
+1 -1
View File
@@ -1,6 +1,6 @@
import bcrypt from 'bcrypt'
import LocalStrategy from 'passport-local'
import User from '../models/user.js'
import { User } from '@linux-odyssey/models'
async function verifyPassword(username, password, done) {
try {
+1 -1
View File
@@ -1,6 +1,6 @@
import schedule from 'node-schedule'
import { Session } from '@linux-odyssey/models'
import config from '../config.js'
import Session from '../models/session.js'
import { deleteContainer } from './docker.js'
export async function removeExpired() {
-17
View File
@@ -1,17 +0,0 @@
import mongoose from 'mongoose'
import config from './config.js'
export default async function connectDB() {
try {
const db = await mongoose.connect(config.db, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
console.log('Connected to MongoDB')
return db.connection.getClient()
} catch (err) {
console.error(err)
process.exit(1)
return null
}
}
+1 -1
View File
@@ -1,6 +1,6 @@
import minimist from 'minimist'
import { FileGraph } from '@linux-odyssey/file-graph'
import Quest from '../models/quest.js'
import { Quest } from '@linux-odyssey/models'
import { pushToSession } from '../api/socket.js'
import SessionHandler from './sessionHandler.js'
+2 -2
View File
@@ -4,10 +4,10 @@ import http from 'http'
import YAML from 'yaml'
import swaggerUI from 'swagger-ui-express'
import passport from 'passport'
import connectDB from '@linux-odyssey/models'
import './auth/passport.js'
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'
@@ -17,7 +17,7 @@ import expiryRemovalScheduler from './containers/expiryChecker.js'
import { createTestUser } from './utils/auth.js'
async function main() {
await connectDB()
await connectDB(config.db)
await createTestUser()
-33
View File
@@ -1,33 +0,0 @@
import { model, Schema } from 'mongoose'
const Command = model(
'Command',
new Schema(
{
session: {
type: Schema.ObjectId,
ref: 'Session',
required: true,
},
pwd: {
type: String,
required: true,
},
command: {
type: String,
required: true,
},
output: {
type: String,
required: false,
},
error: {
type: String,
required: false,
},
},
{ timestamps: true }
)
)
export default Command
-71
View File
@@ -1,71 +0,0 @@
import { model, Schema } from 'mongoose'
const responseSchema = new Schema({
type: {
type: String,
required: true,
},
content: [String],
speaker: String,
color: String,
})
const stageSchema = new Schema({
id: {
type: String,
required: true,
},
task: {
type: String,
},
requirements: [
{
type: String,
required: true,
},
],
repeatable: {
type: Boolean,
required: true,
default: false,
},
condition: {
command: [String],
output: [String],
error: [String],
pwd: [String],
},
responses: {
type: [responseSchema],
required: true,
},
hints: {
type: [String],
required: true,
},
})
const Quest = model(
'Quest',
new Schema({
_id: String,
title: String,
order: Number,
instruction: String,
tasks: {
id: String,
content: String,
display: {
type: Boolean,
default: false,
},
completed: {
type: Boolean,
default: false,
},
},
stages: [stageSchema],
})
)
export default Quest
-71
View File
@@ -1,71 +0,0 @@
import { model, Schema } from 'mongoose'
import { FileNode } from '@linux-odyssey/file-graph'
const nodeSchema = new Schema({
path: String,
type: String,
discovered: {
type: Boolean,
default: false,
},
})
nodeSchema.add({
children: [nodeSchema],
})
const taskSchema = new Schema({
id: {
type: String,
required: true,
},
name: {
type: String,
required: true,
},
completed: {
type: Boolean,
default: false,
},
})
const Session = model(
'Session',
new Schema(
{
user: {
type: Schema.ObjectId,
ref: 'User',
},
quest: {
type: String,
ref: 'Quest',
},
containerId: String,
status: {
type: String,
enum: ['active', 'finished', 'inactive'],
required: true,
default: 'active',
},
finishedAt: Date,
lastActivityAt: {
type: Date,
default: Date.now,
required: true,
},
tasks: [taskSchema],
hints: [String],
graph: {
type: nodeSchema,
default: new FileNode({
path: '/',
type: 'folder',
}),
},
},
{ timestamps: true }
)
)
export default Session
-25
View File
@@ -1,25 +0,0 @@
import { model } from 'mongoose'
const User = model('User', {
username: {
type: String,
unique: true,
required: true,
},
email: {
type: String,
unique: true,
required: true,
},
hashedPassword: String,
google: {
id: String,
displayName: String,
},
github: {
id: String,
displayName: String,
},
})
export default User
+1 -1
View File
@@ -1,7 +1,7 @@
import jwt from 'jsonwebtoken'
import { User } from '@linux-odyssey/models'
import bcrypt from 'bcrypt'
import config from '../config.js'
import User from '../models/user.js'
export function hashPassword(password) {
return bcrypt.hash(password, 10)
+1 -1
View File
@@ -2,7 +2,7 @@ import fs from 'fs/promises'
import path from 'path'
import yaml from 'yaml'
import Quest from '../models/quest.js'
import { Quest } from '@linux-odyssey/models'
const questDirectory = path.join(process.cwd(), '..', 'quests')