-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: hono package and helpers are now centralized in
@u22n/hono
- Loading branch information
1 parent
8308ef6
commit b3f8965
Showing
52 changed files
with
463 additions
and
581 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import { Hono } from 'hono'; | ||
import { createHonoApp } from '@u22n/hono'; | ||
|
||
export const eventApi = new Hono().post('/events/:params{.+}', async (c) => { | ||
return c.json({ error: 'Not implemented' }, 400); | ||
}); | ||
export const eventApi = createHonoApp().post( | ||
'/events/:params{.+}', | ||
async (c) => { | ||
return c.json({ error: 'Not implemented' }, 400); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,55 @@ | ||
import './tracing'; | ||
import type { Ctx, TrpcContext } from './ctx'; | ||
import { env } from './env'; | ||
import { Hono } from 'hono'; | ||
import { serve } from '@hono/node-server'; | ||
import { cors } from 'hono/cors'; | ||
import { | ||
createHonoApp, | ||
setupCors, | ||
setupErrorHandlers, | ||
setupHealthReporting, | ||
setupHonoListener, | ||
setupRouteLogger, | ||
setupRuntime, | ||
setupTrpcHandler | ||
} from '@u22n/hono'; | ||
import { authApi } from './routes/auth'; | ||
import { realtimeApi } from './routes/realtime'; | ||
import { trpcPlatformRouter } from './trpc'; | ||
import { db } from '@u22n/database'; | ||
import { trpcServer } from '@hono/trpc-server'; | ||
import { authMiddleware, serviceMiddleware } from './middlewares'; | ||
import { otel } from '@u22n/otel/hono'; | ||
import { logger } from 'hono/logger'; | ||
import { servicesApi } from './routes/services'; | ||
|
||
const app = new Hono<Ctx>(); | ||
app.use(otel()); | ||
const app = createHonoApp<Ctx>(); | ||
|
||
// Logger middleware | ||
if (env.NODE_ENV === 'development') { | ||
app.use(logger()); | ||
} | ||
app.use(otel()); | ||
|
||
// CORS middleware | ||
app.use( | ||
'*', | ||
cors({ | ||
origin: env.WEBAPP_URL, | ||
credentials: true, | ||
exposeHeaders: ['Location'] | ||
}) | ||
); | ||
setupRouteLogger(app, env.NODE_ENV === 'development'); | ||
setupCors(app, { origin: [env.WEBAPP_URL], exposeHeaders: ['Location'] }); | ||
setupHealthReporting(app, { service: 'Platform' }); | ||
setupErrorHandlers(app); | ||
|
||
// Auth middleware | ||
app.use('*', authMiddleware); | ||
|
||
// Health check endpoint | ||
app.get('/health', (c) => { | ||
const memoryUsage = process.memoryUsage(); | ||
const uptime = process.uptime(); | ||
return c.json({ | ||
service: `Platform`, | ||
memoryUsage, | ||
uptime | ||
}); | ||
}); | ||
setupTrpcHandler( | ||
app, | ||
trpcPlatformRouter, | ||
(_, c) => | ||
({ | ||
db, | ||
account: c.get('account'), | ||
org: null, | ||
event: c, | ||
selfHosted: !env.EE_LICENSE_KEY | ||
}) satisfies TrpcContext | ||
); | ||
|
||
// Routes | ||
app.route('/auth', authApi); | ||
app.route('/realtime', realtimeApi); | ||
|
||
// Service Endpoints | ||
app.use('/services/*', serviceMiddleware); | ||
app.route('/services', servicesApi); | ||
|
||
// TRPC handler | ||
app.use( | ||
'/trpc/*', | ||
trpcServer({ | ||
router: trpcPlatformRouter, | ||
createContext: (_, c) => | ||
({ | ||
db, | ||
account: c.get('account'), | ||
org: null, | ||
event: c, | ||
selfHosted: !env.EE_LICENSE_KEY | ||
}) satisfies TrpcContext | ||
}) | ||
); | ||
|
||
// 404 handler | ||
app.notFound((c) => c.json({ message: 'Not Found' }, 404)); | ||
|
||
// Global error handler | ||
app.onError((err, c) => { | ||
console.error(err); | ||
return c.json({ message: 'Something went wrong' }, 500); | ||
}); | ||
|
||
// Handle uncaught errors | ||
process.on('unhandledRejection', (err) => console.error(err)); | ||
process.on('uncaughtException', (err) => console.error(err)); | ||
|
||
// Start server | ||
const server = serve( | ||
{ | ||
fetch: app.fetch, | ||
port: env.PORT | ||
}, | ||
() => console.info(`Server listening on port ${env.PORT}`) | ||
); | ||
|
||
// Clean Exit | ||
const handleExit = () => { | ||
server.close(() => { | ||
console.info('Shutting down...'); | ||
process.exit(); | ||
}); | ||
}; | ||
|
||
process.on('SIGINT', handleExit); | ||
process.on('SIGTERM', handleExit); | ||
const cleanup = setupHonoListener(app, { port: env.PORT }); | ||
setupRuntime([cleanup]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.