浏览代码

fix(home-server): pass the log stream callback before loggers are created

Karol Sójko 2 年之前
父节点
当前提交
1ca70c1e50

+ 12 - 15
packages/home-server/bin/server.ts

@@ -2,18 +2,15 @@ import { HomeServer } from '../src/Server/HomeServer'
 
 
 const homeServer = new HomeServer()
 const homeServer = new HomeServer()
 
 
-Promise.resolve(homeServer.start({ dataDirectoryPath: `${__dirname}/../data` }))
-  .then(() => {
-    const logStream = homeServer.logs()
-
-    if (logStream !== undefined) {
-      logStream.on('data', (chunk: Buffer) => {
-        // eslint-disable-next-line no-console
-        console.log(chunk.toString())
-      })
-    }
-  })
-  .catch((error) => {
-    // eslint-disable-next-line no-console
-    console.log(`Could not start server: ${error.message}`)
-  })
+Promise.resolve(
+  homeServer.start({
+    dataDirectoryPath: `${__dirname}/../data`,
+    logStreamCallback: (chunk: Buffer) => {
+      // eslint-disable-next-line no-console
+      console.log(chunk.toString())
+    },
+  }),
+).catch((error) => {
+  // eslint-disable-next-line no-console
+  console.log(`Could not start server: ${error.message}`)
+})

+ 10 - 6
packages/home-server/src/Server/HomeServer.ts

@@ -47,7 +47,7 @@ export class HomeServer implements HomeServerInterface {
       const env: Env = new Env(environmentOverrides)
       const env: Env = new Env(environmentOverrides)
       env.load()
       env.load()
 
 
-      this.configureLoggers(env)
+      this.configureLoggers(env, configuration)
 
 
       const apiGatewayService = new ApiGatewayService(serviceContainer)
       const apiGatewayService = new ApiGatewayService(serviceContainer)
       const authService = new AuthService(serviceContainer, controllerContainer, directCallDomainEventPublisher)
       const authService = new AuthService(serviceContainer, controllerContainer, directCallDomainEventPublisher)
@@ -170,6 +170,10 @@ export class HomeServer implements HomeServerInterface {
 
 
       this.serverInstance = undefined
       this.serverInstance = undefined
 
 
+      if (this.logStream) {
+        this.logStream.end()
+      }
+
       return Result.ok('Server stopped.')
       return Result.ok('Server stopped.')
     } catch (error) {
     } catch (error) {
       return Result.fail((error as Error).message)
       return Result.fail((error as Error).message)
@@ -192,13 +196,13 @@ export class HomeServer implements HomeServerInterface {
     return this.authService.activatePremiumFeatures(username)
     return this.authService.activatePremiumFeatures(username)
   }
   }
 
 
-  logs(): NodeJS.ReadableStream | undefined {
-    return this.logStream
-  }
-
-  private configureLoggers(env: Env): void {
+  private configureLoggers(env: Env, configuration: HomeServerConfiguration): void {
     this.logStream = new PassThrough()
     this.logStream = new PassThrough()
 
 
+    if (configuration.logStreamCallback) {
+      this.logStream.on('data', configuration.logStreamCallback)
+    }
+
     const winstonFormatters = [winston.format.splat(), winston.format.json()]
     const winstonFormatters = [winston.format.splat(), winston.format.json()]
 
 
     const level = env.get('LOG_LEVEL', true) || 'info'
     const level = env.get('LOG_LEVEL', true) || 'info'

+ 1 - 0
packages/home-server/src/Server/HomeServerConfiguration.ts

@@ -1,4 +1,5 @@
 export interface HomeServerConfiguration {
 export interface HomeServerConfiguration {
   dataDirectoryPath: string
   dataDirectoryPath: string
   environment?: { [name: string]: string }
   environment?: { [name: string]: string }
+  logStreamCallback?: (chunk: Buffer) => void
 }
 }

+ 0 - 1
packages/home-server/src/Server/HomeServerInterface.ts

@@ -6,5 +6,4 @@ export interface HomeServerInterface {
   activatePremiumFeatures(username: string): Promise<Result<string>>
   activatePremiumFeatures(username: string): Promise<Result<string>>
   stop(): Promise<Result<string>>
   stop(): Promise<Result<string>>
   isRunning(): Promise<boolean>
   isRunning(): Promise<boolean>
-  logs(): NodeJS.ReadableStream | undefined
 }
 }