MCPcopy
hub / github.com/modstart-lib/aigcpanel / createApp

Function createApp

electron/mapi/httpserver/main.ts:194–742  ·  view source on GitHub ↗
(port: number, token: string)

Source from the content-addressed store, hash-verified

192};
193
194const createApp = (port: number, token: string) => {
195 const app = express();
196 app.use(express.json());
197 app.use((_req, res, next) => {
198 res.setHeader("Access-Control-Allow-Origin", "*");
199 res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
200 res.setHeader(
201 "Access-Control-Allow-Headers",
202 "Content-Type, Authorization",
203 );
204 if (_req.method === "OPTIONS") {
205 res.status(200).end();
206 return;
207 }
208 next();
209 });
210
211 app.get("/doc", (_req, res) => {
212 const html = docHtml.replace(/\{\{PORT\}\}/g, String(port));
213 res.status(200)
214 .set("Content-Type", "text/html; charset=utf-8")
215 .send(html);
216 });
217
218 app.use((_req, res, next) => {
219 const auth = _req.headers["authorization"] || "";
220 if (!auth.startsWith("Bearer ") || auth.slice(7) !== token) {
221 sendJson(res, 401, { code: -1, msg: "Unauthorized" });
222 return;
223 }
224 next();
225 });
226
227 app.post("/api/model/list", async (_req, res) => {
228 try {
229 const servers = await getInstalledServers();
230 sendJson(res, 200, { code: 0, data: servers });
231 } catch (e) {
232 sendJson(res, 500, { code: -1, msg: String(e) });
233 }
234 });
235
236 app.post("/api/model/call", async (req, res) => {
237 try {
238 const {
239 model,
240 version,
241 function: funcName,
242 param,
243 } = req.body || {};
244 let serverName: string;
245 let serverVersion: string;
246 if (version !== undefined) {
247 // 新格式:model 和 version 分别传递
248 serverName = model || "";
249 serverVersion = version || "";
250 } else {
251 // 兼容旧格式:model 为 "name|version"

Callers 10

startFunction · 0.85
main.tsFile · 0.85
guide.tsFile · 0.85
feedback.tsFile · 0.85
payment.tsFile · 0.85
user.tsFile · 0.85
about.tsFile · 0.85
setup.tsFile · 0.85
log.tsFile · 0.85
monitor.tsFile · 0.85

Calls 8

sendJsonFunction · 0.85
getInstalledServersFunction · 0.85
getServerRecordFunction · 0.85
buildModelConfigFunction · 0.85
buildTaskParamFunction · 0.85
buildTaskTitleFunction · 0.85
queryOnceFunction · 0.85
splitMethod · 0.80

Tested by

no test coverage detected