open browser on admin interface at start
Massimo Melina committed
Feb 2, 2022 at 01:13 UTC
fc0ac8738678ead780601f0c6f4f4239f942182c
3 files changed
+24
-13
src/config.ts
+10
@@ -91,3 +91,13 @@ export const saveConfigAsap = _.debounce(async () => {
91
fs.writeFile(path, yaml.stringify(state))
92
.catch(err => console.error('Failed at saving config file, please ensure it is writable.', String(err)))
93
})
94
+
95
+// async version of getConfig, allowing you to wait for onfig to be ready
96
+export async function getConfigReady(k: string, definition?: object) {
97
+ return new Promise(resolve => {
98
+ const off = subscribeConfig({ k, ...definition }, v => {
99
+ off?.()
100
+ resolve(v)
101
+ })
102
+ })
103
+}
src/index.ts
+14
-7
@@ -11,6 +11,8 @@ import { headRequests, gzipper, sessions, frontendAndSharedFiles } from './middl
11
import './listen'
12
import { serveAdminFiles } from './serveFrontend'
13
import { adminApis } from './adminApis'
14
+import { getConfigReady } from './config'
15
+import open from 'open'
16
17
export const BUILD_TIMESTAMP = "-"
18
export const SESSION_DURATION = 30*60_000
@@ -19,13 +21,18 @@ export const HFS_STARTED = new Date()
21
console.log('started', HFS_STARTED.toLocaleString(), 'build', BUILD_TIMESTAMP, DEV)
22
console.debug('cwd', process.cwd())
23
22
-const ADMIN_PORT = 63636
23
-new Koa()
24
- .use(mount(API_URI, apiMiddleware(adminApis)))
25
- .use(serveAdminFiles)
26
- .on('error', errorHandler)
27
- .listen(ADMIN_PORT, '127.0.0.1', () =>
28
- console.log('admin interface on http://localhost:'+ADMIN_PORT))
24
+getConfigReady('open_browser_at_start', { defaultValue: true }).then(config => {
25
+ const ADMIN_PORT = 63636
26
+ new Koa()
27
+ .use(mount(API_URI, apiMiddleware(adminApis)))
28
+ .use(serveAdminFiles)
29
+ .on('error', errorHandler)
30
+ .listen(ADMIN_PORT, '127.0.0.1', () => {
31
+ if (config !== false)
32
+ open('http://localhost:' + ADMIN_PORT).then()
33
+ console.log('admin interface on http://localhost:' + ADMIN_PORT)
34
+ })
35
+})
36
37
export const app = new Koa({ keys: ['hfs-keys-test'] })
38
app.use(sessions(app))
src/listen.ts
-6
@@ -1,12 +1,10 @@
1
import * as http from 'http'
2
import { getConfig, subscribeConfig } from './config'
3
-import open from 'open'
3
import { app } from './index'
4
import * as https from 'https'
5
import { watchLoad } from './watchLoad'
6
import { networkInterfaces } from 'os';
7
9
-let firstTime = true
8
let httpSrv: http.Server
9
let httpsSrv: http.Server
10
let cert:string, key: string
@@ -89,10 +87,6 @@ function startServer(srv: http.Server, port: number, secure:string='') {
87
}
88
}
89
92
- if (firstTime && getConfig('open_browser_at_start') !== false) {
93
- open(proto + '://localhost:' + port).then()
94
- firstTime = false
95
- }
90
resolve(null)
91
}).on('error', e => {
92
const { code } = e as any