mirror of
https://github.com/sergeyyarkov/asu_tools_service.git
synced 2026-10-11 18:39:15 +03:00
server router prefix
This commit is contained in:
parent
57e62272e9
commit
611a42a249
@ -51,7 +51,10 @@ export function createServer(options) {
|
|||||||
return {
|
return {
|
||||||
listen(port = 3000, cb) {
|
listen(port = 3000, cb) {
|
||||||
server.listen(port, cb);
|
server.listen(port, cb);
|
||||||
return { router };
|
},
|
||||||
|
router: {
|
||||||
|
prefix: router.prefix,
|
||||||
|
define: router.define
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,18 +1,23 @@
|
|||||||
import { serializeError } from "serialize-error";
|
import { serializeError } from "serialize-error";
|
||||||
import formidable from "formidable";
|
import formidable from "formidable";
|
||||||
import { BadRequestError, HttpError, MethodNotAllowedError, NotFoundError } from "./errors.js";
|
import { BadRequestError, HttpError, MethodNotAllowedError, NotFoundError } from "./errors.js";
|
||||||
|
import * as utils from "./utils/index.js";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @returns {import("./types/http-server.js").HttpRouter}
|
* @returns {import("./types/http-server.js").HttpRouter}
|
||||||
*/
|
*/
|
||||||
export function createRouter() {
|
export function createRouter() {
|
||||||
|
/** @type {import("./types/http-server.js").HttpRoute[]} */
|
||||||
|
const routes = [];
|
||||||
|
|
||||||
|
let prefixNameGroup = "";
|
||||||
|
|
||||||
return {
|
return {
|
||||||
routes: [],
|
|
||||||
async handle(ctx) {
|
async handle(ctx) {
|
||||||
const { req, res } = ctx;
|
const { req, res } = ctx;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
for (const route of this.routes) {
|
for (const route of routes) {
|
||||||
const patternResult = route.pattern.exec(req.url.pathname);
|
const patternResult = route.pattern.exec(req.url.pathname);
|
||||||
|
|
||||||
if (!patternResult) continue;
|
if (!patternResult) continue;
|
||||||
@ -69,9 +74,18 @@ export function createRouter() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
prefix(name, cb) {
|
||||||
|
const prevPrefix = prefixNameGroup;
|
||||||
|
prefixNameGroup += name;
|
||||||
|
cb(this);
|
||||||
|
prefixNameGroup = prevPrefix;
|
||||||
|
},
|
||||||
define(method, pathname, handler, options = {}) {
|
define(method, pathname, handler, options = {}) {
|
||||||
|
if (!utils.matchRoutePathname(pathname)) throw new Error(`Incorrect pathname "${pathname}"`);
|
||||||
|
if (prefixNameGroup) pathname = utils.formatRoutePathname(prefixNameGroup + pathname);
|
||||||
/** @type {import("./types/http-server.js").HttpRoute} */
|
/** @type {import("./types/http-server.js").HttpRoute} */
|
||||||
this.routes.push({ method, pattern: new URLPattern({ pathname }), handler, options });
|
routes.push({ method, pattern: new URLPattern({ pathname }), handler, options });
|
||||||
|
console.log("defined:", pathname);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
6
http-server/types/http-server.d.ts
vendored
6
http-server/types/http-server.d.ts
vendored
@ -3,7 +3,8 @@ import type IncomingForm from "formidable/Formidable.js";
|
|||||||
import formidable from "formidable";
|
import formidable from "formidable";
|
||||||
|
|
||||||
export interface HttpServer {
|
export interface HttpServer {
|
||||||
listen: (port: number, cb: () => void) => { router: HttpRouter };
|
listen: (port: number, cb: () => void) => void;
|
||||||
|
router: Pick<HttpRouter, "prefix" | "define">;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface HttpServerOptions {
|
export interface HttpServerOptions {
|
||||||
@ -26,9 +27,10 @@ export interface HttpRoute {
|
|||||||
handler: HttpRouteHandler;
|
handler: HttpRouteHandler;
|
||||||
options: HttpRouteOptions;
|
options: HttpRouteOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface HttpRouter {
|
export interface HttpRouter {
|
||||||
routes: Array<HttpRoute>;
|
|
||||||
handle: (ctx: HttpContext) => Promise<void>;
|
handle: (ctx: HttpContext) => Promise<void>;
|
||||||
|
prefix: (name: string, cb: (router: HttpRouter) => void) => void;
|
||||||
define: (
|
define: (
|
||||||
method: HttpMethod,
|
method: HttpMethod,
|
||||||
pathname: string,
|
pathname: string,
|
||||||
|
|||||||
1
http-server/utils/index.js
Normal file
1
http-server/utils/index.js
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from "./string.js";
|
||||||
14
http-server/utils/string.js
Normal file
14
http-server/utils/string.js
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
/**
|
||||||
|
* @param {string} pathname
|
||||||
|
*/
|
||||||
|
export function matchRoutePathname(pathname) {
|
||||||
|
return pathname.match(/(^\/[^/].*$|^\/$)/g);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} pathname
|
||||||
|
*/
|
||||||
|
export function formatRoutePathname(pathname) {
|
||||||
|
if (pathname[0] !== "/") pathname = "/" + pathname;
|
||||||
|
return pathname.replaceAll(/\/{2,}/g, "/");
|
||||||
|
}
|
||||||
31
index.js
31
index.js
@ -17,22 +17,27 @@ const cors = {
|
|||||||
headers: "Content-Type,Accept"
|
headers: "Content-Type,Accept"
|
||||||
};
|
};
|
||||||
|
|
||||||
const server = createServer({ cors }).listen(SERVER_PORT, async () => {
|
const server = createServer({ cors });
|
||||||
|
|
||||||
|
server.listen(SERVER_PORT, async () => {
|
||||||
console.log(`Service ${packageJson.name} is running at port ${SERVER_PORT}.`);
|
console.log(`Service ${packageJson.name} is running at port ${SERVER_PORT}.`);
|
||||||
await fsp.mkdir(path.join(process.cwd(), "/uploads"), { recursive: true });
|
await fsp.mkdir(path.join(process.cwd(), "/uploads"), { recursive: true });
|
||||||
await db.connect().then(() => console.log("Database connected!"));
|
await db.connect().then(() => console.log("Database connected!"));
|
||||||
});
|
});
|
||||||
|
|
||||||
server.router.define("GET", "/", ({ res }) => res.sendText("Сервис инструментов таблицы АСУ."));
|
server.router.prefix("/api", (router) => {
|
||||||
server.router.define("GET", "/api/reports_status_parse", reportsHandlers.statusParseHandler);
|
router.define("GET", "/reports_status_parse", reportsHandlers.statusParseHandler);
|
||||||
server.router.define("POST", "/api/reports_excel_file_parse", reportsHandlers.parseExcelHandler, {
|
router.define("POST", "/reports_excel_file_parse", reportsHandlers.parseExcelHandler, {
|
||||||
incomingForm: {
|
incomingForm: {
|
||||||
maxFields: 1,
|
maxFields: 1,
|
||||||
maxFiles: 1,
|
maxFiles: 1,
|
||||||
filename: (_, ext) => `gpp-reports${ext}`,
|
filename: (_, ext) => `gpp-reports${ext}`,
|
||||||
keepExtensions: true,
|
keepExtensions: true,
|
||||||
uploadDir: path.join(process.cwd(), "./uploads"),
|
uploadDir: path.join(process.cwd(), "./uploads"),
|
||||||
filter: ({ mimetype }) => !!mimetype && mimetype.includes("application/vnd.ms-excel")
|
filter: ({ mimetype }) => !!mimetype && mimetype.includes("application/vnd.ms-excel")
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
router.define("POST", "/reports_sync", reportsHandlers.syncParsedHandler);
|
||||||
});
|
});
|
||||||
server.router.define("POST", "/api/reports_sync", reportsHandlers.syncParsedHandler);
|
|
||||||
|
server.router.define("GET", "/", ({ res }) => res.sendText("Сервис инструментов таблицы АСУ."));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user