-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
67 lines (55 loc) · 1.82 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import dotenv from 'dotenv';
dotenv.config();
import express, {Application, Request, Response} from 'express';
import DatabaseClient from "./util/database.util";
import EmbeddingUtil from "./util/embedding.util";
import cors from 'cors';
const PORT = process.env.PORT || 3000;
const app: Application = express();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({extended: true}));
app.post('/post', async (req: Request, res: Response) => {
const {title, content} = req.body;
if (!title || !content)
return res.status(400).json({
message: 'Title and content are required'
});
try {
const embedding = await EmbeddingUtil.generateEmbedding(title);
console.log(embedding, embedding.length);
const post = await DatabaseClient.createPost(title, content, embedding);
return res.status(201).json({
message: 'Post created successfully',
data: post
});
} catch (error) {
console.error(error);
return res.status(500).json({
message: 'Internal server error'
});
}
});
app.post('/post/recommend', async (req: Request, res: Response) => {
const {title} = req.body;
if (!title)
return res.status(400).json({
message: 'Title is required'
});
try {
const embedding = await EmbeddingUtil.generateEmbedding(title);
const posts = await DatabaseClient.getRecommendedPosts(embedding);
return res.status(200).json({
message: 'Posts retrieved successfully',
data: posts
});
} catch (error) {
console.error(error);
return res.status(500).json({
message: 'Internal server error'
});
}
});
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});