Skip to content

Commit 6599095

Browse files
authored
better environment variables handling (#68)
1 parent 6bf6ddc commit 6599095

File tree

9 files changed

+53
-14
lines changed

9 files changed

+53
-14
lines changed

.env.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ SERVER_PORT=4000 # optional, server port (alias: PORT, PUBLIC_SERVER_URL.port) (
77
PUBLIC_X_ANON_KEY=...
88
X_API_KEY=...
99

10-
DATABASE_URL=file:local.db
10+
DATABASE_URL=file:../../local.db
1111

1212
BETTER_AUTH_SECRET=...
1313
BETTER_AUTH_URL=http://localhost:4000

docs/developer_readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ bun install --frozen-lockfile
1010
## 開発
1111

1212
```bash
13-
# データベースを作成するには、packages/server に移動してから以下のコマンドを実行してください
14-
bunx drizzle-kit push
13+
# データベースを作成するには、 `.env` で `DATABASE_URL` を設定し、以下のコマンドを実行してください
14+
bun db push
1515
```
1616

1717
```bash

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"scripts": {
99
"dev": "bun --filter=@packages/{web,server} dev",
1010
"dev:mock": "bun --filter=@packages/{web,server} dev:mock",
11+
"db": "cd packages/server; bun --env-file=../../.env drizzle-kit",
1112
"storybook": "concurrently 'cd packages/server; bun dev' 'cd packages/web; bun run storybook'",
1213
"build": "cd packages/web && bun run build",
1314
"check": "bunx biome check .",

packages/server/drizzle.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { defineConfig } from "drizzle-kit";
2+
import { env } from "./lib/env.ts";
23

34
export default defineConfig({
45
out: "./drizzle",
56
schema: "./db/schema.ts",
67
dialect: "sqlite",
78
dbCredentials: {
8-
url: process.env.DATABASE_URL ?? "file:local.db",
9+
url: env.DATABASE_URL,
910
},
1011
});

packages/server/lib/auth.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { drizzleAdapter } from "better-auth/adapters/drizzle";
33
import Elysia from "elysia";
44
import { db } from "../db/index.ts";
55
import * as schema from "../db/schema.ts";
6+
import { env } from "../lib/env.ts";
67

78
export const auth = betterAuth({
89
database: drizzleAdapter(db, {
@@ -11,11 +12,11 @@ export const auth = betterAuth({
1112
}),
1213
socialProviders: {
1314
google: {
14-
clientId: process.env.GOOGLE_CLIENT_ID as string,
15-
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
15+
clientId: env.GOOGLE_CLIENT_ID,
16+
clientSecret: env.GOOGLE_CLIENT_SECRET,
1617
},
1718
},
18-
trustedOrigins: [process.env.PUBLIC_WEB_URL ?? "http://localhost:3000"],
19+
trustedOrigins: [env.PUBLIC_WEB_URL],
1920
});
2021

2122
const betterAuthMacro = new Elysia({ name: "better-auth" })

packages/server/lib/env.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import * as e from "./utils/environment.ts";
2+
3+
export const env = {
4+
DATABASE_URL: e.string("DATABASE_URL"),
5+
PUBLIC_WEB_URL: e.string("PUBLIC_WEB_URL"),
6+
GOOGLE_CLIENT_ID: e.string("GOOGLE_CLIENT_ID"),
7+
GOOGLE_CLIENT_SECRET: e.string("GOOGLE_CLIENT_SECRET"),
8+
9+
SERVER_PORT: e.string_optional("SERVER_PORT"),
10+
PORT: e.string_optional("PORT"),
11+
PUBLIC_SERVER_URL: e.string_optional("PUBLIC_SERVER_URL"),
12+
13+
PUBLIC_MOCK_DATA: e.boolean("PUBLIC_MOCK_DATA"),
14+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { panic } from "./panic.ts";
2+
3+
export function string(name: string) {
4+
return (
5+
string_optional(name) ?? panic(`Environment variable ${name} not found`)
6+
);
7+
}
8+
export function string_optional(name: string): string | undefined {
9+
return process.env[name];
10+
}
11+
export function boolean(name: string, fallback?: boolean): boolean {
12+
const val = string_optional(name);
13+
if (val === "1" || val === "true") {
14+
return true;
15+
}
16+
if (val === "0" || val === "false") {
17+
return false;
18+
}
19+
if (fallback !== undefined) return fallback;
20+
panic(`Environment variable ${name} not found`);
21+
}

packages/server/lib/utils/panic.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function panic(message: string): never {
2+
throw new Error(message);
3+
}

packages/server/serve.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import { app } from "./app.ts";
2+
import { env } from "./lib/env.ts";
23

34
const port =
4-
process.env.SERVER_PORT ??
5-
process.env.PORT ??
6-
parsePublicServerURL() ??
7-
"4000";
5+
env.SERVER_PORT ?? env.PORT ?? parsePort(env.PUBLIC_SERVER_URL) ?? "4000";
86

97
app.listen(port, () =>
108
console.log(`Server started at http://localhost:${port}`),
119
);
1210

13-
function parsePublicServerURL() {
14-
if (!process.env.PUBLIC_SERVER_URL) return undefined;
15-
const url = new URL(process.env.PUBLIC_SERVER_URL);
11+
function parsePort(urlstr: string | undefined) {
12+
if (!urlstr) return undefined;
13+
const url = new URL(urlstr);
1614
return url.port;
1715
}

0 commit comments

Comments
 (0)