Initial commit
This commit is contained in:
commit
e6d6427ddc
29 changed files with 4378 additions and 0 deletions
28
api/src/endpoints/ping.spec.ts
Normal file
28
api/src/endpoints/ping.spec.ts
Normal 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);
|
||||
});
|
||||
});
|
||||
9
api/src/endpoints/ping.ts
Normal file
9
api/src/endpoints/ping.ts
Normal 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
34
api/src/hapi.ts
Normal 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
35
api/src/main.ts
Normal 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();
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue