MCPcopy Index your code
hub / github.com/ChatLab/ChatLab / start

Function start

apps/desktop/main/api/index.ts:50–98  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

48const PORT_FALLBACK_LIMIT = 10
49
50export async function start(): Promise<void> {
51 if (server) {
52 apiLogger.info('Server already running')
53 return
54 }
55
56 const config = cm().load()
57 cm().ensureToken(config)
58 lastError = null
59
60 const preferredPort = config.port
61 let lastErr: any
62
63 for (let offset = 0; offset < PORT_FALLBACK_LIMIT; offset++) {
64 const port = preferredPort + offset
65 try {
66 server = createServer()
67 registerSystemRoutes(server)
68 registerSessionRoutes(server)
69 registerImportRoutes(server)
70
71 await server.listen({ port, host: '127.0.0.1' })
72 startedAt = Math.floor(Date.now() / 1000)
73 runningPort = port
74 if (offset > 0) {
75 apiLogger.info(`Port ${preferredPort} in use, bound to http://127.0.0.1:${port} instead`)
76 } else {
77 apiLogger.info(`Server started on http://127.0.0.1:${port}`)
78 }
79 return
80 } catch (err: any) {
81 server = null
82 lastErr = err
83 if (err.code !== 'EADDRINUSE') break
84 apiLogger.warn(`Port ${port} is already in use, trying ${port + 1}…`)
85 }
86 }
87
88 startedAt = null
89 runningPort = null
90 if (lastErr?.code === 'EADDRINUSE') {
91 lastError = `PORT_IN_USE:${preferredPort}`
92 apiLogger.warn(`No available port found starting from ${preferredPort}`)
93 } else {
94 lastError = lastErr?.message || 'Unknown error'
95 apiLogger.error('Failed to start', lastErr)
96 }
97 throw lastErr
98}
99
100export async function stop(): Promise<void> {
101 if (!server) return

Callers 4

restartFunction · 0.85
autoStartFunction · 0.85
setEnabledFunction · 0.85
setPortFunction · 0.85

Calls 11

createServerFunction · 0.90
registerSystemRoutesFunction · 0.90
registerSessionRoutesFunction · 0.90
registerImportRoutesFunction · 0.90
cmFunction · 0.85
ensureTokenMethod · 0.80
infoMethod · 0.65
warnMethod · 0.65
errorMethod · 0.65
loadMethod · 0.45
nowMethod · 0.45

Tested by

no test coverage detected