mirror of
https://github.com/Cameri/nostream.git
synced 2025-03-26 17:52:30 +01:00
commit latest changes
This commit is contained in:
parent
93e561f00e
commit
874d7a4856
13
migrations/20230312_210600_create_configs_table.js
Normal file
13
migrations/20230312_210600_create_configs_table.js
Normal file
@ -0,0 +1,13 @@
|
||||
exports.up = function (knex) {
|
||||
return knex.schema.createTable('configs', (config) => {
|
||||
config.unique(['key', 'category'])
|
||||
config.uuid('id').primary().defaultTo(knex.raw('uuid_generate_v4()'))
|
||||
config.text('key').notNullable().index()
|
||||
config.jsonb('value').notNullable().index()
|
||||
config.text('category').notNullable().index()
|
||||
})
|
||||
}
|
||||
|
||||
exports.down = function (knex) {
|
||||
return knex.schema.dropTable('configs')
|
||||
}
|
4
package-lock.json
generated
4
package-lock.json
generated
@ -10,7 +10,7 @@
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@noble/secp256k1": "1.7.1",
|
||||
"axios": "^1.2.3",
|
||||
"axios": "1.2.3",
|
||||
"bech32": "2.0.0",
|
||||
"body-parser": "1.20.1",
|
||||
"debug": "4.3.4",
|
||||
@ -20,7 +20,7 @@
|
||||
"helmet": "6.0.1",
|
||||
"joi": "17.7.0",
|
||||
"js-yaml": "4.1.0",
|
||||
"knex": "^2.4.1",
|
||||
"knex": "2.4.1",
|
||||
"pg": "8.8.0",
|
||||
"pg-query-stream": "4.2.4",
|
||||
"ramda": "0.28.0",
|
||||
|
@ -1,7 +1,7 @@
|
||||
[
|
||||
{
|
||||
"key": "relay_url",
|
||||
"value": "wss://nostream.your-domain.com",
|
||||
"value": { "url": "wss://nostream.your-domain.com"},
|
||||
"category": "info"
|
||||
},
|
||||
{
|
||||
|
@ -2,38 +2,33 @@
|
||||
const { extname, join } = require('path')
|
||||
const fs = require('fs')
|
||||
const yaml = require('js-yaml')
|
||||
const { v5: uuidv5 } = require('uuid')
|
||||
const { mergeDeepRight } = require('ramda')
|
||||
const { mergeDeepLeft } = require('ramda')
|
||||
|
||||
const SettingsFileTypes = {
|
||||
yaml: 'yaml',
|
||||
json: 'json',
|
||||
}
|
||||
|
||||
const NAMESPACE = 'c646b451-db73-47fb-9a70-ea24ce8a225a'
|
||||
|
||||
exports.seed = async function (knex) {
|
||||
const settingsFilePath = `${process.cwd()}/seeds/configs.json`
|
||||
let defaultConfigs = fs.readFileSync(settingsFilePath)
|
||||
defaultConfigs = addIdsToConfigs(defaultConfigs)
|
||||
const defaultConfigs = JSON.parse(fs.readFileSync(settingsFilePath, 'utf-8'))
|
||||
|
||||
const rawConfigs = getConfigs()
|
||||
const parsedConfigs = parseAll(rawConfigs)
|
||||
|
||||
const mergedSettings = mergeDeepRight(defaultConfigs, parsedConfigs)
|
||||
const mergedSettings = mergeDeepLeft(defaultConfigs, parsedConfigs)
|
||||
|
||||
if (mergedSettings) {
|
||||
// await knex.batchInsert('configs', configsByCategory, 10)
|
||||
}
|
||||
}
|
||||
|
||||
const addIdsToConfigs = (configs) => {
|
||||
return configs.map(config => {
|
||||
return {
|
||||
...config,
|
||||
id: uuidv5('key', NAMESPACE),
|
||||
for (const settingKey of Object.keys(mergedSettings)) {
|
||||
try {
|
||||
//const res = await knex('configs').insert(setting)
|
||||
const res = await knex('configs').insert([mergedSettings[settingKey]])
|
||||
console.log('knex res', res)
|
||||
} catch (err) {
|
||||
// TODO remove this log when finished developing
|
||||
console.log('Failed to insert config due to error: ', err)
|
||||
// Nothing to log as if this fails the config already exists, which is fine
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const getConfigs = () => {
|
||||
@ -92,11 +87,8 @@ const parseAll = (jsonConfigs) => {
|
||||
}
|
||||
|
||||
const parseOneLevelDeepConfigs = (configs, category) => {
|
||||
const keys = Object.keys(configs)
|
||||
console.log(keys)
|
||||
const flattenedConfigs = Object.keys(configs).map(key => {
|
||||
return {
|
||||
id: uuidv5('key', NAMESPACE),
|
||||
key,
|
||||
value: configs[key],
|
||||
category,
|
||||
@ -105,3 +97,6 @@ const parseOneLevelDeepConfigs = (configs, category) => {
|
||||
|
||||
return flattenedConfigs
|
||||
}
|
||||
|
||||
|
||||
// TODO: fix the key "enabled", as it repeats
|
19
src/index.ts
19
src/index.ts
@ -3,38 +3,33 @@ import dotenv from 'dotenv'
|
||||
dotenv.config()
|
||||
|
||||
import { appFactory } from './factories/app-factory'
|
||||
import { getMasterDbClient } from './database/client'
|
||||
import { maintenanceWorkerFactory } from './factories/maintenance-worker-factory'
|
||||
import { SettingsStatic } from './utils/settings'
|
||||
import { staticMirroringWorkerFactory } from './factories/static-mirroring.worker-factory'
|
||||
import { workerFactory } from './factories/worker-factory'
|
||||
|
||||
export const getRunner = (): any => {
|
||||
const dbClient = getMasterDbClient()
|
||||
const initializeSettings = new SettingsStatic(dbClient).init()
|
||||
console.log('here1i')
|
||||
const settingsInstance = SettingsStatic.instance
|
||||
|
||||
initializeSettings
|
||||
settingsInstance.init()
|
||||
.then(() => {
|
||||
if (cluster.isPrimary) {
|
||||
appFactory().run()
|
||||
return appFactory().run()
|
||||
} else {
|
||||
switch (process.env.WORKER_TYPE) {
|
||||
case 'worker':
|
||||
workerFactory().run()
|
||||
return
|
||||
return workerFactory().run()
|
||||
case 'maintenance':
|
||||
maintenanceWorkerFactory().run()
|
||||
return
|
||||
return maintenanceWorkerFactory().run()
|
||||
case 'static-mirroring':
|
||||
staticMirroringWorkerFactory().run()
|
||||
return
|
||||
return staticMirroringWorkerFactory().run()
|
||||
default:
|
||||
throw new Error(`Unknown worker: ${process.env.WORKER_TYPE}`)
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.log('whoooops---------', error)
|
||||
throw new Error('Failed to load settings', error)
|
||||
})
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
import { createLogger } from '../factories/logger-factory'
|
||||
import { DatabaseClient } from '../@types/base'
|
||||
import { getMasterDbClient } from '../database/client'
|
||||
import { Setting } from '../@types/setting'
|
||||
import { SettingRepository } from '../repositories/settings-repository'
|
||||
import { Settings } from '../@types/settings'
|
||||
@ -9,10 +10,10 @@ const debug = createLogger('settings')
|
||||
export class SettingsStatic {
|
||||
private static _instance: SettingsStatic
|
||||
private static dbClient: DatabaseClient
|
||||
static _settings: Settings | undefined
|
||||
static _settings: any | undefined
|
||||
static settingsRepository: SettingRepository | undefined
|
||||
|
||||
constructor(dbClient: DatabaseClient) {
|
||||
private constructor(dbClient: DatabaseClient) {
|
||||
SettingsStatic.dbClient = dbClient
|
||||
SettingsStatic.settingsRepository = new SettingRepository(dbClient)
|
||||
if (SettingsStatic._instance) {
|
||||
@ -22,32 +23,34 @@ export class SettingsStatic {
|
||||
SettingsStatic._instance = this
|
||||
}
|
||||
|
||||
public init() {
|
||||
public async init() {
|
||||
debug('SettingsStatic.init()')
|
||||
return new Promise((resolve, reject) => {
|
||||
const settingsPromise = SettingsStatic.loadSettingsFromDb(SettingsStatic.constructSettingsJsonBlob)
|
||||
if (settingsPromise) {
|
||||
resolve('success')
|
||||
}
|
||||
reject('Failed to initialize settings')
|
||||
})
|
||||
await SettingsStatic.loadSettingsFromDb()
|
||||
//const settingsPromise = await SettingsStatic.loadSettingsFromDb(SettingsStatic.constructSettingsJsonBlob)
|
||||
//if (settingsPromise) {
|
||||
// resolve('success')
|
||||
//}
|
||||
// reject('Failed to initialize settings')
|
||||
}
|
||||
|
||||
static get instance() {
|
||||
return SettingsStatic._instance ?? (SettingsStatic._instance = new SettingsStatic(this.dbClient))
|
||||
return SettingsStatic._instance ?? (SettingsStatic._instance = new SettingsStatic(getMasterDbClient()))
|
||||
}
|
||||
|
||||
private static loadSettingsFromDb(callback) {
|
||||
private static async loadSettingsFromDb() {
|
||||
debug('SettingsStatic.loadSettingsFromDb()')
|
||||
const promise = SettingsStatic.settingsRepository.getSettings()
|
||||
const rawDbSettings = await SettingsStatic.settingsRepository.getSettings()
|
||||
const parsedSettings = SettingsStatic.constructSettingsJsonBlob(rawDbSettings)
|
||||
this._settings = parsedSettings
|
||||
console.log('rawDbSettings', rawDbSettings)
|
||||
console.log('parsedSettings', parsedSettings)
|
||||
|
||||
return promise.then(rawSettingsFromDb => {
|
||||
const settingsJsonBlob = callback(rawSettingsFromDb)
|
||||
this._settings = settingsJsonBlob
|
||||
})
|
||||
// return promise.then(rawSettingsFromDb => {
|
||||
// const settingsJsonBlob = callback(rawSettingsFromDb)
|
||||
// this._settings = settingsJsonBlob
|
||||
// })
|
||||
}
|
||||
|
||||
|
||||
public static createSettings(): Settings {
|
||||
return this._settings
|
||||
}
|
||||
@ -59,8 +62,9 @@ export class SettingsStatic {
|
||||
}
|
||||
|
||||
private static updateSingletonSettings(setting) {
|
||||
const updateSettings = this._settings
|
||||
updateSettings[setting.category][setting.key] = setting.value
|
||||
const updatedSettings = this._settings
|
||||
updatedSettings[setting.category][setting.key] = setting.value
|
||||
this._settings = updatedSettings
|
||||
}
|
||||
|
||||
private static constructSettingsJsonBlob(rawSettingsFromDb): any {
|
||||
|
Loading…
x
Reference in New Issue
Block a user