Skip to content

Commit

Permalink
actions
Browse files Browse the repository at this point in the history
  • Loading branch information
chibat committed Aug 17, 2023
1 parent 4eedf62 commit b8b77ac
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.env
*.gz
_fresh/
4 changes: 4 additions & 0 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
6 changes: 3 additions & 3 deletions routes/callback.tsx
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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],
Expand Down
6 changes: 3 additions & 3 deletions server/auth.ts
Original file line number Diff line number Diff line change
@@ -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 };

Expand Down Expand Up @@ -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"],
Expand Down
31 changes: 18 additions & 13 deletions server/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
handler: (client: PoolClient) => Promise<T>,
Expand Down
21 changes: 18 additions & 3 deletions server/env.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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();

0 comments on commit b8b77ac

Please sign in to comment.