-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (77 loc) · 2.37 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
/*
* Module dependencies
*/
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const dotenv = require('dotenv');
var Filter = require('bad-words'); // TBD, about whether or not to use.
/*
* Load environment variables from .env file, where API keys and passwords are configured.
*/
dotenv.config({ path: '.env' });
/*
* Create Express server.
*/
const app = express();
/*
* Express configuration.
*/
app.use(express.static(__dirname + '/css'));
app.use(express.static(__dirname + '/js'));
app.use(express.static(__dirname + '/img'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('view engine', 'pug');
const http = require('http').Server(app);
const io = require('socket.io')(http);
const port = process.env.PORT || 3000;
/*
* Controllers (route handlers).
*/
const scriptController = require('./controllers/script');
const openaiController = require('./controllers/openai-gpt3')
/**
* Connect to MongoDB.
*/
// mongoose.set('useFindAndModify', false);
// mongoose.set('useCreateIndex', true);
// mongoose.set('useNewUrlParser', true);
// mongoose.set('useUnifiedTopology', true);
mongoose.connect(process.env.MONGODB_URI);
mongoose.connection.on('error', (err) => {
console.error(err);
console.log('%s MongoDB connection error. Please make sure MongoDB is running.', chalk.red('✗'));
process.exit();
});
function isProfane(text) {
const filter = new Filter();
return (filter.isProfane(text));
}
/*
* Primary app routes.
* (In alphabetical order)
*/
app.get('/', (req, res) => {
res.render('home');
});
app.get('/:sessionID', scriptController.getScript);
app.post('/feed', scriptController.postComment);
app.post('/gpt3', openaiController.getResponses);
io.on('connection', (socket) => {
socket.on('chat message', msg => {
io.emit('chat message', msg);
});
socket.on('post comment', msg => {
// console.log(msg);
msg["isProfane"] = isProfane(msg["text"]);
// io.emit('post comment', msg); // emit to all listening sockets
socket.broadcast.emit('post comment', msg); // emit to all listening socketes but the one sending
});
socket.on('error', function(err) {
console.log(err);
});
});
http.listen(port, () => {
console.log(`Socket.IO server running at http://localhost:${port}/`);
});