47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
|
|
import Fastify from 'fastify';
|
||
|
|
import cors from '@fastify/cors';
|
||
|
|
import cookie from '@fastify/cookie';
|
||
|
|
import websocket from '@fastify/websocket';
|
||
|
|
import { config } from './config.js';
|
||
|
|
import { authRoutes } from './routes/auth.js';
|
||
|
|
import { serverRoutes } from './routes/servers.js';
|
||
|
|
import { oauthRoutes } from './routes/oauth.js';
|
||
|
|
|
||
|
|
const app = Fastify({
|
||
|
|
logger: {
|
||
|
|
level: config.NODE_ENV === 'production' ? 'info' : 'debug',
|
||
|
|
transport:
|
||
|
|
config.NODE_ENV === 'development'
|
||
|
|
? { target: 'pino-pretty', options: { colorize: true, singleLine: true } }
|
||
|
|
: undefined,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
await app.register(cors, {
|
||
|
|
origin: [config.NEXT_PUBLIC_APP_URL],
|
||
|
|
credentials: true,
|
||
|
|
});
|
||
|
|
await app.register(cookie);
|
||
|
|
await app.register(websocket, { options: { maxPayload: 1024 * 1024 } });
|
||
|
|
|
||
|
|
app.get('/health', async () => ({ ok: true, ts: Date.now() }));
|
||
|
|
|
||
|
|
await app.register(authRoutes);
|
||
|
|
await app.register(serverRoutes);
|
||
|
|
await app.register(oauthRoutes);
|
||
|
|
|
||
|
|
app.setErrorHandler((err, _req, reply) => {
|
||
|
|
app.log.error(err);
|
||
|
|
if (!reply.sent) {
|
||
|
|
reply.code(err.statusCode ?? 500).send({ error: err.message ?? 'internal_error' });
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
try {
|
||
|
|
await app.listen({ port: config.PORT, host: '0.0.0.0' });
|
||
|
|
app.log.info(`api listening on http://localhost:${config.PORT}`);
|
||
|
|
} catch (err) {
|
||
|
|
app.log.error(err);
|
||
|
|
process.exit(1);
|
||
|
|
}
|