1+ import { registry } from "./registry" ;
2+ import { auth } from "./auth" ;
3+ import { Hono } from "hono" ;
4+ import { serve } from "@hono/node-server" ;
5+ import { createMemoryDriver } from "@rivetkit/memory" ;
6+
7+ // Setup router
8+ const app = new Hono ( ) ;
9+
10+ // Start RivetKit
11+ const { client, hono } = registry . run ( {
12+ driver : createMemoryDriver ( ) ,
13+ cors : {
14+ // IMPORTANT: Configure origins in production
15+ origin : "*" ,
16+ } ,
17+ } ) ;
18+
19+ // Mount Better Auth routes
20+ app . on ( [ "GET" , "POST" ] , "/api/auth/**" , ( c ) => auth . handler ( c . req . raw ) ) ;
21+
22+ // Expose RivetKit to the frontend
23+ app . route ( "/registry" , hono ) ;
24+
25+ // Example HTTP endpoint to join chat room
26+ app . post ( "/api/join-room/:roomId" , async ( c ) => {
27+ const roomId = c . req . param ( "roomId" ) ;
28+
29+ // Verify authentication
30+ const authResult = await auth . api . getSession ( {
31+ headers : c . req . header ( ) ,
32+ } ) ;
33+
34+ if ( ! authResult ?. session || ! authResult ?. user ) {
35+ return c . json ( { error : "Unauthorized" } , 401 ) ;
36+ }
37+
38+ try {
39+ const room = client . chatRoom . getOrCreate ( roomId ) ;
40+ const messages = await room . getMessages ( ) ;
41+
42+ return c . json ( {
43+ success : true ,
44+ roomId,
45+ messages,
46+ user : authResult . user
47+ } ) ;
48+ } catch ( error ) {
49+ return c . json ( { error : "Failed to join room" } , 500 ) ;
50+ }
51+ } ) ;
52+
53+ serve ( { fetch : app . fetch , port : 6420 } , ( ) =>
54+ console . log ( "Listening at http://localhost:6420" ) ,
55+ ) ;
0 commit comments