mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
Merge pull request #40 from linux-odyssey/refactor-separate-models
Separate models to new package
This commit is contained in:
27 files changed
+127
-61
No files matched your search
@@ -1,6 +1,8 @@
|
||||
version: '3'
|
||||
services:
|
||||
app:
|
||||
depends_on:
|
||||
- backend
|
||||
build:
|
||||
context: .
|
||||
dockerfile: app/Dockerfile.dev
|
||||
@@ -12,6 +14,8 @@ services:
|
||||
- ./app:/app/app
|
||||
- ./packages:/app/packages
|
||||
backend:
|
||||
depends_on:
|
||||
- db
|
||||
build:
|
||||
context: .
|
||||
dockerfile: server/Dockerfile.dev
|
||||
|
||||
File renamed without changes.
@@ -0,0 +1,14 @@
|
||||
import mongoose from 'mongoose'
|
||||
|
||||
export { default as User } from './user.js'
|
||||
export { default as Quest } from './quest.js'
|
||||
export { default as Session } from './session.js'
|
||||
export { default as Command } from './command.js'
|
||||
|
||||
export default async function connectDB(uri) {
|
||||
const db = await mongoose.connect(uri, {
|
||||
useNewUrlParser: true,
|
||||
useUnifiedTopology: true,
|
||||
})
|
||||
return db.connection.getClient()
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "@linux-odyssey/models",
|
||||
"type": "module",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"license": "GPL",
|
||||
"exports": "./index.js",
|
||||
"dependencies": {
|
||||
"@linux-odyssey/file-graph": "*",
|
||||
"mongoose": "^7.6.0"
|
||||
}
|
||||
}
|
||||
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,33 @@
|
||||
import { model, Schema } from 'mongoose'
|
||||
|
||||
const User = model(
|
||||
'User',
|
||||
new Schema(
|
||||
{
|
||||
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,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: true,
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
export default User
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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,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,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,4 +1,4 @@
|
||||
import User from '../models/user.js'
|
||||
import { User } from '@linux-odyssey/models'
|
||||
|
||||
export default async function oauthVerify(provider, profile, query, cb) {
|
||||
process.nextTick(async () => {
|
||||
|
||||
@@ -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,7 +1,8 @@
|
||||
import connectDB from '@linux-odyssey/models'
|
||||
import { removeExpired } from '../containers/expiryChecker.js'
|
||||
import connectDB from '../db.js'
|
||||
import config from '../config.js'
|
||||
|
||||
connectDB()
|
||||
connectDB(config.db)
|
||||
.then(removeExpired)
|
||||
.then(() => {
|
||||
process.exit(0)
|
||||
|
||||
@@ -68,8 +68,8 @@ export async function deleteContainer(id) {
|
||||
}
|
||||
try {
|
||||
await container.stop()
|
||||
await container.remove()
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
console.log(error)
|
||||
}
|
||||
await container.remove()
|
||||
}
|
||||
@@ -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() {
|
||||
|
||||
@@ -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,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'
|
||||
|
||||
|
||||
+8
-2
@@ -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,13 @@ import expiryRemovalScheduler from './containers/expiryChecker.js'
|
||||
import { createTestUser } from './utils/auth.js'
|
||||
|
||||
async function main() {
|
||||
await connectDB()
|
||||
try {
|
||||
await connectDB(config.db)
|
||||
console.log('Connected to MongoDB')
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
await createTestUser()
|
||||
|
||||
|
||||
@@ -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,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)
|
||||
|
||||
@@ -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')
|
||||
|
||||
|
||||
@@ -807,6 +807,13 @@
|
||||
semver "^7.3.5"
|
||||
tar "^6.1.11"
|
||||
|
||||
"@mongodb-js/saslprep@^1.1.0":
|
||||
version "1.1.0"
|
||||
resolved "https://registry.yarnpkg.com/@mongodb-js/saslprep/-/saslprep-1.1.0.tgz#022fa36620a7287d17acd05c4aae1e5f390d250d"
|
||||
integrity sha512-Xfijy7HvfzzqiOAhAepF4SGN5e9leLkMvg/OPOF97XemjfVCYN/oWa75wnkc6mltMSTwY+XlbhWgUOJmkFspSw==
|
||||
dependencies:
|
||||
sparse-bitfield "^3.0.3"
|
||||
|
||||
"@nodelib/fs.scandir@2.1.5":
|
||||
version "2.1.5"
|
||||
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5"
|
||||
@@ -1569,6 +1576,11 @@ bson@^5.4.0:
|
||||
resolved "https://registry.yarnpkg.com/bson/-/bson-5.4.0.tgz#0eea77276d490953ad8616b483298dbff07384c6"
|
||||
integrity sha512-WRZ5SQI5GfUuKnPTNmAYPiKIof3ORXAF4IRU5UcgmivNIon01rWQlw5RUH954dpu8yGL8T59YShVddIPaU/gFA==
|
||||
|
||||
bson@^5.5.0:
|
||||
version "5.5.0"
|
||||
resolved "https://registry.yarnpkg.com/bson/-/bson-5.5.0.tgz#a419cc69f368d2def3b8b22ea03ed1c9be40e53f"
|
||||
integrity sha512-B+QB4YmDx9RStKv8LLSl/aVIEV3nYJc3cJNNTK2Cd1TL+7P+cNpw9mAPeCgc5K+j01Dv6sxUzcITXDx7ZU3F0w==
|
||||
|
||||
buffer-crc32@~0.2.3:
|
||||
version "0.2.13"
|
||||
resolved "https://registry.yarnpkg.com/buffer-crc32/-/buffer-crc32-0.2.13.tgz#0d333e3f00eac50aa1454abd30ef8c2a5d9a7242"
|
||||
@@ -4323,6 +4335,17 @@ mongodb@5.7.0:
|
||||
optionalDependencies:
|
||||
saslprep "^1.0.3"
|
||||
|
||||
mongodb@5.9.0:
|
||||
version "5.9.0"
|
||||
resolved "https://registry.yarnpkg.com/mongodb/-/mongodb-5.9.0.tgz#5a22065fa8cfaf1d58bf2e3c451cd2c4bfa983a2"
|
||||
integrity sha512-g+GCMHN1CoRUA+wb1Agv0TI4YTSiWr42B5ulkiAfLLHitGK1R+PkSAf3Lr5rPZwi/3F04LiaZEW0Kxro9Fi2TA==
|
||||
dependencies:
|
||||
bson "^5.5.0"
|
||||
mongodb-connection-string-url "^2.6.0"
|
||||
socks "^2.7.1"
|
||||
optionalDependencies:
|
||||
"@mongodb-js/saslprep" "^1.1.0"
|
||||
|
||||
mongoose@^7.4.0:
|
||||
version "7.4.0"
|
||||
resolved "https://registry.yarnpkg.com/mongoose/-/mongoose-7.4.0.tgz#3726a8dea0c9e2d54651845cc84c813a6507906a"
|
||||
@@ -4336,6 +4359,19 @@ mongoose@^7.4.0:
|
||||
ms "2.1.3"
|
||||
sift "16.0.1"
|
||||
|
||||
mongoose@^7.6.0:
|
||||
version "7.6.0"
|
||||
resolved "https://registry.yarnpkg.com/mongoose/-/mongoose-7.6.0.tgz#3037b754ccdf168385e52d4bcabeb7f5c3b17a5f"
|
||||
integrity sha512-ztQ12rm0BQN5i7LB6xhWX4l9a9w2aa3jEwa/mM2vAutYJRyAwOzcusvKJBULMzFHyUDBOVW15grisexypgMIWA==
|
||||
dependencies:
|
||||
bson "^5.4.0"
|
||||
kareem "2.5.1"
|
||||
mongodb "5.9.0"
|
||||
mpath "0.9.0"
|
||||
mquery "5.0.0"
|
||||
ms "2.1.3"
|
||||
sift "16.0.1"
|
||||
|
||||
mpath@0.9.0:
|
||||
version "0.9.0"
|
||||
resolved "https://registry.yarnpkg.com/mpath/-/mpath-0.9.0.tgz#0c122fe107846e31fc58c75b09c35514b3871904"
|
||||
|
||||
Reference in new issue
Block a user