-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
67 lines (61 loc) · 2.27 KB
/
app.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
const express = require('express');
const mysql2 = require('mysql2/promise');
const config = require('./config.js');
const redis = require('./functions/redis')
const mysql = require('./functions/mysql');
const fs = require('fs');
(async () => {
//Create application
const http = new express();
global.http = http;
http.set('view engine', 'ejs');
http.use((req, res, next) => {
console.log(`[${req.method.toUpperCase()}]`, req.url, 'from', req.ip);
next();
});
fs.readdir('./routes', (err, files) => {
if(err) console.error(err);
console.log(`${files.length} endpoints loaded!`);
let jsFile = files.filter(f => f.split('.').pop() === 'js');
if(jsFile.length <= 0){
return console.log('No EndPoints!')
}
jsFile.forEach((f, i) => {
let endPoint = require(`./routes/${f}`);
console.log(`${f} endPoint loaded!`);
http.use(endPoint);
})
})
//Connect to mysql
let connection = await mysql.Init(config.mysql.host, config.mysql.user, config.mysql.password,config.mysql.database, () => {
console.log('[I] Mysql connected!');
}).then(async connection => {
try {
await connection.ping();
console.log('[I] Database connected')
} catch (error) {
console.log(`Connection error: ${error}`)
}
});
/**
@description OLD code
const connection = await mysql2.createPool({
host:config.mysql.host, user: config.mysql.user, password: config.mysql.password, database: config.mysql.database,
}, () => {
console.log('[I] Database connected')
});*/
global.mysql = connection;
/**
* @description OLD CODE
* const redis = await redisclient.createClient(config.redis.port, config.redis.host, {}, () => {
* console.log('[I] Redis connected')
* })
*/
let rediscnt = redis.init({host: config.redis.host, port: config.redis.port, db: config.redis.database}).then(
console.log('[I] redis connected!')
);
global.redis = rediscnt;
http.listen(config.web.port, config.web.hostname, () => {
console.log(`[I] Listening for clients on http://${config.web.hostname}:${config.web.port}`)
});
})()