-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
80 lines (69 loc) · 2.32 KB
/
server.js
File metadata and controls
80 lines (69 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const express = require("express");
const cors = require("cors");
require("dotenv").config();
const authMiddleware = require("./middleware/auth");
const secretRouteHandler = require("./routes/secret");
const getImportantData = require("./routes/service-role");
const getImportantDataWithAnonKey = require("./routes/anon-key");
const app = express();
const PORT = process.env.PORT || 3001;
// JWTシークレットの確認
const hmacSecret = process.env.SUPABASE_JWT_SECRET;
if (!hmacSecret) {
console.error("Please set the SUPABASE_JWT_SECRET environment variable");
process.exit(1);
}
// Supabase設定の確認
if (
!process.env.SUPABASE_URL ||
!process.env.SUPABASE_SERVICE_ROLE_KEY ||
!process.env.SUPABASE_ANON_KEY
) {
console.error(
"Please set SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY, and SUPABASE_ANON_KEY environment variables"
);
process.exit(1);
}
// ミドルウェアの設定
app.use(
cors({
origin: "*", // 本番環境では許可されたオリジンのみに制限することを推奨
allowedHeaders: [
"Origin",
"Content-Length",
"Content-Type",
"Authorization",
],
})
);
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// 基本的なルート
app.get("/", (req, res) => {
res.json({ message: "Express server is running!" });
});
// ヘルスチェック用エンドポイント
app.get("/health", (req, res) => {
res.json({ status: "OK", timestamp: new Date().toISOString() });
});
// 認証が必要な秘密のルート
app.post("/secret", authMiddleware, secretRouteHandler);
// Service roleを使用してSupabaseからデータを取得するルート
app.get("/important-data/service-role", authMiddleware, getImportantData);
// Anon keyを使用してSupabaseからデータを取得するルート
app.get(
"/important-data/anon-key",
authMiddleware,
getImportantDataWithAnonKey
);
// サーバー起動
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
console.log(`Health check: http://localhost:${PORT}/health`);
console.log(
`Important data (service role): http://localhost:${PORT}/important-data/service-role`
);
console.log(
`Important data (anon key): http://localhost:${PORT}/important-data/anon-key`
);
});