-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
49 changed files
with
1,191 additions
and
1,359 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "https://deno.land/[email protected]/path/mod.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export interface IContext { | ||
secret?: string | ||
isHtml?: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 })}`, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
Oops, something went wrong.