-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (61 loc) · 2.16 KB
/
index.js
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
68
import express from 'express';
import helmet from 'helmet';
import cors from 'cors';
import compression from 'compression';
import { Database } from './infrastructure/database/db';
import { User } from './infrastructure/database/models/user';
import { Message } from './infrastructure/database/models/message';
import { Crypto } from './utils/crypto';
import { Math } from './utils/math';
const app = express();
app.use(helmet());
app.use(cors());
app.use(compression());
app.use(express.json());
const database = new Database('mongodb://localhost:27017/cosmia', 'redis://localhost:6379');
await database.connect();
app.post('/register', async (req, res) => {
const { username, email, password } = req.body;
try {
const user = await User.createUser(username, email, password);
res.status(201).json({ message: 'User created successfully' });
} catch (error) {
res.status(500).json({ message: 'Error creating user' });
}
});
app.post('/login', async (req, res) => {
const { username, password } = req.body;
try {
const user = await User.getUserByUsername(username);
if (!user) {
res.status(401).json({ message: 'Invalid username or password' });
} else if (!(await user.authenticate(password))) {
res.status(401).json({ message: 'Invalid username or password' });
} else {
res.status(200).json({ message: 'Login successful' });
}
} catch (error) {
res.status(500).json({ message: 'Error logging in' });
}
});
app.post('/send-message', async (req, res) => {
const { from, to, content, type, priority } = req.body;
try {
const message = await Message.createMessage(from, to, content, type, priority);
res.status(201).json({ message: 'Message sent successfully' });
} catch (error) {
res.status(500).json({ message: 'Error sending message' });
}
});
app.get('/messages', async (req, res) => {
const userId = req.query.userId;
try {
const messages = await Message.getMessagesForUser(userId);
res.status(200).json(messages);
} catch (error) {
res.status(500).json({ message: 'Error retrieving messages' });
}
});
app.listen(3000, () => {
console.log('Cosmia Core API listening on port 3000');
});