First commit
This commit is contained in:
46
src/server/app.js
Normal file
46
src/server/app.js
Normal file
@@ -0,0 +1,46 @@
|
||||
import { Hono } from 'hono';
|
||||
import { serveStatic } from '@hono/node-server/serve-static';
|
||||
import { HttpError } from './utils/httpErrors.js';
|
||||
import { loadUser } from './middleware/auth.js';
|
||||
import { requireCsrf } from './middleware/csrf.js';
|
||||
import accountRoutes from './modules/account/routes.js';
|
||||
import alertRoutes from './modules/alerts/routes.js';
|
||||
import authRoutes from './modules/auth/routes.js';
|
||||
import notificationMethodRoutes from './modules/notificationMethods/routes.js';
|
||||
import siteRoutes from './modules/sites/routes.js';
|
||||
import { logger } from './utils/logger.js';
|
||||
|
||||
export function createApp() {
|
||||
const app = new Hono();
|
||||
|
||||
app.use('*', loadUser);
|
||||
app.use('/api/*', requireCsrf);
|
||||
|
||||
app.get('/api/health', (c) => c.json({ ok: true, service: 'CertRemind' }));
|
||||
app.route('/api/auth', authRoutes);
|
||||
app.route('/api/account', accountRoutes);
|
||||
app.route('/api/sites', siteRoutes);
|
||||
app.route('/api/alerts', alertRoutes);
|
||||
app.route('/api/notification-methods', notificationMethodRoutes);
|
||||
|
||||
app.use('/assets/*', serveStatic({ root: './dist' }));
|
||||
app.get('*', serveStatic({ path: './dist/index.html' }));
|
||||
|
||||
app.onError((error, c) => {
|
||||
if (error instanceof HttpError) {
|
||||
return c.json({ error: error.message, details: error.details }, error.status);
|
||||
}
|
||||
|
||||
logger.error('api.unhandled_error', {
|
||||
message: error.message,
|
||||
stack: envSafeStack(error),
|
||||
});
|
||||
return c.json({ error: 'サーバーエラーが発生しました' }, 500);
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
function envSafeStack(error) {
|
||||
return process.env.NODE_ENV === 'production' ? undefined : error.stack;
|
||||
}
|
||||
Reference in New Issue
Block a user