File size: 2,473 Bytes
c0dd699
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Run `npm run dev` in your terminal to start a development server
 * Open a browser tab at http://localhost:8787/ to see your worker in action
 * Run `npm run deploy` to publish your worker
 *
 * Bind resources to your worker in `wrangler.toml`.
 *
 * Learn more at https://developers.cloudflare.com/workers/
 */

import { fromHono } from 'chanfana';
import { Hono } from 'hono';
import { cors } from 'hono/cors';

import { authMiddleware, durableObjectMiddleware } from './middlewares';

import { TaskStatusDurableObject } from './durableObjects';

import { GetR2PutUrl } from './endpoints/getR2PutUrl';
import { CreateTask } from './endpoints/taskCreate';
import { FetchTask } from './endpoints/taskFetch';
import { UpdateTask } from './endpoints/taskUpdate';
import { UploadFile, UploadFilePut } from './endpoints/fileUpload';
import { DownloadFile } from './endpoints/fileDownload';
import { UserInfo } from './endpoints/userInfo';
import { FetchBill } from './endpoints/billFetch';
import { FetchTaskSSE } from './endpoints/taskFetchSSE';

// Star a Hono app
const app = new Hono();

app.use(
	'/api/*',
	cors({
		credentials: true,
		origin: '*',
	})
);

// Setup OpenAPI registry
const options = {
	docs_url: '/',
	schema: {
		info: {
			title: 'Remove background Worker API',
			version: '1.0',
		},
		servers: [
			{
				url: '/',
				description: 'Development server',
			},
			{
				url: 'https://a1d-rb.k-xshar.workers.dev/',
				description: 'Production server',
			},
		],
		security: [
			{
				BearerAuth: [],
			},
		],
	},
};
const openapi = fromHono(app, options);

openapi.registry.registerComponent('securitySchemes', 'BearerAuth', {
	type: 'http',
	scheme: 'bearer',
	bearerFormat: 'JWT',
});

// Register OpenAPI endpoints
openapi.get('/api/uploads/:file_name/url', authMiddleware, GetR2PutUrl);
openapi.post('/api/uploads/:file_name', authMiddleware, UploadFile);
openapi.put('/api/uploads/:file_name', authMiddleware, UploadFilePut);
openapi.get('/api/uploads/:file_name', DownloadFile);

openapi.get('/api/user', authMiddleware, UserInfo);

openapi.post('/api/task', authMiddleware, CreateTask);
openapi.get('/api/task/:id', durableObjectMiddleware, FetchTask);
openapi.get('/api/task/:id/sse', durableObjectMiddleware, FetchTaskSSE);
openapi.patch('/api/task/:id', authMiddleware, durableObjectMiddleware, UpdateTask);

openapi.get('/admin/bill', FetchBill);

export { TaskStatusDurableObject };

// Export the Hono app
export default app;