forked from Gulivertx/cbatmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
135 lines (115 loc) · 3.86 KB
/
server.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
128
129
130
131
132
133
134
135
const express = require('express');
const createError = require('http-errors');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const cors = require('cors');
const compression = require('compression');
const logger = require('morgan');
const fs = require('fs');
const path = require('path');
const rfs = require('rotating-file-stream');
const os = require('os');
const DarkSkyApi = require('dark-sky-api');
const common = require('./config/common');
const config = common.config();
const appInfo = common.info();
const apiConfig = require('./config/api');
const app = express();
/** Configure DarkSkyApi **/
DarkSkyApi.apiKey = apiConfig.dark_sky.secret_key;
DarkSkyApi.proxy = true;
/** Logs configuration **/
// Set logs directory
const logsDir = path.join(__dirname, 'logs');
// Ensure logs directory exists
fs.existsSync(logsDir) || fs.mkdirSync(logsDir);
// Create a rotating write stream
const accessLogStream = rfs('access.log', {
interval: '1d', // rotate daily
path: logsDir
});
/** View engine setup **/
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
/** Express configuration **/
app.use(compression());
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(logger(config.log, config.log === 'dev' ? null : {stream: accessLogStream}));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', (req, res, next) => {
res.render('index', {
title: appInfo.name,
description: appInfo.description,
version: appInfo.version,
author: appInfo.author,
keywords: appInfo.keywords
});
});
app.get('/info', (req, res, next) => {
res.json({
name: appInfo.name,
description: appInfo.description,
version: appInfo.version,
author: appInfo.author,
process: {
platform: os.platform(),
arch: os.arch(),
release: os.release(),
hostname: os.hostname(),
type: os.type(),
cpuload: os.loadavg(),
usedmem: Math.round((os.totalmem() - os.freemem()) / 1024 / 1024),
totalmem: Math.round(os.totalmem() / 1024 / 1024),
uptime: getTimeString(os.uptime() * 1000)
}
})
});
app.get('/darksky/:latitude/:longitude/:lang/:units', (req, res, next) => {
if (!req.params.latitude ||
!req.params.longitude ||
!req.params.lang ||
!req.params.units) return res.status(400).json({status: 'error', msg: 'Bad request'});
DarkSkyApi.units = req.params.units; // default 'us'
DarkSkyApi.language = req.params.lang; // default 'en'
const position = {
latitude: req.params.latitude,
longitude: req.params.longitude
};
DarkSkyApi.loadItAll('hourly,flags', position)
.then(result => {
res.json(result)
})
});
/** Catch 404 and forward to error handler **/
app.use((req, res, next) => {
next(createError(404));
});
/** Errors handler **/
app.use((err, req, res, next) => {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
app.listen(3000, () => {
console.log('Server running on http://localhost:3000')
});
const getTimeString = (milli) => {
let d, h, m, s, ms;
s = Math.floor(milli / 1000);
m = Math.floor(s / 60);
s = s % 60; s = s < 10 ? '0' + s : s;
h = Math.floor(m / 60);
m = m % 60; m = m < 10 ? '0' + m : m;
d = Math.floor(h / 24);
h = h % 24; h = h < 10 ? '0' + h : h;
ms = Math.floor((milli % 1000) * 1000) / 1000;
return `${d}d ${h}:${m}:${s}`;
};