-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.ts
More file actions
executable file
·82 lines (73 loc) · 2.7 KB
/
app.ts
File metadata and controls
executable file
·82 lines (73 loc) · 2.7 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env nodejs
import axios from 'axios';
import express, { Request, Response } from "express";
import fs from 'fs';
import logger from 'morgan';
import path from 'path';
import Database from './database/database';
/** Routes */
import about from './routes/about/about';
import dashboard from './routes/admin/dashboard/dashboard';
import login from './routes/admin/login/login';
import events from './routes/events/events';
import join from './routes/join/join';
import merch from './routes/merch/merch';
import partners from './routes/partners/partners';
import { logger as LOGGER } from './utils/logger';
const app = express();
const db = new Database();
const tokens = JSON.parse(fs.readFileSync('config/tokens.json', 'utf8'));
/** Logger for HTTP requests. */
app.use(logger('dev'));
/** application/x-www-form-urlencoded. */
app.use(express.urlencoded({ extended: false }));
/** Handles parsing for JSON data. */
app.use(express.json());
/** Set directory for static files. */
app.use(express.static(path.join(__dirname, 'public')));
/** Set the view template path. */
app.set('views', path.join(__dirname, 'views'));
/** Set view engine. */
app.set('view engine', 'pug');
/** Index page */
app.get('/', async (req: Request, res: Response) => {
LOGGER.info(`[${req.method}] - ${req.path} - Fetching latest tweets`);
const url = 'https://api.twitter.com/1.1/statuses/user_timeline.json'
+'?screen_name=enucs&exclude_replies=true&include_rts=false&count=3';
const bearerToken = 'bearer ' + tokens.twitter;
let tweets = [];
try {
const instance = await axios({ url: url, headers: { 'Authorization': bearerToken }});
tweets = instance.data.map((tweet: any) => {
return {
body: tweet.text,
created_at: new Date(tweet.created_at)
.toLocaleString('en-GB', {
day: '2-digit',
month: 'short',
year: 'numeric',
hour: '2-digit',
minute: '2-digit'
}),
handle: tweet.user.screen_name
};
});
} catch (e) {
LOGGER.error('Failed to read data from Twitter API', e);
}
let events = await db.getFutureEvents(6);
res.render('index', {
events: events.map(event => event.prettifyDates()),
tweets: tweets || null
});
});
app.use('/about', about.router);
app.use('/events', events.router);
app.use('/partners', partners.router);
app.use('/merch', merch.router);
app.use('/join', join.router);
app.use('/', login.router);
app.use('/admin/dashboard', dashboard.router);
app.listen(3000, () => {
LOGGER.info('Listening on port 3000...');
});