@@ -3,6 +3,7 @@ import { pathToFileURL } from 'node:url';
33import OpenAI from 'openai' ;
44import type { FetchPort } from '../application/ports/output/FetchPort' ;
55import type { LlmPort } from '../application/ports/output/LlmPort' ;
6+ import type { LoggerPort } from '../application/ports/output/LoggerPort' ;
67import type { PersistencePort } from '../application/ports/output/PersistencePort' ;
78import { makeReportingAgentService } from '../application/usecases/agent/makeReportingAgentService' ;
89import { makeSnapshotQueryService } from '../application/usecases/queries/makeSnapshotQueryService' ;
@@ -12,13 +13,29 @@ import { NodeFetchAdapter } from '../infrastructure/fetch/NodeFetchAdapter';
1213import type { RedditCredentials } from '../infrastructure/items/redditAuth' ;
1314import { RedditItemsAdapter } from '../infrastructure/items/RedditItemsAdapter' ;
1415import { OpenAIAdapter } from '../infrastructure/llm/OpenAIAdapter' ;
16+ import { makeLogger } from '../infrastructure/logging/root' ;
1517import { PostgresAdapter } from '../infrastructure/persistence/PostgresAdapter' ;
1618import { makeReportController } from '../interface/web/ReportController' ;
1719
20+ const rootLogger = makeLogger ( ) ;
21+
22+ process . on ( 'unhandledRejection' , ( reason ) => {
23+ rootLogger . error ( 'Unhandled rejection' , { error : reason } ) ;
24+ process . exit ( 1 ) ;
25+ } ) ;
26+
27+ process . on ( 'uncaughtException' , ( err ) => {
28+ rootLogger . error ( 'Uncaught exception' , { error : err } ) ;
29+ process . exit ( 1 ) ;
30+ } ) ;
31+
1832type Deps = {
33+ logger : LoggerPort ;
1934 port : number ;
35+
2036 fetcher : FetchPort ;
2137 persistence : PersistencePort ;
38+
2239 llm : LlmPort ;
2340 redditUrl : string ;
2441 redditCreds : RedditCredentials ;
@@ -33,19 +50,24 @@ export function buildServer(deps: Deps) {
3350
3451 const agent = makeReportingAgentService ( provider , deps . llm , deps . persistence ) ;
3552 const query = makeSnapshotQueryService ( deps . persistence ) ;
36- const app = makeReportController ( agent , query ) ;
53+ const app = makeReportController (
54+ deps . logger . child ( { module : 'http' } ) ,
55+ agent ,
56+ query ,
57+ ) ;
3758 return { app, port : deps . port } ;
3859}
3960
40- export function depsFromConfig ( config : ReportingAgentConfig ) : Deps {
61+ export function buildDeps ( config : ReportingAgentConfig ) : Deps {
4162 const { port, databaseUrl, openaiApiKey, reddit } = config ;
4263
4364 return {
44- redditUrl : reddit . url ,
65+ logger : rootLogger ,
4566 port,
4667 fetcher : new NodeFetchAdapter ( globalThis . fetch ) ,
4768 persistence : new PostgresAdapter ( databaseUrl ) ,
4869 llm : new OpenAIAdapter ( new OpenAI ( { apiKey : openaiApiKey } ) ) ,
70+ redditUrl : reddit . url ,
4971 redditCreds : {
5072 clientId : reddit . clientId ,
5173 clientSecret : reddit . clientSecret ,
@@ -57,11 +79,11 @@ export function depsFromConfig(config: ReportingAgentConfig): Deps {
5779
5880export function runHttpServer ( ) {
5981 const config = loadReportingAgentConfig ( ) ;
60- const { app, port } = buildServer ( depsFromConfig ( config ) ) ;
82+ const deps = buildDeps ( config ) ;
83+ const { app, port } = buildServer ( deps ) ;
6184
62- return app . listen ( port , ( ) => {
63- console . log ( `→ http://localhost:${ port } ` ) ;
64- } ) ;
85+ const httpLogger = rootLogger . child ( { module : 'http' } ) ;
86+ return app . listen ( port , ( ) => httpLogger . info ( 'Server listening' , { port } ) ) ;
6587}
6688
6789const entryUrl = process . argv [ 1 ]
@@ -72,7 +94,7 @@ if (import.meta.url === entryUrl) {
7294 try {
7395 runHttpServer ( ) ;
7496 } catch ( err ) {
75- console . error ( 'ReportingAgentService run failed:' , err ) ;
97+ rootLogger . error ( 'ReportingAgentService run failed:' , { error : err } ) ;
7698 process . exit ( 1 ) ;
7799 }
78100}
0 commit comments