diff --git a/.gitignore b/.gitignore index bbb508f..04210a3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /.env *.gz +_fresh/ diff --git a/main.ts b/main.ts index 74d4f37..2a7af93 100644 --- a/main.ts +++ b/main.ts @@ -6,10 +6,14 @@ import { start } from "$fresh/server.ts"; import manifest from "~/fresh.gen.ts"; +import { initPool } from "~/server/db.ts"; +import { env } from "~/server/env.ts"; // const render: RenderFunction = (ctx, render) => { // ctx.lang = "ja"; // render(); // }; +env.init(); +initPool(); await start(manifest); diff --git a/routes/callback.tsx b/routes/callback.tsx index 36b5b37..479acf5 100644 --- a/routes/callback.tsx +++ b/routes/callback.tsx @@ -1,5 +1,5 @@ import { Handlers } from "$fresh/server.ts"; -import { clientId, clientSecret } from "~/server/env.ts"; +import { env } from "~/server/env.ts"; import { createSession, getCallbackUrl } from "~/server/auth.ts"; import { selectUserByGoogleId, transaction, updateUser, upsertUser } from "~/server/db.ts"; @@ -29,8 +29,8 @@ export const handler: Handlers = { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" }, body: new URLSearchParams([ - ["client_id", clientId], - ["client_secret", clientSecret], + ["client_id", env.clientId], + ["client_secret", env.clientSecret], ["redirect_uri", redirectUri], ["grant_type", "authorization_code"], ["code", code], diff --git a/server/auth.ts b/server/auth.ts index c18854f..f1818cb 100644 --- a/server/auth.ts +++ b/server/auth.ts @@ -1,6 +1,6 @@ import { getCookies, setCookie } from "$std/http/cookie.ts"; import { AppUser, insertSession, pool, selectSession } from "~/server/db.ts"; -import { clientId } from "~/server/env.ts"; +import { env } from "~/server/env.ts"; export type SessionType = { id: string; user: AppUser }; @@ -39,12 +39,12 @@ export function getCallbackUrl(requestUrl: string) { export function getAuthUrl(requestUrl: string): string { const redirectUri = getCallbackUrl(requestUrl); - if (!clientId) { + if (!env.clientId) { throw new Error("clientId is undefined"); } return "https://accounts.google.com/o/oauth2/auth?" + new URLSearchParams([ - ["client_id", clientId], + ["client_id", env.clientId], ["redirect_uri", redirectUri], ["scope", "https://www.googleapis.com/auth/userinfo.profile"], ["access_type", "offline"], diff --git a/server/db.ts b/server/db.ts index ad83046..7ad6b47 100644 --- a/server/db.ts +++ b/server/db.ts @@ -50,20 +50,25 @@ export type AppNotification = { name?: string; // app_user }; -const connectionPool = new Pool( - { - tls: { - enforce: false, - caCertificates: [ - `-----BEGIN CERTIFICATE-----\n${ - Deno.env.get("MDSNS_DATABASE_CA_CERTIFICATE") - }\n-----END CERTIFICATE-----`, - ], +let connectionPool: Pool; + +export function initPool() { + // build 時に処理が動かないように初期化を遅延させる + connectionPool = new Pool( + { + tls: { + enforce: false, + caCertificates: [ + `-----BEGIN CERTIFICATE-----\n${ + Deno.env.get("MDSNS_DATABASE_CA_CERTIFICATE") + }\n-----END CERTIFICATE-----`, + ], + }, }, - }, - 5, - true, -); + 5, + true, + ); +} export async function pool( handler: (client: PoolClient) => Promise, diff --git a/server/env.ts b/server/env.ts index 6508a37..5021ca5 100644 --- a/server/env.ts +++ b/server/env.ts @@ -1,4 +1,4 @@ -export function get(name: string) { +function get(name: string) { const value = Deno.env.get(name); if (!value) { console.error("Cannot get the environment variable: " + name); @@ -7,5 +7,20 @@ export function get(name: string) { return value; } -export const clientId = get("LEAVES_AUTH_CLIENT_ID"); -export const clientSecret = get("LEAVES_AUTH_CLIENT_SECRET"); +class Env { + #clientId?: string; + #clientSecret?: string; + init() { + // build 時に処理が動かないように初期化を遅延させる + this.#clientId = get("LEAVES_AUTH_CLIENT_ID"); + this.#clientSecret = get("LEAVES_AUTH_CLIENT_SECRET"); + } + get clientId() { + return this.#clientId ?? ""; + } + get clientSecret() { + return this.#clientSecret ?? ""; + } +} + +export const env = new Env();