-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
59 lines (45 loc) · 1.58 KB
/
index.js
File metadata and controls
59 lines (45 loc) · 1.58 KB
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
const superagent = require('superagent');
const express = require('express');
const mysql = require('promise-mysql');
const cors = require('cors');
const API_HOST = process.env.API_HOST;
// Express middleware
const bodyParser = require('body-parser');
const morgan = require('morgan');
const checkLoginToken = require('./lib/check-login-token.js');
// Data loader
const FlorifyDataLoader = require('./lib/florify.js');
const cronJob = require('cron').CronJob;
//ping reminders at 6pm every day
const textJob = new cronJob( '0 18 * * *', function(){
return superagent
.get(`${API_HOST}/reminders`)
.end()
}, null, true);
// Controllers
const authController = require('./controllers/auth.js');
const plantsController = require('./controllers/plants.js');
const reminderController = require('./controllers/reminders.js');
// Database / data loader initialization
const connection = mysql.createPool(process.env.DATABASE_URL);
const dataLoader = new FlorifyDataLoader(connection);
const app = express();
app.use(cors({
allowedOrigins: ['*']
}));
// Express initialization
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(checkLoginToken(dataLoader));
app.use('/auth', authController(dataLoader));
app.use('/plants', plantsController(dataLoader));
app.use('/reminders', reminderController(dataLoader));
// Start the server
const port = process.env.PORT || 3001;
app.listen(port, () => {
if (process.env.C9_HOSTNAME) {
console.log(`Web server is listening on https://${process.env.C9_HOSTNAME}`);
} else {
console.log(`Web server is listening on http://localhost:${port}`);
}
});