1
0
Fork 0
mirror of https://github.com/seejohnrun/haste-server.git synced 2024-11-22 20:51:21 +00:00

fix for server

This commit is contained in:
Yusuf Yilmaz 2022-06-02 12:27:43 +02:00
parent 9c2f1d24ba
commit 30c1c486f6

View file

@ -20,15 +20,9 @@ if (config.logging) {
addLogging(config) addLogging(config)
} }
let documentHandler: DocumentHandler buildDocumenthandler(config).then((documentHandler: DocumentHandler) => {
// Compress the static javascript assets
buildDocumenthandler(config).then((handler) => { if (config.recompressStaticAssets) {
documentHandler = handler
})
// Compress the static javascript assets
if (config.recompressStaticAssets) {
const list = fs.readdirSync(getStaticDirectory(__dirname)) const list = fs.readdirSync(getStaticDirectory(__dirname))
for (let j = 0; j < list.length; j += 1) { for (let j = 0; j < list.length; j += 1) {
const item = list[j] const item = list[j]
@ -52,13 +46,13 @@ if (config.recompressStaticAssets) {
winston.info(`compressed ${item} into ${dest}`) winston.info(`compressed ${item} into ${dest}`)
} }
} }
} }
// Send the static documents into the preferred store, skipping expirations // Send the static documents into the preferred store, skipping expirations
let documentPath let documentPath
let data let data
Object.keys(config.documents).forEach(name => { Object.keys(config.documents).forEach(name => {
documentPath = config.documents[name] documentPath = config.documents[name]
data = fs.readFileSync(documentPath, 'utf8') data = fs.readFileSync(documentPath, 'utf8')
winston.info('loading static document', { name, path: documentPath }) winston.info('loading static document', { name, path: documentPath })
@ -78,65 +72,66 @@ Object.keys(config.documents).forEach(name => {
path: documentPath, path: documentPath,
}) })
} }
}) })
const app: Express = express() const app: Express = express()
// Rate limit all requests // Rate limit all requests
if (config.rateLimits) { if (config.rateLimits) {
config.rateLimits.end = true config.rateLimits.end = true
app.use(connectRateLimit(config.rateLimits)) app.use(connectRateLimit(config.rateLimits))
} }
// get raw documents - support getting with extension // get raw documents - support getting with extension
app.get('/raw/:id', async (request, response) => app.get('/raw/:id', async (request, response) =>
documentHandler.handleRawGet(request, response), documentHandler.handleRawGet(request, response),
) )
app.head('/raw/:id', (request, response) => app.head('/raw/:id', (request, response) =>
documentHandler.handleRawGet(request, response), documentHandler.handleRawGet(request, response),
) )
// // add documents // // add documents
app.post('/documents', (request, response) => app.post('/documents', (request, response) =>
documentHandler.handlePost(request, response), documentHandler.handlePost(request, response),
) )
// get documents // get documents
app.get('/documents/:id', (request, response) => app.get('/documents/:id', (request, response) =>
documentHandler.handleGet(request, response), documentHandler.handleGet(request, response),
) )
app.head('/documents/:id', (request, response) => app.head('/documents/:id', (request, response) =>
documentHandler.handleGet(request, response), documentHandler.handleGet(request, response),
) )
// Otherwise, try to match static files // Otherwise, try to match static files
app.use( app.use(
connectSt({ connectSt({
path: getStaticDirectory(__dirname), path: getStaticDirectory(__dirname),
content: { maxAge: config.staticMaxAge }, content: { maxAge: config.staticMaxAge },
passthrough: true, passthrough: true,
index: false, index: false,
}), }),
) )
// Then we can loop back - and everything else should be a token, // Then we can loop back - and everything else should be a token,
// so route it back to / // so route it back to /
app.get('/:id', (request: Request, response, next) => { app.get('/:id', (request: Request, response, next) => {
request.sturl = '/' request.sturl = '/'
next() next()
}) })
// And match index // And match index
app.use( app.use(
connectSt({ connectSt({
path: getStaticDirectory(__dirname), path: getStaticDirectory(__dirname),
content: { maxAge: config.staticMaxAge }, content: { maxAge: config.staticMaxAge },
index: 'index.html', index: 'index.html',
}), }),
) )
app.listen(config.port, config.host, () => { app.listen(config.port, config.host, () => {
winston.info(`listening on ${config.host}:${config.port}`) winston.info(`listening on ${config.host}:${config.port}`)
})
}) })