website/server.js

112 lines
2.9 KiB
JavaScript
Raw Normal View History

2019-11-15 16:51:24 +00:00
//! Deps
2019-11-19 06:52:41 +00:00
const express = require('express');
const helmet = require('helmet');
const compression = require('compression');
const cors = require('cors');
const morgan = require('morgan');
const chalk = require('chalk');
2019-11-28 19:03:30 +00:00
const hbs = require('express-handlebars');
const UA = require('ua-parser-js');
2019-11-30 13:14:32 +00:00
const exec = require('shell-exec');
2019-11-28 19:03:30 +00:00
const con = require('./constants');
2019-11-19 06:52:41 +00:00
let { port, hostname } = {
port: 3621
// hostname: 'localhost'
};
2019-11-15 16:51:24 +00:00
2019-11-19 06:52:41 +00:00
const app = express();
2019-11-15 16:51:24 +00:00
app.set('view engine', 'hbs');
2019-11-19 06:52:41 +00:00
app.engine(
'hbs',
hbs({
extname: 'hbs',
defaultView: 'default'
})
);
2019-11-15 16:51:24 +00:00
app.set('json spaces', 4);
2019-11-19 06:52:41 +00:00
app.use('/assets', express.static('./assets'));
2019-11-15 16:51:24 +00:00
app.set('view options', {
2019-11-19 06:52:41 +00:00
layout: false
2019-11-15 16:51:24 +00:00
});
app.use(express.json());
2019-11-19 06:52:41 +00:00
app.use(
express.urlencoded({
extended: true
})
);
2019-11-15 16:51:24 +00:00
app.use(helmet());
app.use(compression());
app.use(cors());
// Logging
app.use(
2019-11-19 06:52:41 +00:00
morgan((tokens, req, res) => {
return [
chalk.hex('#34ace0').bold(tokens.method(req, res)),
chalk.hex('#ffb142').bold(tokens.status(req, res)),
chalk.hex('#ff5252').bold(req.hostname + tokens.url(req, res)),
chalk.hex('#2ed573').bold(tokens['response-time'](req, res) + 'ms'),
chalk.hex('#f78fb3').bold('@ ' + tokens.date(req, res))
].join(' ');
})
2019-11-15 16:51:24 +00:00
);
2019-11-28 19:03:30 +00:00
app.use('/portfolio', require('./routes/portfolio'));
2019-11-19 06:52:41 +00:00
app.use('/lore', require('./routes/lore'));
2019-11-28 19:03:30 +00:00
app.use('/art', require('./routes/art'));
2019-11-15 16:51:24 +00:00
2019-11-19 06:52:41 +00:00
module.exports = (client) => {
app.get('/', async (req, res) => {
2019-11-28 19:03:30 +00:00
/* var ua = UA(req.headers['user-agent']);
console.log(ua) */
2019-11-19 06:52:41 +00:00
res.render('index', {
layout: 'main',
2019-11-28 19:03:30 +00:00
name: con.name,
host: req.hostname,
2019-11-30 13:14:32 +00:00
item: [ { name: 'portfolio', source: '/portfolio' } ],
social: [
{ name: 'keybase', color: '33a0ff', link: 'keybase.io/codepupper' },
{ name: 'discord', color: '7289da', link: 'thaldr.in/discord' },
{ name: 'mastodon', color: '3088d4', link: 'pounced-on.me/@code' },
{ name: 'twitter', color: '1DA1F2', link: 'twitter.com/codepupper' },
{ name: 'gitdab', color: 'faacb7', link: 'gitdab.com/y' },
{ name: 'github', color: '333', link: 'github.com/codepupper' },
{ name: 'gitlab', color: 'E24329', link: 'gitlab.com/codepupper' },
{ name: 'twitch', color: '6441a4', link: 'twitch.tv/codepupper' }
]
2019-11-19 06:52:41 +00:00
});
});
2019-11-30 13:14:32 +00:00
app.get('/update', async (req, res) => {
if (req.headers.authorization !== 'Update') {
res.status(400).jsonp({
success: false,
message: 'You are not authorized to access this Endpoint'
});
}
await exec('git pull')
.then((r) => {
console.log('Pulled latest changes');
res.status(200).jsonp({
success: true,
message: 'Successfully pulled latest changes'
});
trying.edit('```fix\n' + r.stdout + '```');
})
.catch((error) => {
res.status(400).jsonp({
success: false,
message: error.message
});
console.error(error);
});
});
2019-11-15 16:51:24 +00:00
2019-11-19 06:52:41 +00:00
app.listen(
port,
/* hostname, */ () => {
2019-11-28 19:03:30 +00:00
console.log(`Listening on ${port}`);
2019-11-19 06:52:41 +00:00
}
);
};