mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
global public dir for every server module
This commit is contained in:
parent
eed99e6f02
commit
0381f6ddab
18 changed files with 55 additions and 225 deletions
|
@ -39,7 +39,7 @@ import {
|
|||
Submodule,
|
||||
} from '../../types/basicTypes'
|
||||
import { loadJSON } from '../../utils/actions'
|
||||
import { paths } from '../../utils/files'
|
||||
import { paths, publicDir } from '../../utils/files'
|
||||
import { initWorkerPool } from '../../worker/workerPool'
|
||||
|
||||
// other paths
|
||||
|
@ -48,18 +48,12 @@ const moduleName = 'API'
|
|||
// stuff gotten from server.js
|
||||
let userDB: Database
|
||||
let url: string
|
||||
let publicdirs: string[] = []
|
||||
let httpServer: http.Server
|
||||
let httpsServer: https.Server
|
||||
|
||||
function GetApp(): ModuleType {
|
||||
const app = express()
|
||||
|
||||
const publicDir = publicdirs[0]
|
||||
if (!publicDir) {
|
||||
throw new Error(`No public dir! ( API )`)
|
||||
}
|
||||
|
||||
let domain: any = url.split('.')
|
||||
domain.shift()
|
||||
domain = domain.join('.') // "qmining.com"
|
||||
|
@ -157,9 +151,7 @@ function GetApp(): ModuleType {
|
|||
|
||||
// -------------------------------------------------------------------------------------------
|
||||
|
||||
publicdirs.forEach((pdir) => {
|
||||
app.use(express.static(pdir))
|
||||
})
|
||||
app.use(express.static(publicDir))
|
||||
|
||||
// -------------------------------------------------------------------------------------------
|
||||
|
||||
|
@ -206,7 +198,6 @@ function setupSubModules(
|
|||
app: parentApp,
|
||||
userDB: userDB,
|
||||
url: url, // only used by userManagement.ts
|
||||
publicdirs: publicdirs,
|
||||
moduleSpecificData: moduleSpecificData,
|
||||
httpServer: httpServer,
|
||||
httpsServer: httpsServer,
|
||||
|
@ -227,7 +218,6 @@ export default {
|
|||
setup: (data: SetupData): void => {
|
||||
userDB = data.userDB
|
||||
url = data.url
|
||||
publicdirs = data.publicdirs
|
||||
httpServer = data.httpServer
|
||||
httpsServer = data.httpsServer
|
||||
},
|
||||
|
|
|
@ -25,6 +25,7 @@ import dbtools from '../../../utils/dbtools'
|
|||
import logger from '../../../utils/logger'
|
||||
import { Request, SubmoduleData, User } from '../../../types/basicTypes'
|
||||
import socketAuth from '../../../middlewares/socketAuth.middleware'
|
||||
import { publicDir } from '../../../utils/files'
|
||||
|
||||
const msgDbPath = './data/dbs/msgs.db'
|
||||
const msgPaginationLimit = 15
|
||||
|
@ -44,10 +45,9 @@ interface Message {
|
|||
}
|
||||
|
||||
function setup(data: SubmoduleData): void {
|
||||
const { app, httpServer, httpsServer, userDB, publicdirs } = data
|
||||
const { app, httpServer, httpsServer, userDB } = data
|
||||
const msgDB = dbtools.GetDB(msgDbPath)
|
||||
|
||||
const publicDir = publicdirs[0]
|
||||
const uloadFiles = publicDir + 'chatFiles'
|
||||
logger.Log(
|
||||
`Chat: Starting Socket.io Server on ${httpsServer ? 'https' : 'http'}`
|
||||
|
|
|
@ -23,6 +23,7 @@ import { v4 as uuidv4 } from 'uuid'
|
|||
import logger from '../../../utils/logger'
|
||||
import utils from '../../../utils/utils'
|
||||
import { Request, SubmoduleData, User } from '../../../types/basicTypes'
|
||||
import { publicDir } from '../../../utils/files'
|
||||
|
||||
interface Comment {
|
||||
date: string
|
||||
|
@ -184,9 +185,7 @@ function getPostData(
|
|||
}
|
||||
|
||||
function setup(data: SubmoduleData): void {
|
||||
const { app, publicdirs } = data
|
||||
|
||||
const publicDir = publicdirs[0]
|
||||
const { app } = data
|
||||
|
||||
const forumDir = publicDir + 'forum'
|
||||
const forumFiles = publicDir + 'forumFiles'
|
||||
|
|
|
@ -47,7 +47,12 @@ import {
|
|||
getAvailableQdbIndexes,
|
||||
removeCacheFromQuestion,
|
||||
} from '../../../utils/qdbUtils'
|
||||
import { files, paths, readAndValidateFile } from '../../../utils/files'
|
||||
import {
|
||||
files,
|
||||
paths,
|
||||
publicDir,
|
||||
readAndValidateFile,
|
||||
} from '../../../utils/files'
|
||||
import { GetResult, get } from '../../../utils/networkUtils'
|
||||
import {
|
||||
msgAllWorker,
|
||||
|
@ -294,8 +299,7 @@ async function sendNewDataToWorkers(
|
|||
function writeNewData(
|
||||
newQuestionDbs: QuestionDb[],
|
||||
changedQuestionDbs: QuestionDb[],
|
||||
dbsFilePath: string,
|
||||
publicDir: string
|
||||
dbsFilePath: string
|
||||
) {
|
||||
const qdbsToWrite = [...changedQuestionDbs, ...newQuestionDbs]
|
||||
const existingQdbs = utils.ReadJSON<DataFile[]>(dbsFilePath)
|
||||
|
@ -486,11 +490,9 @@ function setup(data: SubmoduleData): Submodule {
|
|||
const {
|
||||
app,
|
||||
userDB,
|
||||
publicdirs,
|
||||
moduleSpecificData: { setQuestionDbs, getQuestionDbs, dbsFile },
|
||||
} = data
|
||||
|
||||
const publicDir = publicdirs[0]
|
||||
let syncInProgress = false
|
||||
|
||||
// ---------------------------------------------------------------------------------------
|
||||
|
@ -1063,8 +1065,7 @@ function setup(data: SubmoduleData): Submodule {
|
|||
getQuestionDbs().filter((qdb) => {
|
||||
return changedQdbIndexes.includes(qdb.index)
|
||||
}),
|
||||
dbsFile,
|
||||
publicDir
|
||||
dbsFile
|
||||
)
|
||||
|
||||
setQuestionDbs([...mergedQuestionDbs, ...newQuestionDbs])
|
||||
|
|
|
@ -55,7 +55,7 @@ import {
|
|||
SearchResultQuestion,
|
||||
subjectToString,
|
||||
} from '../../../utils/qdbUtils'
|
||||
import { paths } from '../../../utils/files'
|
||||
import { paths, publicDir } from '../../../utils/files'
|
||||
import constants from '../../../constants'
|
||||
import {
|
||||
isJsonValidAndLogError,
|
||||
|
@ -391,7 +391,6 @@ function getNewQdb(
|
|||
location: string,
|
||||
maxIndex: number,
|
||||
dbsFile: string,
|
||||
publicDir: string,
|
||||
questionDbs: QuestionDb[]
|
||||
) {
|
||||
logger.Log(
|
||||
|
@ -446,11 +445,9 @@ function setup(data: SubmoduleData): Submodule {
|
|||
const {
|
||||
app,
|
||||
userDB,
|
||||
publicdirs,
|
||||
moduleSpecificData: { getQuestionDbs, setQuestionDbs, dbsFile },
|
||||
} = data
|
||||
|
||||
const publicDir = publicdirs[0]
|
||||
const motdFile = publicDir + 'motd'
|
||||
const savedQuestionsDir = publicDir + 'savedQuestions'
|
||||
|
||||
|
@ -586,13 +583,7 @@ function setup(data: SubmoduleData): Submodule {
|
|||
if (suitedQuestionDbs.length === 0) {
|
||||
if (!dbExists(location, getQuestionDbs())) {
|
||||
suitedQuestionDbs.push(
|
||||
getNewQdb(
|
||||
location,
|
||||
maxIndex,
|
||||
dbsFile,
|
||||
publicDir,
|
||||
getQuestionDbs()
|
||||
)
|
||||
getNewQdb(location, maxIndex, dbsFile, getQuestionDbs())
|
||||
)
|
||||
} else {
|
||||
logger.Log(
|
||||
|
|
|
@ -1,134 +0,0 @@
|
|||
/* ----------------------------------------------------------------------------
|
||||
|
||||
Question Server
|
||||
GitLab: <https://gitlab.com/MrFry/mrfrys-node-server>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
------------------------------------------------------------------------- */
|
||||
|
||||
import { Response } from 'express'
|
||||
|
||||
import logger from '../../../utils/logger'
|
||||
import utils from '../../../utils/utils'
|
||||
import { Request, SubmoduleData } from '../../../types/basicTypes'
|
||||
|
||||
interface Categories {
|
||||
[key: string]: {
|
||||
name: string
|
||||
color: string
|
||||
}
|
||||
}
|
||||
|
||||
enum CardState {
|
||||
TODO = 'todo',
|
||||
INPROGRESS = 'inprogress',
|
||||
TESTING = 'testing',
|
||||
DONE = 'done',
|
||||
INPROD = 'inprod',
|
||||
NOTPOSSIBLE = 'notpossible',
|
||||
}
|
||||
|
||||
interface Card {
|
||||
id: number
|
||||
name: string
|
||||
description: string
|
||||
category: string
|
||||
points: number
|
||||
state: CardState
|
||||
votes: number[]
|
||||
}
|
||||
|
||||
type Columns = {
|
||||
[key in CardState]: {
|
||||
name: string
|
||||
clickable: boolean
|
||||
}
|
||||
}
|
||||
|
||||
interface Groups {
|
||||
[key: string]: {
|
||||
name: string
|
||||
description: string
|
||||
}
|
||||
}
|
||||
|
||||
interface Todos {
|
||||
categories: Categories
|
||||
cards: Card[]
|
||||
columns: Columns
|
||||
groups: Groups
|
||||
}
|
||||
|
||||
const todosFile = 'data/todos.json'
|
||||
|
||||
function setup(data: SubmoduleData): void {
|
||||
const { app } = data
|
||||
|
||||
app.get('/voteTodo', (req: Request, res: Response) => {
|
||||
logger.LogReq(req)
|
||||
const userId = req.session.user.id
|
||||
const id: string = req.query.id
|
||||
const todos: Todos = utils.ReadJSON(todosFile)
|
||||
|
||||
if (!id) {
|
||||
res.json({
|
||||
msg: 'id query undefined',
|
||||
result: 'not ok',
|
||||
})
|
||||
}
|
||||
|
||||
const cardIndex = todos.cards.findIndex((currcard) => {
|
||||
return currcard.id === parseInt(id)
|
||||
})
|
||||
if (cardIndex === -1) {
|
||||
res.json({
|
||||
msg: 'card not found',
|
||||
result: 'not ok',
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const ind = todos.cards[cardIndex].votes.indexOf(userId)
|
||||
if (ind === -1) {
|
||||
todos.cards[cardIndex].votes.push(userId)
|
||||
} else {
|
||||
todos.cards[cardIndex].votes.splice(ind, 1)
|
||||
}
|
||||
|
||||
utils.WriteFile(JSON.stringify(todos, null, 2), todosFile)
|
||||
res.json({
|
||||
todos: todos,
|
||||
userId: userId,
|
||||
msg: 'updated',
|
||||
result: 'ok',
|
||||
})
|
||||
})
|
||||
|
||||
app.get('/todos', (req: Request, res: Response) => {
|
||||
logger.LogReq(req)
|
||||
const userId = req.session.user.id
|
||||
const todos = utils.ReadJSON(todosFile)
|
||||
|
||||
res.json({
|
||||
todos: todos,
|
||||
userId: userId,
|
||||
result: 'ok',
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export default {
|
||||
setup: setup,
|
||||
}
|
|
@ -23,10 +23,11 @@ import fs from 'fs'
|
|||
import logger from '../../../utils/logger'
|
||||
import utils from '../../../utils/utils'
|
||||
import { Request, SubmoduleData, User } from '../../../types/basicTypes'
|
||||
import { publicDir } from '../../../utils/files'
|
||||
|
||||
const dataFileName = '.data.json'
|
||||
|
||||
function listDir(publicDir: string, subdir: string, userFilesDir: string) {
|
||||
function listDir(subdir: string, userFilesDir: string) {
|
||||
const safeSubdir = subdir.replace(/\.+/g, '').replace(/\/+/g, '')
|
||||
const dir = userFilesDir + '/' + safeSubdir
|
||||
const usersFile = dir + '/' + dataFileName
|
||||
|
@ -83,7 +84,7 @@ function listDir(publicDir: string, subdir: string, userFilesDir: string) {
|
|||
}
|
||||
|
||||
function setup(data: SubmoduleData): void {
|
||||
const { app, publicdirs } = data
|
||||
const { app } = data
|
||||
|
||||
app.use((req: Request, _res, next) => {
|
||||
// /userFiles/test/2021-04-28_10-59.png
|
||||
|
@ -121,8 +122,6 @@ function setup(data: SubmoduleData): void {
|
|||
next()
|
||||
})
|
||||
|
||||
const publicDir = publicdirs[0]
|
||||
|
||||
const userFilesDir = publicDir + 'userFiles'
|
||||
if (!utils.FileExists(userFilesDir)) {
|
||||
utils.CreatePath(userFilesDir, true)
|
||||
|
@ -138,7 +137,7 @@ function setup(data: SubmoduleData): void {
|
|||
const subdir: string = req.query.subdir
|
||||
|
||||
if (subdir) {
|
||||
const result = listDir(publicDir, subdir, userFilesDir)
|
||||
const result = listDir(subdir, userFilesDir)
|
||||
res.json(result)
|
||||
} else {
|
||||
res.json({
|
||||
|
@ -315,7 +314,7 @@ function setup(data: SubmoduleData): void {
|
|||
utils.WriteFile(JSON.stringify(data), dataFilePath)
|
||||
}
|
||||
|
||||
const result = listDir(publicDir, dir, userFilesDir)
|
||||
const result = listDir(dir, userFilesDir)
|
||||
res.json(result)
|
||||
})
|
||||
|
||||
|
@ -333,7 +332,7 @@ function setup(data: SubmoduleData): void {
|
|||
return
|
||||
}
|
||||
utils.CreatePath(userFilesDir + '/' + safeName, true)
|
||||
const result = listDir(publicDir, name, userFilesDir)
|
||||
const result = listDir(name, userFilesDir)
|
||||
if (result.files.length === 0) {
|
||||
utils.deleteDir(userFilesDir + '/' + safeName)
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue