Skip to content

Commit c8a1015

Browse files
committed
set up environment variable config
1 parent f764b12 commit c8a1015

File tree

10 files changed

+67
-50
lines changed

10 files changed

+67
-50
lines changed

.env.sample

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
NEXT_PUBLIC_MODE=default
1+
# Config
2+
PUBLIC_SERVER_URL=http://localhost:4000
3+
PUBLIC_MOCK_DATA=true
4+
SERVER_PORT=4000 # optional, server port (alias: PORT, PUBLIC_SERVER_URL.port) (defaults to 4000 if none are specified)
5+
6+
# Secrets
7+
PUBLIC_X_ANON_KEY=...
8+
X_API_KEY=...

.gitignore

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,11 @@
1-
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
1+
node_modules
2+
/.env
23

3-
node_modules/
4-
5-
.tool-versions
6-
bun.lockb
7-
8-
# dependencies
9-
/node_modules
10-
/.pnp
11-
.pnp.js
12-
.yarn/install-state.gz
13-
14-
# testing
15-
/coverage
16-
17-
# next.js
18-
.next
19-
out
20-
21-
# production
22-
build
23-
24-
# misc
4+
# OS / Dev Env
255
.DS_Store
266
*.pem
27-
.direnv
28-
29-
# debug
30-
npm-debug.log*
31-
yarn-debug.log*
32-
yarn-error.log*
33-
34-
# local env files
35-
.env*.local
36-
37-
# vercel
38-
.vercel
7+
/.direnv
398

40-
# typescript
9+
# Build Output
10+
dist
4111
*.tsbuildinfo
42-
next-env.d.ts

packages/server/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
".": "./app.ts"
88
},
99
"scripts": {
10-
"dev": "bun run --watch ./serve.ts",
11-
"dev:mock": "MOCK=true bun run --watch ./serve.ts"
10+
"dev": "bun --env-file=../../.env run --watch ./serve.ts"
1211
},
1312
"devDependencies": {
1413
"@types/bun": "^1.2.18"

packages/server/serve.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
import { app } from "./app.ts";
22

3-
const port = process.env.SERVER_PORT ?? process.env.PORT ?? 4000;
3+
const port =
4+
process.env.SERVER_PORT ??
5+
process.env.PORT ??
6+
parsePublicServerURL() ??
7+
"4000";
48

59
app.listen(port, () =>
610
console.log(`Server started at http://localhost:${port}`),
711
);
12+
13+
function parsePublicServerURL() {
14+
if (!process.env.PUBLIC_SERVER_URL) return undefined;
15+
const url = new URL(process.env.PUBLIC_SERVER_URL);
16+
return url.port;
17+
}

packages/web/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"type": "module",
66
"scripts": {
77
"dev": "vite",
8-
"dev:mock": "VITE_MODE=mock vite",
98
"build": "tsc && vite build",
109
"preview": "vite preview",
1110
"check": "tsc --noEmit"

packages/web/src/app/utils/user.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import type { RegisterType } from "@/app/type";
22
import { SampleUser } from "@/app/utils/mock_data";
3-
4-
const MODE = import.meta.env.VITE_MODE;
3+
import { env } from "../../lib/env.ts";
54

65
export class User {
76
private user: RegisterType | undefined;
87

98
constructor() {
109
if (typeof window !== "undefined") {
11-
if (MODE === "mock") {
10+
if (env.mockData) {
1211
this.user = SampleUser;
1312
} else {
1413
const storedUser = localStorage.getItem("user");
@@ -36,7 +35,7 @@ export class User {
3635
setUser(newUser: RegisterType): void {
3736
this.user = newUser;
3837

39-
if (typeof window !== "undefined" && MODE !== "mock") {
38+
if (typeof window !== "undefined" && !env.mockData) {
4039
localStorage.setItem("user", JSON.stringify(newUser));
4140
}
4241
}

packages/web/src/lib/api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { edenTreaty } from "@elysiajs/eden";
22
import type { App } from "@packages/server";
3+
import { env } from "./env.ts";
34

4-
export const api = edenTreaty<App>("http://localhost:4000").api;
5+
export const api = edenTreaty<App>(env.serverURL).api;

packages/web/src/lib/env.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export const env = {
2+
mockData: boolean(import.meta.env.PUBLIC_MOCK_DATA),
3+
serverURL: string(import.meta.env.PUBLIC_SERVER_URL, {
4+
name: "PUBLIC_SERVER_URL",
5+
}),
6+
};
7+
8+
function boolean(value: string | undefined): boolean {
9+
return value === "true" || value === "1";
10+
}
11+
12+
/**
13+
* if value != null -> return value
14+
* if defaultValue != null -> return defaultValue
15+
* default -> throws Error
16+
* @param value 値
17+
* @param param1
18+
* @returns 結果
19+
*/
20+
function string(
21+
value: string | undefined,
22+
{ name, defaultValue }: { name?: string; defaultValue?: string },
23+
): string {
24+
if (value != null) {
25+
return value;
26+
}
27+
if (defaultValue != null) {
28+
return defaultValue;
29+
}
30+
throw new Error(`${name} is undefined`);
31+
}

packages/web/src/vite-env.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/// <reference types="vite/client" />
22

33
interface ImportMetaEnv {
4-
readonly VITE_MODE: string;
4+
readonly PUBLIC_SERVER_URL: string;
5+
readonly PUBLIC_MOCK_DATA: string;
56
}
67

78
interface ImportMeta {

packages/web/vite.config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import path from "node:path";
21
import react from "@vitejs/plugin-react";
32
import { defineConfig } from "vite";
43

54
export default defineConfig({
65
plugins: [react()],
76
resolve: {
87
alias: {
9-
"@": path.resolve(__dirname, "./src"),
8+
"@": "./src",
109
},
1110
},
11+
envDir: "../../",
12+
envPrefix: "PUBLIC_",
1213
server: {
1314
port: 3000,
1415
},

0 commit comments

Comments
 (0)