Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ SERVER_PORT=4000 # optional, server port (alias: PORT, PUBLIC_SERVER_URL.port) (
PUBLIC_X_ANON_KEY=...
X_API_KEY=...

DATABASE_URL=file:local.db
DATABASE_URL=file:../../local.db

BETTER_AUTH_SECRET=...
BETTER_AUTH_URL=http://localhost:4000
Expand Down
4 changes: 2 additions & 2 deletions docs/developer_readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ bun install --frozen-lockfile
## 開発

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

```bash
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"scripts": {
"dev": "bun --filter=@packages/{web,server} dev",
"dev:mock": "bun --filter=@packages/{web,server} dev:mock",
"db": "cd packages/server; bun --env-file=../../.env drizzle-kit",
"storybook": "concurrently 'cd packages/server; bun dev' 'cd packages/web; bun run storybook'",
"build": "cd packages/web && bun run build",
"check": "bunx biome check .",
Expand Down
3 changes: 2 additions & 1 deletion packages/server/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { defineConfig } from "drizzle-kit";
import { env } from "./lib/env.ts";

export default defineConfig({
out: "./drizzle",
schema: "./db/schema.ts",
dialect: "sqlite",
dbCredentials: {
url: process.env.DATABASE_URL ?? "file:local.db",
url: env.DATABASE_URL,
},
});
7 changes: 4 additions & 3 deletions packages/server/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { drizzleAdapter } from "better-auth/adapters/drizzle";
import Elysia from "elysia";
import { db } from "../db/index.ts";
import * as schema from "../db/schema.ts";
import { env } from "../lib/env.ts";

export const auth = betterAuth({
database: drizzleAdapter(db, {
Expand All @@ -11,11 +12,11 @@ export const auth = betterAuth({
}),
socialProviders: {
google: {
clientId: process.env.GOOGLE_CLIENT_ID as string,
clientSecret: process.env.GOOGLE_CLIENT_SECRET as string,
clientId: env.GOOGLE_CLIENT_ID,
clientSecret: env.GOOGLE_CLIENT_SECRET,
},
},
trustedOrigins: [process.env.PUBLIC_WEB_URL ?? "http://localhost:3000"],
trustedOrigins: [env.PUBLIC_WEB_URL],
});

const betterAuthMacro = new Elysia({ name: "better-auth" })
Expand Down
14 changes: 14 additions & 0 deletions packages/server/lib/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import * as e from "./utils/environment.ts";

export const env = {
DATABASE_URL: e.string("DATABASE_URL"),
PUBLIC_WEB_URL: e.string("PUBLIC_WEB_URL"),
GOOGLE_CLIENT_ID: e.string("GOOGLE_CLIENT_ID"),
GOOGLE_CLIENT_SECRET: e.string("GOOGLE_CLIENT_SECRET"),

SERVER_PORT: e.string_optional("SERVER_PORT"),
PORT: e.string_optional("PORT"),
PUBLIC_SERVER_URL: e.string_optional("PUBLIC_SERVER_URL"),

PUBLIC_MOCK_DATA: e.boolean("PUBLIC_MOCK_DATA"),
};
21 changes: 21 additions & 0 deletions packages/server/lib/utils/environment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { panic } from "./panic.ts";

export function string(name: string) {
return (
string_optional(name) ?? panic(`Environment variable ${name} not found`)
);
}
export function string_optional(name: string): string | undefined {
return process.env[name];
}
export function boolean(name: string, fallback?: boolean): boolean {
const val = string_optional(name);
if (val === "1" || val === "true") {
return true;
}
if (val === "0" || val === "false") {
return false;
}
if (fallback !== undefined) return fallback;
panic(`Environment variable ${name} not found`);
}
3 changes: 3 additions & 0 deletions packages/server/lib/utils/panic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function panic(message: string): never {
throw new Error(message);
}
12 changes: 5 additions & 7 deletions packages/server/serve.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { app } from "./app.ts";
import { env } from "./lib/env.ts";

const port =
process.env.SERVER_PORT ??
process.env.PORT ??
parsePublicServerURL() ??
"4000";
env.SERVER_PORT ?? env.PORT ?? parsePort(env.PUBLIC_SERVER_URL) ?? "4000";

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

function parsePublicServerURL() {
if (!process.env.PUBLIC_SERVER_URL) return undefined;
const url = new URL(process.env.PUBLIC_SERVER_URL);
function parsePort(urlstr: string | undefined) {
if (!urlstr) return undefined;
const url = new URL(urlstr);
return url.port;
}