Repository navigation
Expand file tree
/
Copy pathworker.js
More file actions
164 lines (137 loc) · 3.55 KB
/
Copy pathworker.js
File metadata and controls
164 lines (137 loc) · 3.55 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { Redis } from '@upstash/redis'
import { Hono } from 'hono'
import { cors } from 'hono/cors'
import { logger } from 'hono/logger'
// Initialize Redis
const redis = new Redis({
url: process.env.REDIS_URL,
token: process.env.REDIS_TOKEN,
})
// Initialize Hono app
const app = new Hono()
// Add middleware
app.use('*', logger())
app.use('*', cors())
// Health check endpoint
app.get('/health', (c) => {
return c.json({ status: 'healthy' })
})
// Ready check endpoint
app.get('/ready', async (c) => {
try {
await redis.ping()
return c.json({ status: 'ready' })
} catch (error) {
return c.json({ status: 'not ready', error: error.message }, 503)
}
})
// Validation endpoint
app.post('/api/validate', async (c) => {
try {
const body = await c.req.json()
const { content, file_type } = body
// Generate job ID
const jobId = crypto.randomUUID()
// Store validation request in Redis
await redis.set(`job:${jobId}`, JSON.stringify({
content,
file_type,
status: 'pending',
timestamp: Date.now()
}))
// Return job ID immediately
return c.json({
job_id: jobId,
status: 'queued'
})
} catch (error) {
return c.json({ error: error.message }, 500)
}
})
// Job status endpoint
app.get('/api/job/:jobId', async (c) => {
try {
const jobId = c.req.param('jobId')
const job = await redis.get(`job:${jobId}`)
if (!job) {
return c.json({ error: 'Job not found' }, 404)
}
return c.json(JSON.parse(job))
} catch (error) {
return c.json({ error: error.message }, 500)
}
})
// Batch validation endpoint
app.post('/api/batch-validate', async (c) => {
try {
const body = await c.req.json()
const { items } = body
if (!Array.isArray(items) || items.length === 0 || items.length > 10) {
return c.json({ error: 'Invalid batch size' }, 400)
}
const batchId = crypto.randomUUID()
const jobIds = []
for (const item of items) {
const jobId = crypto.randomUUID()
jobIds.push(jobId)
await redis.set(`job:${jobId}`, JSON.stringify({
content: item.content,
file_type: item.file_type,
status: 'pending',
timestamp: Date.now(),
batch_id: batchId
}))
}
return c.json({
batch_id: batchId,
job_ids: jobIds,
status: 'queued'
})
} catch (error) {
return c.json({ error: error.message }, 500)
}
})
// Batch status endpoint
app.get('/api/batch/:batchId', async (c) => {
try {
const batchId = c.req.param('batchId')
const jobs = await redis.keys(`job:*`)
const batchJobs = []
for (const jobKey of jobs) {
const job = await redis.get(jobKey)
const jobData = JSON.parse(job)
if (jobData.batch_id === batchId) {
batchJobs.push(jobData)
}
}
if (batchJobs.length === 0) {
return c.json({ error: 'Batch not found' }, 404)
}
return c.json({
batch_id: batchId,
jobs: batchJobs
})
} catch (error) {
return c.json({ error: error.message }, 500)
}
})
// Serve frontend static files
app.get('*', async (c) => {
const url = new URL(c.req.url)
const path = url.pathname === '/' ? '/index.html' : url.pathname
try {
const file = await c.env.ASSETS.get(path)
if (!file) {
return c.notFound()
}
const headers = new Headers()
file.writeHttpMetadata(headers)
headers.set('etag', file.httpEtag)
return new Response(file.body, {
headers,
})
} catch (error) {
return c.json({ error: error.message }, 500)
}
})
export default app