add db class
This commit is contained in:
38
src/index.ts
38
src/index.ts
@@ -1,32 +1,32 @@
|
||||
import { serve } from 'bun';
|
||||
import index from './index.html';
|
||||
import DB from './server/db';
|
||||
|
||||
const server = serve({
|
||||
routes: {
|
||||
// Serve index.html for all unmatched routes.
|
||||
'/*': index,
|
||||
|
||||
'/api/hello': {
|
||||
async GET(req) {
|
||||
return Response.json({
|
||||
message: 'Hello, world!',
|
||||
method: 'GET',
|
||||
});
|
||||
},
|
||||
async PUT(req) {
|
||||
return Response.json({
|
||||
message: 'Hello, world!',
|
||||
method: 'PUT',
|
||||
});
|
||||
},
|
||||
'/api/group/:id': async req => {
|
||||
const id = req.params.id;
|
||||
const group = await new DB().getGroup(id);
|
||||
console.log('Fetching group with ID:', id, 'Result:', group);
|
||||
if (!group) return new Response(JSON.stringify({ error: 'Not found' }), { status: 404 });
|
||||
return new Response(JSON.stringify(group), { headers: { 'Content-Type': 'application/json' } });
|
||||
},
|
||||
|
||||
'/api/hello/:name': async req => {
|
||||
const name = req.params.name;
|
||||
return Response.json({
|
||||
message: `Hello, ${name}!`,
|
||||
});
|
||||
},
|
||||
'/api/group': {
|
||||
async POST(req) {
|
||||
try {
|
||||
const group = (await req.formData() as FormData).get('code') as string;
|
||||
const response = await new DB().createGroup(group);
|
||||
return new Response(JSON.stringify(response), { headers: { 'Content-Type': 'application/json' } });
|
||||
} catch (err) {
|
||||
console.error('Error creating group:', err);
|
||||
return new Response(JSON.stringify({ error: 'Failed to create group' }), { status: 500 });
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
development: process.env.NODE_ENV !== 'production' && {
|
||||
|
||||
Reference in New Issue
Block a user