feat(login): 添加API地址配置功能并支持本地存储 fix(api): 移除CWD_CONFIG_KV相关配置改用数据库存储 fix(cors): 修改跨域配置允许所有来源 perf(email): 添加邮件发送日志记录和调试信息 perf(comments): 优化评论提交和邮件通知逻辑 docs(backend): 更新后端配置文档移除CWD_CONFIG_KV相关说明 chore: 更新端口号和示例配置
63 lines
1.9 KiB
TypeScript
63 lines
1.9 KiB
TypeScript
import { Hono } from 'hono';
|
||
import { cors } from 'hono/cors';
|
||
import { Bindings } from './bindings';
|
||
import { customCors } from './utils/cors';
|
||
import { adminAuth } from './utils/auth';
|
||
|
||
import { getComments } from './api/public/getComments';
|
||
import { postComment } from './api/public/postComment';
|
||
import { adminLogin } from './api/admin/login';
|
||
import { deleteComment } from './api/admin/deleteComment';
|
||
import { listComments } from './api/admin/listComments';
|
||
import { updateStatus } from './api/admin/updateStatus';
|
||
import { getAdminEmail } from './api/admin/getAdminEmail';
|
||
import { setAdminEmail } from './api/admin/setAdminEmail';
|
||
|
||
const app = new Hono<{ Bindings: Bindings }>();
|
||
const VERSION = 'v0.0.1';
|
||
|
||
app.use('*', async (c, next) => {
|
||
console.log('Request:start', {
|
||
method: c.req.method,
|
||
path: c.req.path,
|
||
url: c.req.url,
|
||
hasDb: !!c.env.CWD_DB,
|
||
hasAuthKv: !!c.env.CWD_AUTH_KV
|
||
});
|
||
const res = await next();
|
||
console.log('Request:end', {
|
||
method: c.req.method,
|
||
path: c.req.path
|
||
});
|
||
return res;
|
||
});
|
||
|
||
app.use('/api/*', async (c, next) => {
|
||
const corsMiddleware = customCors(c.env.ALLOW_ORIGIN);
|
||
return corsMiddleware(c, next);
|
||
});
|
||
app.use('/admin/*', async (c, next) => {
|
||
const corsMiddleware = customCors(c.env.ALLOW_ORIGIN);
|
||
return corsMiddleware(c, next);
|
||
});
|
||
|
||
app.get('/', (c) => {
|
||
return c.html(
|
||
`CWD 评论部署成功,当前版本 ${VERSION},<a href="https://github.com/anghunk/cwd-comments" target="_blank" rel="noreferrer">查看文档</a>`
|
||
);
|
||
});
|
||
|
||
app.get('/api/comments', getComments);
|
||
app.post('/api/comments', postComment);
|
||
|
||
app.post('/admin/login', adminLogin);
|
||
app.use('/admin/*', adminAuth);
|
||
app.delete('/admin/comments/delete', deleteComment);
|
||
app.get('/admin/comments/list', listComments);
|
||
app.put('/admin/comments/status', updateStatus);
|
||
// 设置接口
|
||
app.get('/admin/settings/email', getAdminEmail);
|
||
app.put('/admin/settings/email', setAdminEmail);
|
||
|
||
export default app;
|