|
1 | | -import { getCorsMiddleware } from "@/middleware/cors"; |
2 | | -import { errorHandler } from "@/middleware/error"; |
3 | | -import audioRoutes from "@/routes/audio"; |
4 | | -import { healthRoutes } from "@/routes/health"; |
5 | | -import pinsRoutes from "@/routes/pins"; |
6 | | -import { logger } from "@/utils/logger"; |
7 | | -import { Hono } from "hono"; |
8 | | -import { logger as honoLogger } from "hono/logger"; |
9 | | -import { requestId } from "hono/request-id"; |
10 | | -import { timing } from "hono/timing"; |
| 1 | +import { getCorsMiddleware } from "@/middleware/cors" |
| 2 | +import { errorHandler } from "@/middleware/error" |
| 3 | +import audioRoutes from "@/routes/audio" |
| 4 | +import { healthRoutes } from "@/routes/health" |
| 5 | +import pinsRoutes from "@/routes/pins" |
| 6 | +import { logger } from "@/utils/logger" |
| 7 | +import { Hono } from "hono" |
| 8 | +import { logger as honoLogger } from "hono/logger" |
| 9 | +import { requestId } from "hono/request-id" |
| 10 | +import { timing } from "hono/timing" |
11 | 11 |
|
12 | 12 | /** |
13 | 13 | * Cloudflare Workers環境変数の型定義 |
14 | 14 | */ |
15 | 15 | export interface Env { |
16 | | - // 環境変数 |
17 | | - ENVIRONMENT: "development" | "production"; |
18 | | - CORS_ORIGIN?: string; |
| 16 | + // 環境変数 |
| 17 | + ENVIRONMENT: "development" | "production" |
| 18 | + CORS_ORIGIN?: string |
19 | 19 |
|
20 | | - // Supabase |
21 | | - SUPABASE_URL: string; |
22 | | - SUPABASE_ANON_KEY: string; |
23 | | - SUPABASE_SERVICE_KEY: string; |
| 20 | + // Supabase |
| 21 | + SUPABASE_URL: string |
| 22 | + SUPABASE_ANON_KEY: string |
| 23 | + SUPABASE_SERVICE_KEY: string |
24 | 24 |
|
25 | | - // Python Audio Analyzer(YAMNet使用) |
26 | | - PYTHON_AUDIO_ANALYZER_URL: string; |
27 | | - PYTHON_AUDIO_ANALYZER_TIMEOUT: string; |
| 25 | + // Python Audio Analyzer(YAMNet使用) |
| 26 | + PYTHON_AUDIO_ANALYZER_URL: string |
| 27 | + PYTHON_AUDIO_ANALYZER_TIMEOUT: string |
28 | 28 |
|
29 | | - // KVネームスペース(将来使用) |
30 | | - // CACHE: KVNamespace |
| 29 | + // KVネームスペース(将来使用) |
| 30 | + // CACHE: KVNamespace |
31 | 31 |
|
32 | | - // R2バケット(将来使用) |
33 | | - // AUDIO_STORAGE: R2Bucket |
| 32 | + // R2バケット(将来使用) |
| 33 | + // AUDIO_STORAGE: R2Bucket |
34 | 34 |
|
35 | | - // Durable Objects(将来使用) |
36 | | - // RATE_LIMITER: DurableObjectNamespace |
| 35 | + // Durable Objects(将来使用) |
| 36 | + // RATE_LIMITER: DurableObjectNamespace |
37 | 37 | } |
38 | 38 |
|
39 | 39 | /** |
40 | 40 | * Honoアプリケーションの初期化 |
41 | 41 | */ |
42 | | -const app = new Hono<{ Bindings: Env }>(); |
| 42 | +const app = new Hono<{ Bindings: Env }>() |
43 | 43 |
|
44 | 44 | // グローバルミドルウェア |
45 | | -app.use("*", requestId()); |
46 | | -app.use("*", timing()); |
47 | | -app.use("*", honoLogger()); |
48 | | -app.use("*", errorHandler); |
| 45 | +app.use("*", requestId()) |
| 46 | +app.use("*", timing()) |
| 47 | +app.use("*", honoLogger()) |
| 48 | +app.use("*", errorHandler) |
49 | 49 |
|
50 | 50 | // CORS設定(環境変数から取得) |
51 | 51 | app.use("*", async (c, next) => { |
52 | | - const corsMiddleware = getCorsMiddleware(c.env); |
53 | | - return corsMiddleware(c, next); |
54 | | -}); |
| 52 | + const corsMiddleware = getCorsMiddleware(c.env) |
| 53 | + return corsMiddleware(c, next) |
| 54 | +}) |
55 | 55 |
|
56 | 56 | // リクエストログ |
57 | 57 | app.use("*", async (c, next) => { |
58 | | - const start = Date.now(); |
59 | | - const method = c.req.method; |
60 | | - const url = new URL(c.req.url); |
61 | | - |
62 | | - logger.info("Request received", { |
63 | | - requestId: c.get("requestId"), |
64 | | - method, |
65 | | - path: url.pathname, |
66 | | - query: Object.fromEntries(url.searchParams), |
67 | | - userAgent: c.req.header("user-agent"), |
68 | | - }); |
69 | | - |
70 | | - await next(); |
71 | | - |
72 | | - const duration = Date.now() - start; |
73 | | - const status = c.res.status; |
74 | | - |
75 | | - logger.info("Request completed", { |
76 | | - requestId: c.get("requestId"), |
77 | | - method, |
78 | | - path: url.pathname, |
79 | | - status, |
80 | | - duration, |
81 | | - }); |
82 | | -}); |
| 58 | + const start = Date.now() |
| 59 | + const method = c.req.method |
| 60 | + const url = new URL(c.req.url) |
| 61 | + |
| 62 | + logger.info("Request received", { |
| 63 | + requestId: c.get("requestId"), |
| 64 | + method, |
| 65 | + path: url.pathname, |
| 66 | + query: Object.fromEntries(url.searchParams), |
| 67 | + userAgent: c.req.header("user-agent"), |
| 68 | + }) |
| 69 | + |
| 70 | + await next() |
| 71 | + |
| 72 | + const duration = Date.now() - start |
| 73 | + const status = c.res.status |
| 74 | + |
| 75 | + logger.info("Request completed", { |
| 76 | + requestId: c.get("requestId"), |
| 77 | + method, |
| 78 | + path: url.pathname, |
| 79 | + status, |
| 80 | + duration, |
| 81 | + }) |
| 82 | +}) |
83 | 83 |
|
84 | 84 | /** |
85 | 85 | * ルートハンドラー |
86 | 86 | */ |
87 | 87 | app.get("/", (c) => { |
88 | | - return c.json({ |
89 | | - success: true, |
90 | | - data: { |
91 | | - name: "Sonory API", |
92 | | - version: "0.1.0", |
93 | | - environment: c.env.ENVIRONMENT, |
94 | | - timestamp: new Date().toISOString(), |
95 | | - }, |
96 | | - }); |
97 | | -}); |
| 88 | + return c.json({ |
| 89 | + success: true, |
| 90 | + data: { |
| 91 | + name: "Sonory API", |
| 92 | + version: "0.1.0", |
| 93 | + environment: c.env.ENVIRONMENT, |
| 94 | + timestamp: new Date().toISOString(), |
| 95 | + }, |
| 96 | + }) |
| 97 | +}) |
98 | 98 |
|
99 | 99 | // APIルート |
100 | | -app.route("/api/health", healthRoutes); |
101 | | -app.route("/api/audio", audioRoutes); |
102 | | -app.route("/api/pins", pinsRoutes); |
| 100 | +app.route("/api/health", healthRoutes) |
| 101 | +app.route("/api/audio", audioRoutes) |
| 102 | +app.route("/api/pins", pinsRoutes) |
103 | 103 |
|
104 | 104 | // 404ハンドラー |
105 | 105 | app.notFound((c) => { |
106 | | - return c.json( |
107 | | - { |
108 | | - success: false, |
109 | | - error: { |
110 | | - code: "NOT_FOUND", |
111 | | - message: "The requested resource was not found", |
112 | | - timestamp: new Date().toISOString(), |
113 | | - requestId: c.get("requestId"), |
114 | | - }, |
115 | | - }, |
116 | | - 404, |
117 | | - ); |
118 | | -}); |
| 106 | + return c.json( |
| 107 | + { |
| 108 | + success: false, |
| 109 | + error: { |
| 110 | + code: "NOT_FOUND", |
| 111 | + message: "The requested resource was not found", |
| 112 | + timestamp: new Date().toISOString(), |
| 113 | + requestId: c.get("requestId"), |
| 114 | + }, |
| 115 | + }, |
| 116 | + 404, |
| 117 | + ) |
| 118 | +}) |
119 | 119 |
|
120 | 120 | /** |
121 | 121 | * Cloudflare Workersエクスポート |
122 | 122 | */ |
123 | 123 | export default { |
124 | | - fetch: app.fetch, |
125 | | -}; |
| 124 | + fetch: app.fetch, |
| 125 | +} |
0 commit comments