HTTP request logging format 2
This commit is contained in:
+39
-24
@@ -15,12 +15,14 @@ import { CharacterManager } from '../Components/Character/CharacterManager';
|
||||
import { UserManager } from '../Components/User/UserManager';
|
||||
import { RootComponent } from '../Injector/ServiceDecorator';
|
||||
import { TableDefinitionExporter } from '../Types/Interfaces';
|
||||
import { AdminConf, TableDefiniton } from '../Types/Types';
|
||||
import { AdminConf, TableDefiniton, SomeOf } from '../Types/Types';
|
||||
import { RPCConfigLoader } from '../Components/RPCConfigLoader';
|
||||
import { FrontworkComponent } from '../Types/FrontworkComponent';
|
||||
import { IAdmin } from './Interface';
|
||||
import { Injector } from '../Injector/Injector';
|
||||
import { PubSub } from '../Components/PubSub/PubSub';
|
||||
import { BurstyRateLimiter, RateLimiterMemory, RateLimiterAbstract } from 'rate-limiter-flexible'
|
||||
|
||||
|
||||
@RootComponent({
|
||||
injectable: IAdmin,
|
||||
@@ -67,13 +69,24 @@ export class FrontworkAdmin
|
||||
|
||||
async start() {
|
||||
let port: number = this.config.getConfig().httpPort
|
||||
const rateLimiter = new BurstyRateLimiter(
|
||||
new RateLimiterMemory({
|
||||
points: 2,
|
||||
duration: 1,
|
||||
}),
|
||||
new RateLimiterMemory({
|
||||
keyPrefix: 'burst',
|
||||
points: 60,
|
||||
duration: 60,
|
||||
})
|
||||
)
|
||||
|
||||
await this.makeKnex()
|
||||
const app = await this.makeExpress()
|
||||
const app = await this.makeExpress(rateLimiter)
|
||||
const httpServer = new http.Server(app)
|
||||
await this.startWebsocket(httpServer)
|
||||
httpServer.listen(port)
|
||||
await this.attachAngularSSR(app)
|
||||
await this.attachAngularSSR(app, rateLimiter)
|
||||
getLogger('Admin#startWebsocket').debug("Webserver up on", port)
|
||||
|
||||
await Promise.all(this.frontworkComponents.map(c => c.initialize ? c.initialize() : undefined))
|
||||
@@ -125,51 +138,53 @@ export class FrontworkAdmin
|
||||
}
|
||||
], {
|
||||
errorHandler: (sock, err, rpc, args) => {
|
||||
console.log("RPC", rpc)
|
||||
console.log("ERR", err)
|
||||
console.log("ARGS", args)
|
||||
getLogger("startWebsocket#errorHandler").error("RPC", rpc)
|
||||
getLogger("startWebsocket#errorHandler").error("ERR", err)
|
||||
getLogger("startWebsocket#errorHandler").error("ARGS", args)
|
||||
}
|
||||
})
|
||||
.attach(httpServer)
|
||||
}
|
||||
|
||||
private async makeExpress() {
|
||||
private async makeExpress(rateLimiter : SomeOf<RateLimiterAbstract> = new RateLimiterMemory({ points: 6, duration: 3 })) {
|
||||
if (this.httpServer != null || this.express != null) {
|
||||
getLogger('Admin#startWebserver').warn("Webserver is already running")
|
||||
return
|
||||
}
|
||||
|
||||
this.express = express()
|
||||
|
||||
const distFolder = "../../../../dist"
|
||||
|
||||
this.express.get('*.*', (req, res) => {
|
||||
//console.log('*.*', req.path);
|
||||
try{
|
||||
const filepath = Path.join(__dirname, distFolder, 'browser', decodeURIComponent(req.path))
|
||||
if(!existsSync(filepath)){
|
||||
throw new Error("FAILED REQUEST "+filepath+ " FROM "+req.ip)
|
||||
}
|
||||
res.sendFile(filepath)
|
||||
res.status(200)
|
||||
}catch(e){
|
||||
getLogger('Admin#startWebserver#serveFile').error(String(e))
|
||||
res.send("404 NOT FOUND")
|
||||
res.status(404)
|
||||
}
|
||||
rateLimiter.consume(req.ip)
|
||||
.then(_ => {
|
||||
const filepath = Path.join(__dirname, distFolder, 'browser', decodeURIComponent(req.path))
|
||||
if(!existsSync(filepath)){
|
||||
getLogger('Admin#startWebserver#serveFile').error(String(new Error("404 BAD REQUEST "+filepath+ " FROM "+req.ip)))
|
||||
res.send("404 Not found")
|
||||
res.status(404)
|
||||
return
|
||||
}
|
||||
res.sendFile(filepath)
|
||||
res.status(200)
|
||||
})
|
||||
.catch(_ => {
|
||||
getLogger('Admin#startWebserver#serveFile').error(String(new Error("429 BAD REQUEST "+req.path+" FROM "+req.ip)))
|
||||
res.send('429 Too many requests')
|
||||
res.status(429)
|
||||
})
|
||||
})
|
||||
|
||||
return this.express
|
||||
}
|
||||
|
||||
attachAngularSSR = async (express : express.Application) => {
|
||||
attachAngularSSR = async (express : express.Application, rateLimiter : SomeOf<RateLimiterAbstract> = new RateLimiterMemory({ points: 6, duration: 3 })) => {
|
||||
const distFolder = "../../../../dist"
|
||||
const ngExpressServer = Path.join(__dirname, distFolder, 'server.js')
|
||||
let port: number = this.config.getConfig().httpPort
|
||||
|
||||
try {
|
||||
const req = require(distFolder + "/server.js")
|
||||
await req.attachExpress(express, './dist', getLogger('angularSSR#'))
|
||||
await req.attachExpress(express, './dist', getLogger('angularSSR#'), rateLimiter)
|
||||
getLogger('Admin#startWebserver').debug('Frontend from ' + ngExpressServer + " loaded")
|
||||
} catch (e) {
|
||||
getLogger('Admin#startWebserver').error(e)
|
||||
|
||||
Reference in New Issue
Block a user