api.config
Massimo Melina committed
Jan 9, 2022 at 23:59 UTC
e5728a736c797b4f400a8c3e478df15d7e0ecc51
3 files changed
+34
-18
src/config.ts
+23
-17
@@ -11,23 +11,7 @@ let state:Record<string,any> = {}
11
const emitter = new EventEmitter()
12
watchLoad(argv.config || process.env.hfs_config || PATH, data => {
13
started = true
14
- for (const k in data)
15
- check(k)
16
- for (const k in { ...state, ...configProps })
17
- if (!(k in data))
18
- check(k)
19
-
20
- function check(k: string) {
21
- const oldV = state[k]
22
- const newV = data[k]
23
- const { caster, defaultValue } = configProps[k] ?? {}
24
- let v = newV === undefined ? defaultValue : newV
25
- if (caster)
26
- v = caster(v)
27
- if (JSON.stringify(v) === JSON.stringify(oldV)) return
28
- state[k] = v
29
- emitter.emit('new.'+k, v, oldV)
30
- }
14
+ setConfig(data)
15
})
16
17
const configProps:Record<string, ConfigProps> = {}
@@ -62,3 +46,25 @@ export function subscribeConfig({ k, ...definition }:{ k:string } & ConfigProps,
46
export function getConfig(k:string) {
47
return state[k]
48
}
49
+
50
+export function setConfig(newCfg: Record<string,any>) {
51
+ for (const k in newCfg)
52
+ check(k)
53
+ const oldKeys = Object.keys(state)
54
+ oldKeys.push(...Object.keys(configProps))
55
+ for (const k of oldKeys)
56
+ if (!newCfg.hasOwnProperty(k))
57
+ check(k)
58
+
59
+ function check(k: string) {
60
+ const oldV = state[k]
61
+ const newV = newCfg[k]
62
+ const { caster, defaultValue } = configProps[k] ?? {}
63
+ let v = newV === undefined ? defaultValue : newV
64
+ if (caster)
65
+ v = caster(v)
66
+ if (JSON.stringify(v) === JSON.stringify(oldV)) return
67
+ state[k] = v
68
+ emitter.emit('new.'+k, v, oldV)
69
+ }
70
+}
src/frontEndApis.ts
+10
@@ -4,6 +4,7 @@ import { PLUGINS_PUB_URI } from './const'
4
import * as api_file_list from './api.file_list'
5
import * as api_auth from './api.auth'
6
import _ from 'lodash'
7
+import { setConfig } from './config'
8
9
export const frontEndApis: ApiHandlers = {
10
@@ -19,4 +20,13 @@ export const frontEndApis: ApiHandlers = {
20
return { css, js }
21
},
22
23
+ async config({ values }, ctx) {
24
+ if (isLocalhost(ctx.ip))
25
+ setConfig(values)
26
+ },
27
+
28
+}
29
+
30
+function isLocalhost(ip: string) {
31
+ return ip === '127.0.0.1' || ip === '::1'
32
}
todo.md
+1
-1
@@ -1,5 +1,5 @@
1
# To do
2
-- api to get config
2
+- download counter
3
- update tests to SRP login
4
- anti-csrf
5
- upload