Files
speckle-server/packages/server/modules/core/rest/diffUpload.js
T
Iain Sproat 84cb74e8b3 feat(structured logging): implements structured logging for backend (#1217)
* each log line is a json object
* structured logging allows logs to be ingested by machines and the logs to be indexed and queried addresses #1105
* structured logging allows arbitrary properties to be appended to each log line, and ingestion of logs to remain robust
* Structured logging provided by `pino` library
* Add `express-pino-logger` dependency
* Remove `debug`, `morgan`, and `morgan-debug` and replace with structured logging
* `console.log` & `console.error` replaced with structured logging in backend
* Remove `DEBUG` environment variable and replace with `LOG_LEVEL`
- Note that there is a test which reads from a logged line on `stdout`. This is not robust, it would be better to use the childProcess.pid to look up the port number.
* Log errors at points we explicitly send error to Sentry
* Amend indentation of a couple of log messages to align indentation with others
2022-11-25 16:05:05 +00:00

56 lines
1.5 KiB
JavaScript

'use strict'
const zlib = require('zlib')
const cors = require('cors')
const { contextMiddleware } = require('@/modules/shared')
const { validatePermissionsWriteStream } = require('./authUtils')
const {
rejectsRequestWithRatelimitStatusIfNeeded
} = require('@/modules/core/services/ratelimits')
const { hasObjects } = require('../services/objects')
const { logger } = require('@/logging/logging')
module.exports = (app) => {
app.options('/api/diff/:streamId', cors())
app.post('/api/diff/:streamId', cors(), contextMiddleware, async (req, res) => {
const rejected = await rejectsRequestWithRatelimitStatusIfNeeded({
action: 'POST /api/diff/:streamId',
req,
res
})
if (rejected) return rejected
const hasStreamAccess = await validatePermissionsWriteStream(
req.params.streamId,
req
)
if (!hasStreamAccess.result) {
return res.status(hasStreamAccess.status).end()
}
const objectList = JSON.parse(req.body.objects)
logger.info(
`[User ${req.context.userId || '-'}] Diffing ${
objectList.length
} objects for stream ${req.params.streamId}`
)
const response = await hasObjects({
streamId: req.params.streamId,
objectIds: objectList
})
// logger.debug(response)
res.writeHead(200, {
'Content-Encoding': 'gzip',
'Content-Type': 'application/json'
})
const gzip = zlib.createGzip()
gzip.write(JSON.stringify(response))
gzip.flush()
gzip.end()
gzip.pipe(res)
})
}