mirror of
https://gitlab.com/MrFry/mrfrys-node-server
synced 2025-04-01 20:24:18 +02:00
script build update, peers file watching/updating/validating fixes, peers schema improvements
This commit is contained in:
parent
1e4f45a76c
commit
0d1abc7d4f
4 changed files with 33 additions and 35 deletions
|
@ -88,6 +88,9 @@ log "Making moodle test userscript"
|
||||||
checkFile "$PWD/submodules/moodle-test-userscript/stable.user.js"
|
checkFile "$PWD/submodules/moodle-test-userscript/stable.user.js"
|
||||||
mkdir -pv "$PWD/publicDirs/qminingPublic/moodle-test-userscript"
|
mkdir -pv "$PWD/publicDirs/qminingPublic/moodle-test-userscript"
|
||||||
ln -sfv "$PWD/submodules/moodle-test-userscript/stable.user.js" "$PWD/publicDirs/qminingPublic/moodle-test-userscript/"
|
ln -sfv "$PWD/submodules/moodle-test-userscript/stable.user.js" "$PWD/publicDirs/qminingPublic/moodle-test-userscript/"
|
||||||
|
cd "./submodules/moodle-test-userscript/" || exit
|
||||||
|
./make.sh || exit
|
||||||
|
cd "${baseDir}" || exit
|
||||||
|
|
||||||
# ------------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------------
|
||||||
# DB-s
|
# DB-s
|
||||||
|
|
|
@ -53,12 +53,7 @@ import {
|
||||||
getAvailableQdbIndexes,
|
getAvailableQdbIndexes,
|
||||||
removeCacheFromQuestion,
|
removeCacheFromQuestion,
|
||||||
} from '../../../utils/qdbUtils'
|
} from '../../../utils/qdbUtils'
|
||||||
import {
|
import { files, paths, readAndValidateFile } from '../../../utils/files'
|
||||||
isJsonValidAndLogError,
|
|
||||||
PeersInfoSchema,
|
|
||||||
SelfInfoSchema,
|
|
||||||
} from '../../../types/typeSchemas'
|
|
||||||
import { paths } from '../../../utils/files'
|
|
||||||
import { GetResult, get, post } from '../../../utils/networkUtils'
|
import { GetResult, get, post } from '../../../utils/networkUtils'
|
||||||
|
|
||||||
interface MergeResult {
|
interface MergeResult {
|
||||||
|
@ -419,15 +414,6 @@ function setup(data: SubmoduleData): Submodule {
|
||||||
|
|
||||||
let peers: PeerInfo[] = utils.ReadJSON(paths.peersFile)
|
let peers: PeerInfo[] = utils.ReadJSON(paths.peersFile)
|
||||||
let selfInfo: PeerInfo = utils.ReadJSON(paths.selfInfoFile)
|
let selfInfo: PeerInfo = utils.ReadJSON(paths.selfInfoFile)
|
||||||
|
|
||||||
// TODO: this should be checked by files!
|
|
||||||
if (!isJsonValidAndLogError(peers, PeersInfoSchema, paths.peersFile)) {
|
|
||||||
throw new Error('Invalid peers file')
|
|
||||||
}
|
|
||||||
if (!isJsonValidAndLogError(selfInfo, SelfInfoSchema, paths.selfInfoFile)) {
|
|
||||||
throw new Error('Invalid peers file')
|
|
||||||
}
|
|
||||||
// self info file is not required to have the publicKey, as it is always added on init
|
|
||||||
selfInfo.publicKey = publicKey
|
selfInfo.publicKey = publicKey
|
||||||
|
|
||||||
const filesToWatch = [
|
const filesToWatch = [
|
||||||
|
@ -435,13 +421,9 @@ function setup(data: SubmoduleData): Submodule {
|
||||||
fname: paths.peersFile,
|
fname: paths.peersFile,
|
||||||
logMsg: 'Peers file updated',
|
logMsg: 'Peers file updated',
|
||||||
action: () => {
|
action: () => {
|
||||||
try {
|
const newVal = readAndValidateFile<PeerInfo[]>(files.peersFile)
|
||||||
peers = utils.ReadJSON(paths.peersFile)
|
if (newVal) {
|
||||||
} catch (e) {
|
peers = newVal
|
||||||
logger.Log(
|
|
||||||
`Peers file contents are invalid! Check if syntax is correct for ${paths.peersFile}`,
|
|
||||||
'redbg'
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -449,14 +431,9 @@ function setup(data: SubmoduleData): Submodule {
|
||||||
fname: paths.selfInfoFile,
|
fname: paths.selfInfoFile,
|
||||||
logMsg: 'P2P self info file changed',
|
logMsg: 'P2P self info file changed',
|
||||||
action: () => {
|
action: () => {
|
||||||
try {
|
const newVal = readAndValidateFile<PeerInfo>(files.selfInfoFile)
|
||||||
selfInfo = utils.ReadJSON(paths.selfInfoFile)
|
if (newVal) {
|
||||||
selfInfo.publicKey = publicKey
|
selfInfo = newVal
|
||||||
} catch (e) {
|
|
||||||
logger.Log(
|
|
||||||
`Self info file contents are invalid! Check if syntax is correct for ${paths.selfInfoFile}`,
|
|
||||||
'redbg'
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -487,7 +464,7 @@ function setup(data: SubmoduleData): Submodule {
|
||||||
|
|
||||||
function getSelfInfo(includeVerboseInfo?: boolean) {
|
function getSelfInfo(includeVerboseInfo?: boolean) {
|
||||||
const result: RemotePeerInfo = {
|
const result: RemotePeerInfo = {
|
||||||
selfInfo: selfInfo,
|
selfInfo: { ...selfInfo, publicKey: publicKey },
|
||||||
myPeers: peers,
|
myPeers: peers,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -941,7 +918,7 @@ function setup(data: SubmoduleData): Submodule {
|
||||||
// APP SETUP
|
// APP SETUP
|
||||||
// ---------------------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------------------
|
||||||
app.get('/selfInfo', (_req: Request, res: Response<PeerInfo>) => {
|
app.get('/selfInfo', (_req: Request, res: Response<PeerInfo>) => {
|
||||||
res.json(selfInfo)
|
res.json({ ...selfInfo, publicKey: publicKey })
|
||||||
})
|
})
|
||||||
|
|
||||||
app.get('/p2pinfo', (_req: Request, res: Response<RemotePeerInfo>) => {
|
app.get('/p2pinfo', (_req: Request, res: Response<RemotePeerInfo>) => {
|
||||||
|
|
|
@ -42,14 +42,12 @@ const PeerInfoSchemaBase = {
|
||||||
name: { type: 'string' },
|
name: { type: 'string' },
|
||||||
host: { type: 'string' },
|
host: { type: 'string' },
|
||||||
port: { type: 'number' },
|
port: { type: 'number' },
|
||||||
publicKey: { type: 'string' },
|
|
||||||
contact: { type: 'string' },
|
contact: { type: 'string' },
|
||||||
lastSync: { type: 'number' },
|
lastSync: { type: 'number' },
|
||||||
note: { type: 'string' },
|
note: { type: 'string' },
|
||||||
pw: { type: 'string' },
|
|
||||||
http: { type: 'boolean' },
|
http: { type: 'boolean' },
|
||||||
},
|
},
|
||||||
required: ['name', 'host', 'port', 'contact', 'pw'],
|
additionalProperties: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const SelfInfoSchema: Schema = {
|
export const SelfInfoSchema: Schema = {
|
||||||
|
@ -59,6 +57,12 @@ export const SelfInfoSchema: Schema = {
|
||||||
|
|
||||||
export const PeerInfoSchema: Schema = {
|
export const PeerInfoSchema: Schema = {
|
||||||
...PeerInfoSchemaBase,
|
...PeerInfoSchemaBase,
|
||||||
|
properties: {
|
||||||
|
...PeerInfoSchemaBase.properties,
|
||||||
|
publicKey: { type: 'string' },
|
||||||
|
pw: { type: 'string' },
|
||||||
|
},
|
||||||
|
required: ['name', 'host', 'port', 'contact', 'pw'],
|
||||||
}
|
}
|
||||||
|
|
||||||
export const PeersInfoSchema: Schema = {
|
export const PeersInfoSchema: Schema = {
|
||||||
|
|
|
@ -51,6 +51,20 @@ export const validateFiles = (): boolean => {
|
||||||
return everythingValid
|
return everythingValid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const readAndValidateFile = <T>(file: FileDescriptor): T => {
|
||||||
|
if (!file.schema) return null
|
||||||
|
const fileExists = utils.FileExists(file.path)
|
||||||
|
if (!fileExists) return null
|
||||||
|
|
||||||
|
const content = utils.ReadFile(file.path)
|
||||||
|
const parsedContent: T = JSON.parse(content)
|
||||||
|
|
||||||
|
if (!isJsonValidAndLogError(parsedContent, file.schema, file.path))
|
||||||
|
return null
|
||||||
|
|
||||||
|
return parsedContent
|
||||||
|
}
|
||||||
|
|
||||||
export const files = {
|
export const files = {
|
||||||
// --------------------------------------------------------------------------------
|
// --------------------------------------------------------------------------------
|
||||||
// server / modules files
|
// server / modules files
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue