forked from codeforanchorage/courtbot
-
Notifications
You must be signed in to change notification settings - Fork 10
/
web.js
127 lines (107 loc) · 3.39 KB
/
web.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
var express = require('express');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var cookieSession = require('cookie-session');
var log4js = require("log4js");
var logfmt = require('logfmt');
var courtbot = require('courtbot-engine');
var connections = require('./connectionTypes');
require('./config');
require("courtbot-engine-pg");
require("courtbot-engine-data-oscn")(
process.env.OSCN_COUNTY || "tulsa",
process.env.OSCN_API_URL || "https://oscn-case-api.herokuapp.com"
);
require("courtbot-engine-data-courtbook")({
courtbookUrl: process.env.COURTBOOK_URL,
oauthConfig: {
tokenUrl: process.env.COURTBOOK_OAUTH_TOKEN_URL,
audience: process.env.COURTBOOK_OAUTH_AUDIENCE,
clientId: process.env.COURTBOOK_OAUTH_CLIENT_ID,
clientSecret: process.env.COURTBOOK_OAUTH_SECRET
}
});
var appenders = [
{
"type": "logLevelFilter",
"level": "TRACE",
"appender": {
"type": "console"
}
}
]
if(process.env.LOGENTRIES_TOKEN) {
// log4js.loadAppender("logentries-log4js-appender");
// log4js.addAppender(log4js.appenders["logentries-log4js-appender"]({
// token: process.env.LOGENTRIES_TOKEN
// }));
appenders.push({
"type": "logentries-log4js-appender",
options: {
"token": process.env.LOGENTRIES_TOKEN
}
})
}
log4js.configure({appenders});
const log = log4js.getLogger("courtbot");
var app = express();
// Express Middleware
app.use(logfmt.requestLogger());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(cookieParser(process.env.COOKIE_SECRET));
app.use(cookieSession({
keys: [
process.env.COOKIE_SECRET
]
}));
// Serve testing page on which you can impersonate Twilio
// (but not in production)
if (app.settings.env === 'development') {
//app.use(express.static('public'))
}
// Allows CORS
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
// Enable CORS support for IE8.
app.get('/proxy.html', function(req, res) {
res.send('<!DOCTYPE HTML>\n' + '<script src="http://jpillora.com/xdomain/dist/0.6/xdomain.min.js" master="http://www.courtrecords.alaska.gov"></script>');
});
app.get('/', function(req, res) {
log.info("Homepage request", req);
res.status(200).send('Hello, I am Courtbot. I have a heart of justice and a knowledge of court cases.');
});
const courtbotConfig = {
dbUrl: process.env.DATABASE_URL,
ConsoleREPL: !!process.env.USE_CONSOLE,
reminderDaysOut: process.env.REMINDER_DAYS_OUT,
twilioAccount: process.env.TWILIO_ACCOUNT,
twilioToken: process.env.TWILIO_AUTH_TOKEN,
twilioPhone: process.env.TWILIO_PHONE_NUMBER,
timeZoneName: "America/Chicago"
};
connections.setup(courtbotConfig);
log.info("Courtbot config", courtbotConfig);
app.use("/", courtbot.routes(courtbotConfig));
// Error handling Middleware
app.use(function (err, req, res, next) {
if (!res.headersSent) {
// during development, return the trace to the client for
// helpfulness
log.error("Error: " + err.message, err);
if (app.settings.env !== 'production') {
return res.status(500).send(err.stack)
}
return res.status(500).send('Sorry, internal server error')
}
});
var port = Number(process.env.PORT || 5000);
app.listen(port, function() {
log.info("Listening on " + port);
});
module.exports = app;