Skip to content

Commit

Permalink
morgan access log, close #1
Browse files Browse the repository at this point in the history
  • Loading branch information
kucingbasah737 committed Nov 27, 2023
1 parent a91875e commit b3db2f8
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 20 deletions.
14 changes: 14 additions & 0 deletions lib/compose-full-target-url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const urlJoin = require('url-join');

const PROTOCOL = process.env.HOSTNAME_PROTOCOL || 'https';
const PORT = process.env.HOSTNAME_PORT || '';

module.exports = (hostname, path) => urlJoin(
[
PROTOCOL,
'://',
hostname,
PORT || '',
].join(''),
path,
);
38 changes: 38 additions & 0 deletions lib/webserver/access-logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const morgan = require('morgan');
const rfs = require('rotating-file-stream');
const composeFullTargetUrl = require('../compose-full-target-url');

/**
*
* @param {import('express').Application} app
* @param {string} logDir
*/
module.exports = (app, logDir) => {
const stream = rfs.createStream('access.log', {
// size: '10M', // rotate every 10 MegaBytes written
interval: '1d', // rotate daily
// compress: 'gzip' // compress rotated files
maxFiles: Number(process.env.WEB_KEEP_ACESSS_FILES) || 45,
path: logDir,
});

morgan.token('xid', (req, res) => res.locals.xid || '-');
morgan.token('email', (req) => req.session?.email || '-');
// morgan.token('hostname', (req) => req.hostname || '-');
morgan.token(
'url',
/**
*
* @param {import('express').Request} req
* @returns
*/
(req) => composeFullTargetUrl(req.hostname, req.originalUrl),
);

app.use(morgan(
':remote-addr - :remote-user [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent" :email :xid',
{
stream,
},
));
};
45 changes: 25 additions & 20 deletions lib/webserver/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const redis = require('redis');
const dayjs = require('dayjs');

const logger = require('../logger');
const accessLogger = require('./access-logger');
const targetLookup = require('./target-lookup');
const validateSession = require('./validate-session');
const composeTrustProxy = require('./compose-trust-proxies');
Expand Down Expand Up @@ -45,7 +46,14 @@ module.exports = async () => {
logger.verbose(`${MODULE_NAME} 1A4F8EE0: Will use cf-connecting-ip as requester ip`);
}

logger.verbose(`${MODULE_NAME} A3F66C95: Creating redis client for sesssion store`);
const trustedProxies = await composeTrustProxy();
logger.verbose(`${MODULE_NAME} 1DFD2897: Trusting proxies`, {
trustedProxies,
});
app.set('trust proxy', trustedProxies);

// sessions
logger.verbose(`${MODULE_NAME} A3F66C95: Initializing session store`);
const redisClient = redis.createClient();
await redisClient.connect();

Expand All @@ -61,25 +69,7 @@ module.exports = async () => {
saveUninitialized: false,
secret: process.env.WEB_SESSION_SECRET || uniqid(),
}));

app.use(express.static('./node_modules/bootstrap/dist'));
app.use(express.static('./public/'));
app.use(express.static('./public.local/'));

const nunjucksEnv = nunjucks.configure('views', {
autoescape: true,
noCache: !!process.env.WEB_TEMPLATE_NO_CACHE,
express: app,
});

nunjucksEnv.addFilter('simpleDate', (ts) => dayjs(ts).format('YYYY-MM-DD'));
nunjucksEnv.addFilter('simpleDateTime', (ts) => dayjs(ts).format('YYYY-MM-DD Hh:mm:ssZ[Z]'));

const trustedProxies = await composeTrustProxy();
logger.verbose(`${MODULE_NAME} 1DFD2897: Trusting proxies`, {
trustedProxies,
});
app.set('trust proxy', trustedProxies);
// end of sessions

app.use((req, res, next) => {
const xid = uniqid();
Expand Down Expand Up @@ -111,6 +101,21 @@ module.exports = async () => {
next();
});

app.use(express.static('./node_modules/bootstrap/dist'));
app.use(express.static('./public/'));
app.use(express.static('./public.local/'));

const nunjucksEnv = nunjucks.configure('views', {
autoescape: true,
noCache: !!process.env.WEB_TEMPLATE_NO_CACHE,
express: app,
});

nunjucksEnv.addFilter('simpleDate', (ts) => dayjs(ts).format('YYYY-MM-DD'));
nunjucksEnv.addFilter('simpleDateTime', (ts) => dayjs(ts).format('YYYY-MM-DD Hh:mm:ssZ[Z]'));

accessLogger(app, 'logs');

app.get('/', (req, res) => res.redirect(res.locals.mainPage));

app.use('/dashboard', validateSession, routerDashboard);
Expand Down
68 changes: 68 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@
"express-session": "^1.17.3",
"geoip-lite": "^1.4.8",
"ip-toolkit": "^1.0.10",
"morgan": "^1.10.0",
"mysql2": "^3.6.5",
"nunjucks": "^3.2.4",
"prompt": "^1.3.0",
"redis": "^4.6.11",
"rotating-file-stream": "^3.1.1",
"sd-notify": "^2.8.0",
"simple-git": "^3.21.0",
"uniqid": "^5.4.0",
Expand Down

0 comments on commit b3db2f8

Please sign in to comment.