first commit
This commit is contained in:
20
backend/src/auth.ts
Normal file
20
backend/src/auth.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { betterAuth } from 'better-auth';
|
||||
import { prismaAdapter } from 'better-auth/adapters/prisma';
|
||||
import { prisma } from './prisma';
|
||||
|
||||
export const auth = betterAuth({
|
||||
database: prismaAdapter(prisma, {
|
||||
provider: 'sqlite'
|
||||
}),
|
||||
basePath: '/api',
|
||||
emailAndPassword: {
|
||||
enabled: true,
|
||||
},
|
||||
socialProviders: {
|
||||
github: {
|
||||
clientId: process.env.GITHUB_CLIENT_ID || 'dummy',
|
||||
clientSecret: process.env.GITHUB_CLIENT_SECRET || 'dummy',
|
||||
enabled: !!(process.env.GITHUB_CLIENT_ID && process.env.GITHUB_CLIENT_SECRET),
|
||||
},
|
||||
},
|
||||
});
|
||||
35
backend/src/index.ts
Normal file
35
backend/src/index.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { Elysia } from 'elysia';
|
||||
import { cors } from '@elysiajs/cors';
|
||||
import { auth } from './auth';
|
||||
import { prisma } from './prisma';
|
||||
import { resources } from './resources';
|
||||
import { posts } from './posts';
|
||||
|
||||
const app = new Elysia()
|
||||
.use(
|
||||
cors({
|
||||
origin: 'http://localhost:5173',
|
||||
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
|
||||
credentials: true,
|
||||
allowedHeaders: ['Content-Type', 'Authorization']
|
||||
})
|
||||
)
|
||||
.mount('/auth', auth.handler)
|
||||
.get('/health', () => 'OK')
|
||||
.use(resources)
|
||||
.use(posts)
|
||||
.get('/user/:id', async ({ params }) => {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: params.id
|
||||
}
|
||||
});
|
||||
return user;
|
||||
});
|
||||
|
||||
app.listen(3001, () => {
|
||||
console.log('Server running on port 3001');
|
||||
});
|
||||
|
||||
export default app;
|
||||
export type App = typeof app;
|
||||
38
backend/src/posts.ts
Normal file
38
backend/src/posts.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { Elysia, t } from 'elysia';
|
||||
import { prisma } from './prisma';
|
||||
|
||||
export const posts = new Elysia({ prefix: '/posts' })
|
||||
.get('/', async () => {
|
||||
return await prisma.post.findMany({
|
||||
include: { user: true },
|
||||
orderBy: { createdAt: 'desc' }
|
||||
});
|
||||
})
|
||||
.get('/:id', async ({ params: { id } }) => {
|
||||
return await prisma.post.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
user: true,
|
||||
comments: {
|
||||
include: {
|
||||
user: true
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc'
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
.post('/', async ({ body }) => {
|
||||
return await prisma.post.create({
|
||||
data: body
|
||||
});
|
||||
}, {
|
||||
body: t.Object({
|
||||
title: t.String(),
|
||||
content: t.String(),
|
||||
userId: t.String(),
|
||||
published: t.Optional(t.Boolean())
|
||||
})
|
||||
});
|
||||
3
backend/src/prisma.ts
Normal file
3
backend/src/prisma.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
export const prisma = new PrismaClient();
|
||||
41
backend/src/resources.ts
Normal file
41
backend/src/resources.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { Elysia, t } from 'elysia';
|
||||
import { prisma } from './prisma';
|
||||
|
||||
export const resources = new Elysia({ prefix: '/resources' })
|
||||
.get('/', async () => {
|
||||
return await prisma.resource.findMany({
|
||||
orderBy: { createdAt: 'desc' }
|
||||
});
|
||||
})
|
||||
.post('/', async ({ body }) => {
|
||||
return await prisma.resource.create({
|
||||
data: body
|
||||
});
|
||||
}, {
|
||||
body: t.Object({
|
||||
title: t.String(),
|
||||
description: t.Optional(t.String()),
|
||||
url: t.String(),
|
||||
icon: t.Optional(t.String()),
|
||||
category: t.Optional(t.String())
|
||||
})
|
||||
})
|
||||
.put('/:id', async ({ params: { id }, body }) => {
|
||||
return await prisma.resource.update({
|
||||
where: { id },
|
||||
data: body
|
||||
});
|
||||
}, {
|
||||
body: t.Object({
|
||||
title: t.Optional(t.String()),
|
||||
description: t.Optional(t.String()),
|
||||
url: t.Optional(t.String()),
|
||||
icon: t.Optional(t.String()),
|
||||
category: t.Optional(t.String())
|
||||
})
|
||||
})
|
||||
.delete('/:id', async ({ params: { id } }) => {
|
||||
return await prisma.resource.delete({
|
||||
where: { id }
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user