Organize directories
This commit is contained in:
parent
8a91c9b89b
commit
4898382f78
433 changed files with 0 additions and 0 deletions
23
packages/metamigo-common/plugins/config.ts
Normal file
23
packages/metamigo-common/plugins/config.ts
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
import { Server } from "@hapi/hapi";
|
||||
import cloneDeep from "lodash/cloneDeep";
|
||||
import { deepFreeze } from "../helpers";
|
||||
|
||||
interface ConfigOptions {
|
||||
config: unknown;
|
||||
}
|
||||
|
||||
const register = async (
|
||||
server: Server,
|
||||
options: ConfigOptions
|
||||
): Promise<void> => {
|
||||
const safeConfig = deepFreeze(cloneDeep(options.config));
|
||||
server.decorate("server", "config", () => safeConfig);
|
||||
};
|
||||
|
||||
const ConfigPlugin = {
|
||||
register,
|
||||
name: "config",
|
||||
version: "0.0.1",
|
||||
};
|
||||
|
||||
export default ConfigPlugin;
|
||||
37
packages/metamigo-common/plugins/request-id.ts
Normal file
37
packages/metamigo-common/plugins/request-id.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import { Server } from "@hapi/hapi";
|
||||
import { v4 as uuid } from "uuid";
|
||||
|
||||
interface RequestIdOptions {
|
||||
header?: string;
|
||||
}
|
||||
|
||||
const register = async (
|
||||
server: Server,
|
||||
options?: RequestIdOptions
|
||||
): Promise<void> => {
|
||||
const header = options?.header || "x-request-id";
|
||||
server.ext("onPreResponse", async (request, h) => {
|
||||
if (!request.response) {
|
||||
return h.continue;
|
||||
}
|
||||
|
||||
if ("isBoom" in request.response) {
|
||||
const id = request.response.output.headers[header] || uuid();
|
||||
request.response.output.headers[header] = id;
|
||||
} else {
|
||||
const id = request.headers[header] || uuid();
|
||||
// @ts-ignore
|
||||
request.response.header(header, id);
|
||||
}
|
||||
|
||||
return h.continue;
|
||||
});
|
||||
};
|
||||
|
||||
const RequestIdPlugin = {
|
||||
register,
|
||||
name: "request-id",
|
||||
version: "0.0.1",
|
||||
};
|
||||
|
||||
export default RequestIdPlugin;
|
||||
60
packages/metamigo-common/plugins/status.ts
Normal file
60
packages/metamigo-common/plugins/status.ts
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import { Server, RouteOptionsAccess } from "@hapi/hapi";
|
||||
import { Prometheus } from "@promster/hapi";
|
||||
|
||||
interface StatusOptions {
|
||||
path?: string;
|
||||
auth?: RouteOptionsAccess;
|
||||
}
|
||||
|
||||
const count = (statusCounter: any) => async () => {
|
||||
statusCounter.inc();
|
||||
return "Incremented metamigo_status_test counter";
|
||||
};
|
||||
|
||||
const ping = async () => "OK";
|
||||
|
||||
const statusRoutes = (server: Server, opt?: StatusOptions) => {
|
||||
const path = opt?.path || "/status";
|
||||
const statusCounter = new Prometheus.Counter({
|
||||
name: "metamigo_status_test",
|
||||
help: "Test counter",
|
||||
});
|
||||
|
||||
return [
|
||||
{
|
||||
method: "GET",
|
||||
path: `${path}/ping`,
|
||||
handler: ping,
|
||||
options: {
|
||||
auth: opt?.auth,
|
||||
tags: ["api", "status", "ping"],
|
||||
description: "Returns 200 and OK as the response.",
|
||||
},
|
||||
},
|
||||
{
|
||||
method: "GET",
|
||||
path: `${path}/inc`,
|
||||
handler: count(statusCounter),
|
||||
options: {
|
||||
auth: opt?.auth,
|
||||
tags: ["api", "status", "prometheus"],
|
||||
description: "Increments a test counter, for testing prometheus.",
|
||||
},
|
||||
},
|
||||
];
|
||||
};
|
||||
|
||||
const register = async (
|
||||
server: Server,
|
||||
options: StatusOptions
|
||||
): Promise<void> => {
|
||||
server.route(statusRoutes(server, options));
|
||||
};
|
||||
|
||||
const StatusPlugin = {
|
||||
register,
|
||||
name: "status",
|
||||
version: "0.0.1",
|
||||
};
|
||||
|
||||
export default StatusPlugin;
|
||||
Loading…
Add table
Add a link
Reference in a new issue