-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
151 lines (128 loc) · 3.8 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const express = require('express')
const helmet = require('helmet')
const cors = require('cors')
const { MongoClient } = require('mongodb')
console.log('Starting server...')
require('dotenv').config()
console.log('process.env.OPENAI_API_KEY', process.env.OPENAI_API_KEY)
const { Configuration, OpenAIApi } = require('openai');
const openai = new OpenAIApi(new Configuration({
apiKey: process.env.OPENAI_API_KEY
}));
const dburi = 'mongodb://127.0.0.1:27017'
const app = express()
const port = 3000
app.use(cors())
app.use(helmet.contentSecurityPolicy({
directives: {
defaultSrc: ["'self'"],
}
}))
app.use(
helmet.contentSecurityPolicy({
useDefaults: false,
"upgrade-insecure-requests": true,
directives: {
"default-src": [
"'self'"
]
}
})
)
app.use(express.json())
const withdb = async (cb) => {
try {
const client = await MongoClient.connect(dburi, {
useNewUrlParser: true,
useUnifiedTopology: true
})
const db = client.db('test')
return cb({
moods: db.collection('moods'),
thoughts: db.collection('thoughts'),
journal: db.collection('journal')
})
} catch (error) {
console.log(error)
return error
}
}
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.get("/test", (req, res) => {
console.log({ origin: req.headers.origin });
res.json({ message: "Hello from the world!" });
});
app.post("/test", (req, res) => {
console.log(req.body);
res.json({
message: `Hello sender! I got your POST request. This is what you sent me: ${req.body.post}`,
});
});
app.get('/moods', async (req, res) => {
console.log('GET /moods')
const moods = await withdb(({ moods }) => moods.find().toArray())
res.json(moods)
console.log(moods)
})
app.post('/moods', async (req, res) => {
const mood = req.body
console.log('POST /moods', mood)
await withdb(({ moods }) => moods.insertOne(mood))
res.json({ message: 'Mood saved!' })
})
app.get('/thoughts', async (req, res) => {
console.log('GET /thoughts')
const thoughts = await withdb(({ thoughts }) => thoughts.find().toArray())
res.json(thoughts)
console.log(thoughts)
})
app.post('/thoughts', async (req, res) => {
const thought = req.body
console.log('POST /thoughts', thought)
await withdb(({ thoughts }) => thoughts.insertOne(thought))
res.json({ message: 'Thought saved!' })
})
app.get('journals', async (req, res) => {
console.log('GET /journals')
const journal = await withdb(({ journal }) => journal.find().toArray())
res.json(journal)
console.log(journal)
})
app.post('/journals', async (req, res) => {
const journal = req.body
console.log('POST /journals', journal)
await withdb(({ journal }) => journal.insertOne(journal))
res.json({ message: 'Journal saved!' })
})
// app.post('/chat', async (req, res) => {
// const messages = req.body
// console.log('POST /chat', messages)
// const response = await openai.createChatCompletion({
// model: process.env.OPENAI_MODEL,
// // model: 'gpt-3.5-turbo',
// messages: messages.map(m=> {return {role:m.role, content:m.content}})
// })
// let message = response.data.choices[0].message
// console.log(message)
// res.json({role: message.role, content: message.content, time: new Date()})
// })
app.post('/chat', async (req, res) => {
const messages = req.body
console.log('POST /chat', messages)
const response = await fetch('localhost:5000/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(messages)
})
const message = await response.json()
console.log(message)
res.json({role: message.role, content: message.content, time: new Date()})
})
app.route('/favicon.ico').get((req, res) => res.status(204));
app.listen(port, () => {
console.log(`server listening at http://0.0.0.0:${port}`)
})