mirror of
https://github.com/seejohnrun/haste-server.git
synced 2024-11-01 03:21:21 +00:00
fix for server
This commit is contained in:
parent
9c2f1d24ba
commit
30c1c486f6
1 changed files with 107 additions and 112 deletions
|
@ -20,15 +20,9 @@ if (config.logging) {
|
|||
addLogging(config)
|
||||
}
|
||||
|
||||
let documentHandler: DocumentHandler
|
||||
|
||||
buildDocumenthandler(config).then((handler) => {
|
||||
documentHandler = handler
|
||||
})
|
||||
|
||||
// Compress the static javascript assets
|
||||
if (config.recompressStaticAssets) {
|
||||
|
||||
buildDocumenthandler(config).then((documentHandler: DocumentHandler) => {
|
||||
// Compress the static javascript assets
|
||||
if (config.recompressStaticAssets) {
|
||||
const list = fs.readdirSync(getStaticDirectory(__dirname))
|
||||
for (let j = 0; j < list.length; j += 1) {
|
||||
const item = list[j]
|
||||
|
@ -52,13 +46,13 @@ if (config.recompressStaticAssets) {
|
|||
winston.info(`compressed ${item} into ${dest}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Send the static documents into the preferred store, skipping expirations
|
||||
let documentPath
|
||||
let data
|
||||
// Send the static documents into the preferred store, skipping expirations
|
||||
let documentPath
|
||||
let data
|
||||
|
||||
Object.keys(config.documents).forEach(name => {
|
||||
Object.keys(config.documents).forEach(name => {
|
||||
documentPath = config.documents[name]
|
||||
data = fs.readFileSync(documentPath, 'utf8')
|
||||
winston.info('loading static document', { name, path: documentPath })
|
||||
|
@ -78,65 +72,66 @@ Object.keys(config.documents).forEach(name => {
|
|||
path: documentPath,
|
||||
})
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
const app: Express = express()
|
||||
const app: Express = express()
|
||||
|
||||
// Rate limit all requests
|
||||
if (config.rateLimits) {
|
||||
// Rate limit all requests
|
||||
if (config.rateLimits) {
|
||||
config.rateLimits.end = true
|
||||
app.use(connectRateLimit(config.rateLimits))
|
||||
}
|
||||
}
|
||||
|
||||
// get raw documents - support getting with extension
|
||||
app.get('/raw/:id', async (request, response) =>
|
||||
// get raw documents - support getting with extension
|
||||
app.get('/raw/:id', async (request, response) =>
|
||||
documentHandler.handleRawGet(request, response),
|
||||
)
|
||||
)
|
||||
|
||||
app.head('/raw/:id', (request, response) =>
|
||||
app.head('/raw/:id', (request, response) =>
|
||||
documentHandler.handleRawGet(request, response),
|
||||
)
|
||||
)
|
||||
|
||||
// // add documents
|
||||
app.post('/documents', (request, response) =>
|
||||
// // add documents
|
||||
app.post('/documents', (request, response) =>
|
||||
documentHandler.handlePost(request, response),
|
||||
)
|
||||
)
|
||||
|
||||
// get documents
|
||||
app.get('/documents/:id', (request, response) =>
|
||||
// get documents
|
||||
app.get('/documents/:id', (request, response) =>
|
||||
documentHandler.handleGet(request, response),
|
||||
)
|
||||
)
|
||||
|
||||
app.head('/documents/:id', (request, response) =>
|
||||
app.head('/documents/:id', (request, response) =>
|
||||
documentHandler.handleGet(request, response),
|
||||
)
|
||||
)
|
||||
|
||||
// Otherwise, try to match static files
|
||||
app.use(
|
||||
// Otherwise, try to match static files
|
||||
app.use(
|
||||
connectSt({
|
||||
path: getStaticDirectory(__dirname),
|
||||
content: { maxAge: config.staticMaxAge },
|
||||
passthrough: true,
|
||||
index: false,
|
||||
}),
|
||||
)
|
||||
)
|
||||
|
||||
// Then we can loop back - and everything else should be a token,
|
||||
// so route it back to /
|
||||
app.get('/:id', (request: Request, response, next) => {
|
||||
// Then we can loop back - and everything else should be a token,
|
||||
// so route it back to /
|
||||
app.get('/:id', (request: Request, response, next) => {
|
||||
request.sturl = '/'
|
||||
next()
|
||||
})
|
||||
})
|
||||
|
||||
// And match index
|
||||
app.use(
|
||||
// And match index
|
||||
app.use(
|
||||
connectSt({
|
||||
path: getStaticDirectory(__dirname),
|
||||
content: { maxAge: config.staticMaxAge },
|
||||
index: 'index.html',
|
||||
}),
|
||||
)
|
||||
)
|
||||
|
||||
app.listen(config.port, config.host, () => {
|
||||
app.listen(config.port, config.host, () => {
|
||||
winston.info(`listening on ${config.host}:${config.port}`)
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue