MCPcopy Create free account
hub / github.com/MemTensor/MemOS / startHttpServer

Function startHttpServer

apps/memos-local-plugin/server/http.ts:53–118  ·  view source on GitHub ↗
(
  deps: ServerDeps,
  options: ServerOptions = {},
)

Source from the content-addressed store, hash-verified

51const IMPORT_MAX_BODY_BYTES = 64 * 1024 * 1024;
52
53export async function startHttpServer(
54 deps: ServerDeps,
55 options: ServerOptions = {},
56): Promise<ServerHandle> {
57 const log = rootLogger.child({ channel: "server.http" });
58 const host = options.host ?? "127.0.0.1";
59 const port = options.port ?? 0;
60 const extraHeaders = options.extraHeaders ?? {};
61
62 const routes = buildRoutes(deps, options);
63
64 const server = createServer(async (req, res) => {
65 for (const [k, v] of Object.entries(extraHeaders)) {
66 res.setHeader(k, v);
67 }
68 try {
69 await dispatch(req, res, routes, deps, options, log);
70 } catch (err) {
71 const msg = err instanceof Error ? err.message : String(err);
72 log.error("request.unhandled", { path: req.url, err: msg });
73 if (!res.headersSent) {
74 writeJson(res, 500, { error: { code: "internal", message: msg } });
75 }
76 try {
77 res.end();
78 } catch {
79 // best-effort — connection may already be closed
80 }
81 }
82 });
83
84 // Single bind attempt. EADDRINUSE is propagated so the caller
85 // (`bridge.cts` / `adapters/openclaw`) can log it and run headless.
86 await new Promise<void>((resolve, reject) => {
87 const onErr = (e: NodeJS.ErrnoException) => reject(e);
88 server.once("error", onErr);
89 server.listen(port, host, () => {
90 server.off("error", onErr);
91 resolve();
92 });
93 });
94
95 const addr = server.address();
96 const actualPort = typeof addr === "object" && addr ? addr.port : port;
97 const url = `http://${host === "0.0.0.0" ? "127.0.0.1" : host}:${actualPort}`;
98 let closed = false;
99
100 log.info("server.started", { url, port: actualPort });
101
102 return {
103 url,
104 port: actualPort,
105 get closed() {
106 return closed;
107 },
108 async close() {
109 if (closed) return;
110 closed = true;

Callers 5

createRuntimeFunction · 0.85
http.test.tsFile · 0.85
sse.test.tsFile · 0.85
startWithFunction · 0.85

Calls 8

buildRoutesFunction · 0.85
writeJsonFunction · 0.85
resolveFunction · 0.85
endMethod · 0.80
dispatchFunction · 0.70
childMethod · 0.65
errorMethod · 0.65
infoMethod · 0.65

Tested by 1

startWithFunction · 0.68