Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions api_routes/gpt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const router = require('express').Router();

router.route('/auth')
.post(async (req, res) => {
const { pw } = req.body;

if (pw !== process.env.GPT_PASSWORD) {
res.send({
status: 'error',
message: 'Incorrect password.',
});
return;
}

await new Promise((resolve) => setTimeout(resolve, 2000));

res.send({
status: 'success',
token: 'secretToken',
});
});

module.exports = router;
3 changes: 3 additions & 0 deletions api_routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ const robokache = require('./robokache');
const { handleAxiosError } = require('./utils');
const services = require('./services');
const external_apis = require('./external');
const gpt_auth = require('./gpt');

const samples = JSON.parse(fs.readFileSync(path.join(__dirname, './sample-query-cache.json')));

router.use('/', external_apis);

router.use('/robokache', robokache.router);

router.use('/gpt', gpt_auth);

router.route('/quick_answer')
.post(async (req, res) => {
// if this is a sample query, load the response from the cache JSON:
Expand Down
133 changes: 106 additions & 27 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"d3-force": "^2.1.1",
"dotenv": "^8.6.0",
"express": "^4.17.3",
"express-rate-limit": "^6.8.1",
"idb-keyval": "^5.0.6",
"js-yaml": "^3.14.0",
"lodash": "^4.17.21",
Expand Down
19 changes: 19 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const bodyParser = require('body-parser');
// standard express logger
const morgan = require('morgan');
const path = require('path');
const rateLimit = require('express-rate-limit');

// load env variables
dotenv.config();
Expand All @@ -19,8 +20,25 @@ const PORT = process.env.PORT || 7080;
axios.defaults.maxContentLength = Infinity;
axios.defaults.maxBodyLength = Infinity;

// GPT auth: 300 reqs/hr
const gptAuthLimiter = rateLimit({
windowMs: 1 * 60 * 60 * 1000, // 1 hour
max: 300, // 300 request per window
standardHeaders: true,
legacyHeaders: false,
});
// GPT: 60 req/min
const gptLimiter = rateLimit({
windowMs: 1 * 60 * 1000, // 1 minute
max: 60, // 60 request per window
standardHeaders: true,
legacyHeaders: false,
});

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json({ limit: '4000mb' }));
app.use('/api/gpt', gptLimiter);
app.use('/api/gpt/auth', gptAuthLimiter);
app.use(morgan('dev'));

// Add routes
Expand All @@ -36,5 +54,6 @@ app.get('*', (req, res) => {
});

app.listen(PORT, () => {
// eslint-disable-next-line no-console
console.log(`🌎 ==> qgraph running on port ${PORT}!`);
});
Loading