-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
275 lines (219 loc) · 7.15 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
'use strict';
const HTTP_PORT = 9999;
const fs = require('fs');
const express = require('express');
const { v4: uuidv4 } = require('uuid');
var cookieParser = require('cookie-parser');
const morgan = require('morgan');
const winston = require('./config/winstonConfig');
const cors = require('cors');
const axios = require('axios');
const app = express();
const apiKeys = JSON.parse(fs.readFileSync('apikeys.json'));
const http = require('http').Server(app);
const utils = require('./utils/utils');
// database stuff
const low = require('lowdb');
const FileSync = require('lowdb/adapters/FileSync');
const adapter = new FileSync('db.json');
const db = low(adapter);
db.defaults({ requests: {} }).write();
// logging middleware
app.use(morgan('short', {stream: winston.stream}));
app.use(cookieParser());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
//
// Generate a guid everytime we visit the root page to
// keep track of the current request. This is what
// we'll use as the main key in our DB.
//
app.use(function(req, res, next) {
if (req.path === '/' || req.path === '/index.html') {
let guid = uuidv4(); // Generate a new GUID
res.cookie('guid', guid, { maxAge: 900000, httpOnly: false }); // Set the cookie
winston.info('New GUID generated and set as cookie:' + guid);
}
next();
});
app.use(express.static('static')); // static file serve.
app.post('/api/idea', function(req, res) {
//
// Fetch the idea inputted from screen one and
// store this in the db using the guid as the key
//
let guid = req.cookies.guid;
if (!guid) {
winston.error("Missing guid cookie!");
res.status(400);
res.end();
return;
}
winston.info("/api/idea GUID: " + guid);
winston.info(JSON.stringify(req.body));
//
// Create a new db entry and associate the provided message.
//
let dbEntry = {
input: {
idea: req.body.message
},
output: {
logos: []
}
};
winston.info("Storing " + guid + " " + JSON.stringify(dbEntry));
db.set(`requests.${guid}`, dbEntry).write();
res.status(200);
res.end();
});
app.post('/api/extraInfo', function(req, res) {
let guid = req.cookies.guid;
if (!guid) {
winston.error("Missing guid cookie!");
res.status(400);
res.end();
return;
}
winston.info("/api/extrainfo GUID: " + guid);
winston.info(JSON.stringify(req.body));
//
// Store the extra info in the db.
//
db.set(`requests.${guid}.input.businessType`, req.body.businesstype).write();
db.set(`requests.${guid}.input.funding`, req.body.funding).write();
// target revenue
// location (from map)
// Type of business (dropdown)
// estimated startup budget
// how will you fund your startup? loans, self-funding, crowdfunding, etc
const idea = String(db.get(`requests.${guid}.input.idea`));
utils.generateLogos(idea, guid, db, winston); // async call don't care about completion
res.status(200);
res.end();
});
app.post('/api/location', function(req, res) {
let guid = req.cookies.guid;
if (!guid) {
winston.error("Missing guid cookie!");
res.status(400);
res.end();
return;
}
//
// Store the extra location data in the db.
//
winston.info("/api/location GUID: " + guid);
winston.info(JSON.stringify(req.body));
db.set(`requests.${guid}.input.location`, req.body.location).write();
res.status(200);
res.end();
});
app.get('/api/getLogos', function(req, res) {
const guid = req.cookies.guid;
if (!guid) {
winston.error("Missing guid cookie!");
res.status(400);
res.end();
return;
}
winston.info("/api/getLogos GUID: " + guid);
//
// check db to see if image paths exist, if so then return
// otherwise return http try again.
//
db.read();
let logos = JSON.stringify(db.get(`requests.${guid}.output.logos`));
if (logos === undefined) {
res.status(400);
res.end();
return;
}
logos = JSON.parse(logos);
console.log(logos);
if (logos[0] !== undefined) {
res.json(logos);
} else {
res.status(500);
res.end();
}
});
app.get('/api/getVcs', function(req, res) {
const guid = req.cookies.guid;
if (!guid) {
winston.error("Missing guid cookie!");
res.status(400);
res.end();
return;
}
winston.info("/api/getVcs GUID: " + guid);
let idea = JSON.stringify(db.get(`requests.${guid}.input.idea`));
let location = JSON.stringify(db.get(`requests.${guid}.input.location`));
let result = utils.fetchVcs(idea, location, winston);
result.then(function(data) {
winston.info("/api/getVcs async completed: " + JSON.stringify(data));
res.json(data);
});
});
app.get('/api/getOffices', function(req, res) {
const guid = req.cookies.guid;
if (!guid) {
winston.error("Missing guid cookie!");
res.status(400);
res.end();
return;
}
winston.info("/api/getOffices GUID: " + guid);
let location = JSON.stringify(db.get(`requests.${guid}.input.location`));
let result = utils.fetchOffices(location, winston);
result.then(function(data) {
winston.info("/api/getOffices async completed: " + JSON.stringify(data));
res.json(data);
});
});
app.get('/api/getSummary', function(req, res) {
const guid = req.cookies.guid;
if (!guid) {
winston.error("Missing guid cookie!");
res.status(400);
res.end();
return;
}
winston.info("/api/getSummary GUID: " + guid);
let idea = JSON.stringify(db.get(`requests.${guid}.input.idea`));
let location = JSON.stringify(db.get(`requests.${guid}.input.location`));
let prompt = "Given a new business idea: " + String(idea).replaceAll("\"", '') + " based in " + String(location).replaceAll("\"", '') + ", generate a short blurb about current competition in the space. Also generate a short blurb about approximate funding similar startups were able to receive from VCs. Source data from Crunchbase. Keep the entire response under 4 short sentences.";
winston.info(prompt);
axios.post('https://api.perplexity.ai/chat/completions', {
model: 'llama-3.1-sonar-small-128k-chat',
messages: [
{
role: 'user',
content: prompt
}
],
temperature: 0.1, // Adjust temperature for creativity
}, {
headers: {
'Authorization': `Bearer ${apiKeys['perplexity']}`,
'Content-Type': 'application/json',
},
})
.then(response => {
const message = response.data.choices[0].message.content;
winston.info(message);
res.json({message: message});
})
.catch(error => {
winston.error(error.message);
res.status(500);
res.end()
});
});
app.get('/getMapbox', function(req, res) {
res.json({ apiKey: apiKeys["mapbox"] });
});
http.listen(HTTP_PORT, function() {
winston.info("Listening on port " + HTTP_PORT);
});