forked from mxx1111/mdlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
64 lines (54 loc) · 2.52 KB
/
Copy pathindex.ts
File metadata and controls
64 lines (54 loc) · 2.52 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
import type { Env } from './types'
import { Hono } from 'hono'
import { cors } from 'hono/cors'
import { activateHandler } from './activate'
import { authMiddleware, authRoutes, meHandler } from './auth'
import { isAllowedOrigin } from './origin'
import { createShareHandler, deleteShareHandler, listSharesHandler, unlockShareHandler, viewShareHandler } from './share'
import { SHARE_FAVICON_PATH } from './share-head'
import { pullHandler, pushHandler } from './sync'
import { getThemeHandler, importThemeHandler, listThemesHandler } from './themes'
import { uploadHandler } from './upload'
import { localUploadFileHandler } from './upload-local'
import { afdianWebhookHandler } from './webhook'
import { normalizeWechatImagesHandler } from './wechat-images'
const app = new Hono<{ Bindings: Env, Variables: { userId: string } }>()
// CORS:允许 APP_URL 中配置的来源(支持通配符,逗号分隔)携带凭据访问
app.use(`*`, async (c, next) => {
const handler = cors({
origin: origin => (isAllowedOrigin(c.env, origin) ? origin : null),
allowMethods: [`GET`, `POST`, `DELETE`, `OPTIONS`],
allowHeaders: [`Authorization`, `Content-Type`],
credentials: true,
maxAge: 86400,
})
return handler(c, next)
})
app.get(`/`, c => c.json({ name: `md-api`, ok: true }))
app.get(`/themes`, listThemesHandler)
app.get(`/themes/:id`, getThemeHandler)
app.post(`/themes/import`, importThemeHandler)
// 默认图床上传(公开,由 UPLOAD_ENABLED 控制)
app.post(`/upload`, uploadHandler)
app.get(`/uploads/*`, localUploadFileHandler)
app.post(`/wechat/normalize-images`, normalizeWechatImagesHandler)
app.get(SHARE_FAVICON_PATH, c => c.env.ASSETS.fetch(c.req.raw))
app.route(`/auth`, authRoutes)
// 爱发电 Webhook:无密钥与带路径密钥两种形式共用同一处理器。
// 设置 AFDIAN_WEBHOOK_TOKEN 后,仅 `/webhooks/afdian/<token>` 可通过校验。
app.post(`/webhooks/afdian`, afdianWebhookHandler)
app.post(`/webhooks/afdian/:token`, afdianWebhookHandler)
// 公开分享:只读 HTML 预览页 + 密码解锁
app.get(`/s/:shareId`, viewShareHandler)
app.post(`/s/:shareId/unlock`, unlockShareHandler)
const api = new Hono<{ Bindings: Env, Variables: { userId: string } }>()
api.use(`*`, authMiddleware)
api.get(`/me`, meHandler)
api.get(`/sync/pull`, pullHandler)
api.post(`/sync/push`, pushHandler)
api.post(`/sync/activate`, activateHandler)
api.get(`/share`, listSharesHandler)
api.post(`/share`, createShareHandler)
api.delete(`/share/:id`, deleteShareHandler)
app.route(`/`, api)
export default app