Skip to content

Commit

Permalink
controllers (#22)
Browse files Browse the repository at this point in the history
* controllers

* lint / format
  • Loading branch information
justinmchase authored Mar 6, 2022
1 parent 64f0bfe commit 1b13802
Show file tree
Hide file tree
Showing 49 changed files with 1,191 additions and 1,359 deletions.
4 changes: 2 additions & 2 deletions deps/github.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "https://deno.land/x/[email protected]/jwt.ts"
export * from "https://deno.land/x/[email protected]/request.ts"
export * from "https://deno.land/x/[email protected]/jwt.ts";
export * from "https://deno.land/x/[email protected]/request.ts";
1 change: 1 addition & 0 deletions deps/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "https://deno.land/[email protected]/path/mod.ts";
2 changes: 1 addition & 1 deletion src/controllers/context.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export interface IContext {
secret?: string
isHtml?: boolean;
}
21 changes: 21 additions & 0 deletions src/controllers/error.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Application, Context, Status } from "../../deps/oak.ts";
import { IContext } from "./context.ts";
import { Controller } from "./controller.ts";

export class ErrorController extends Controller {
public async use(app: Application): Promise<void> {
app.use(this.handler.bind(this));
await undefined;
}

private async handler(ctx: Context<IContext>, next: () => Promise<unknown>) {
try {
await next();
} catch (err) {
console.log({ ...err }, err);
ctx.response.status = err.status ?? Status.InternalServerError;
ctx.response.body = { ok: false, message: err.message };
ctx.response.headers.set("Content-Type", "application/json");
}
}
}
24 changes: 24 additions & 0 deletions src/controllers/log.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Application, Context } from "../../deps/oak.ts";
import { Controller } from "./controller.ts";

export class LogController extends Controller {
public async use(app: Application): Promise<void> {
app.use(this.handler.bind(this));
await undefined;
}

private async handler(ctx: Context, next: () => Promise<unknown>) {
const start = new Date().valueOf();
const { request: { ip, hasBody, method, url } } = ctx;
try {
await next();
} finally {
const end = new Date().valueOf();
const t = `${end - start}ms`;
const { response: { status } } = ctx;
console.log(
`${status} ${method} ${url} ${JSON.stringify({ t, ip, hasBody })}`,
);
}
}
}
26 changes: 22 additions & 4 deletions src/controllers/mod.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import { Application } from '../../deps/oak.ts';
import { Application } from "../../deps/oak.ts";
import { IContext } from "./context.ts";
import { Managers } from "../managers/mod.ts";
import { Services } from "../services/mod.ts";
import { ErrorController } from "./error.controller.ts";
import { LogController } from "./log.controller.ts";
import { ParseController } from "./parse.controller.ts";
import { WebhookController } from "./webhook.controller.ts";
import { NotFoundController } from "./notfound.controller.ts";

export async function initControllers(app: Application, managers: Managers, services: Services) {
const { installations, interactions } = managers
const { github } = services
export async function initControllers(
app: Application<IContext>,
managers: Managers,
services: Services,
) {
const { installations, interactions } = managers;
const { github } = services;
const error = new ErrorController();
const log = new LogController();
const parse = new ParseController();
const webhook = new WebhookController(installations, interactions, github);
const notfound = new NotFoundController();

await error.use(app);
await log.use(app);
await parse.use(app);
await webhook.use(app);
await notfound.use(app);
}
21 changes: 21 additions & 0 deletions src/controllers/notfound.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Application, Context } from "../../deps/oak.ts";
import { IContext } from "./context.ts";
import { Controller } from "./controller.ts";
import { NotFoundError } from "../errors/mod.ts";

export class NotFoundController extends Controller {
public async use(app: Application<IContext>): Promise<void> {
app.use(this.handler.bind(this));
await undefined;
}

private async handler(ctx: Context<IContext>) {
if (ctx.request.method === "GET" && ctx.state.isHtml === true) {
ctx.response.redirect("https://justinmchase.github.io/commit-karma/");
} else {
throw new NotFoundError("url", ctx.request.url.toString());
}

await undefined;
}
}
22 changes: 22 additions & 0 deletions src/controllers/parse.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { accepts, Application, Request } from "../../deps/oak.ts";
import { IContext } from "./context.ts";
import { Controller } from "./controller.ts";

export class ParseController extends Controller {
public async use(app: Application<IContext>): Promise<void> {
app.use((ctx, next) => this.handler(ctx.request, ctx.state, next));
await true;
}

private async handler(
req: Request,
context: IContext,
next: () => Promise<unknown>,
) {
// If requesting from a browser, this will be true. If requesting from curl or an app
// then request either */* or application/* and the true content type will be added to the header.
const isHtml = accepts(req, "application/*", "text/html") === "text/html";
context.isHtml = isHtml;
return await next();
}
}
Loading

0 comments on commit 1b13802

Please sign in to comment.