Skip to content

Commit

Permalink
feat: run(appFn, { env })
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Dec 8, 2020
1 parent 5c2d3c3 commit 3d90806
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 31 deletions.
34 changes: 17 additions & 17 deletions src/bin/read-env-options.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { getPrivateKey } from "@probot/get-private-key";
import { Options as PinoOptions, LogLevel } from "@probot/pino";

export function readEnvOptions() {
const privateKey = getPrivateKey();
export function readEnvOptions(
env: Record<string, string | undefined> = process.env
) {
const privateKey = getPrivateKey({ env });

return {
args: [],
privateKey: (privateKey && privateKey.toString()) || undefined,
id: Number(process.env.APP_ID),
port: Number(process.env.PORT) || 3000,
host: process.env.HOST,
secret: process.env.WEBHOOK_SECRET,
webhookPath: process.env.WEBHOOK_PATH,
webhookProxy: process.env.WEBHOOK_PROXY_URL,
logLevel: process.env.LOG_LEVEL as LogLevel,
logFormat: process.env.LOG_FORMAT as PinoOptions["logFormat"],
logLevelInString: process.env.LOG_LEVEL_IN_STRING === "true",
sentryDsn: process.env.SENTRY_DSN,
redisConfig: process.env.REDIS_URL,
baseUrl: process.env.GHE_HOST
? `${process.env.GHE_PROTOCOL || "https"}://${
process.env.GHE_HOST
}/api/v3`
id: Number(env.APP_ID),
port: Number(env.PORT) || 3000,
host: env.HOST,
secret: env.WEBHOOK_SECRET,
webhookPath: env.WEBHOOK_PATH,
webhookProxy: env.WEBHOOK_PROXY_URL,
logLevel: env.LOG_LEVEL as LogLevel,
logFormat: env.LOG_FORMAT as PinoOptions["logFormat"],
logLevelInString: env.LOG_LEVEL_IN_STRING === "true",
sentryDsn: env.SENTRY_DSN,
redisConfig: env.REDIS_URL,
baseUrl: env.GHE_HOST
? `${env.GHE_PROTOCOL || "https"}://${env.GHE_HOST}/api/v3`
: "https://api.github.com",
};
}
17 changes: 13 additions & 4 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ import { Server, ServerOptions } from "./server/server";
import { load } from "./load";
import { defaultApp } from "./apps/default";

type AdditionalOptions = {
env: Record<string, string | undefined>;
};

/**
*
* @param appFnOrArgv set to either a probot application function: `({ app }) => { ... }` or to process.argv
*/
export async function run(appFnOrArgv: ApplicationFunction | string[]) {
export async function run(
appFnOrArgv: ApplicationFunction | string[],
additionalOptions?: AdditionalOptions
) {
const {
// log options
logLevel: level,
Expand All @@ -42,7 +49,7 @@ export async function run(appFnOrArgv: ApplicationFunction | string[]) {
args,
} = Array.isArray(appFnOrArgv)
? readCliOptions(appFnOrArgv)
: readEnvOptions();
: readEnvOptions(additionalOptions?.env);

const logOptions: GetLogOptions = {
level,
Expand Down Expand Up @@ -103,10 +110,12 @@ export async function run(appFnOrArgv: ApplicationFunction | string[]) {
const [appFn] = args;

load(probot, router, appFn);
server.app.use(probot.webhooks.middleware);

server.app.use(webhookPath ? webhookPath : "/", probot.webhooks.middleware);
} else {
load(probot, router, appFnOrArgv);
server.app.use(probot.webhooks.middleware);

server.app.use(webhookPath ? webhookPath : "/", probot.webhooks.middleware);
}
await server.start();

Expand Down
20 changes: 10 additions & 10 deletions src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,18 @@ export class Server {
resolve(server);
}
);
})) as HttpServer;

this.state.httpServer.on("error", (error: NodeJS.ErrnoException) => {
if (error.code === "EADDRINUSE") {
this.log.error(
`Port ${port} is already in use. You can define the PORT environment variable to use a different port.`
);
} else {
server.on("error", (error: NodeJS.ErrnoException) => {
if (error.code === "EADDRINUSE") {
error = Object.assign(error, {
message: `Port ${port} is already in use. You can define the PORT environment variable to use a different port.`,
});
}

this.log.error(error);
}
process.exit(1);
});
reject(error);
});
})) as HttpServer;

return this.state.httpServer;
}
Expand Down

0 comments on commit 3d90806

Please sign in to comment.