Initial commit

This commit is contained in:
Oliver 2023-08-29 19:10:54 -06:00 committed by GitHub
commit e6d6427ddc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 4378 additions and 0 deletions

View file

@ -0,0 +1,28 @@
import { Server, ServerApplicationState } from "@hapi/hapi";
import { describe } from "mocha";
import { expect } from "chai";
import { init } from "~/hapi";
describe("GET /", async () => {
let server: Server<ServerApplicationState>;
beforeEach(async () => {
server = await init();
});
afterEach(async () => {
await server.stop();
});
it("should return 204 on success", async () => {
const res = await server.inject({
method: `GET`,
url: `/`,
});
expect(res.statusCode).to.equal(204);
});
});

View file

@ -0,0 +1,9 @@
import { ServerRoute } from "@hapi/hapi";
const route: ServerRoute = {
method: `GET`, path: `/`,
handler(_, h) {
return h.response().code(204);
},
};
export default route;

34
api/src/hapi.ts Normal file
View file

@ -0,0 +1,34 @@
import { Server } from "@hapi/hapi";
import { globSync } from "glob";
import path from "path";
const server = new Server({
port: 6969,
host: `0.0.0.0`,
});
async function registerRoutes() {
let files = globSync(
path.join(`endpoints`, `**`, `!(*.spec.js|*.map)`),
{ cwd: __dirname, nodir: true }
);
for (const file of files) {
let route = (await import(path.join(__dirname, file))).default;
server.route(route);
};
};
export async function init() {
await registerRoutes();
await server.initialize();
return server;
};
export async function start() {
await registerRoutes();
await server.start();
return server;
};

35
api/src/main.ts Normal file
View file

@ -0,0 +1,35 @@
// Filepath aliasing to avoid relative-imports whenever possible, this must stay
// at the top of this file as the first statement
import "module-alias/register";
import { Logger } from "tslog";
import { start } from "~/hapi";
export const isDev = process.env.NODE_ENV?.startsWith(`dev`);
let logLevel = 4;
if (process.env.LOG_LEVEL) {
logLevel = parseInt(process.env.LOG_LEVEL);
} else if (isDev) {
logLevel = 0;
};
export const log = new Logger({
minLevel: logLevel,
hideLogPositionForProduction: !isDev,
maskValuesOfKeys: [ `password`, `token` ],
});
async function main() {
let server = await start();
log.info(`Server listening`)
};
if (require.main === module) {
process.on(`unhandledRejection`, err => {
log.error(err);
process.exit(1);
});
main();
};