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:
parent
9c2f1d24ba
commit
30c1c486f6
1 changed files with 107 additions and 112 deletions
219
src/server.ts
219
src/server.ts
|
@ -20,123 +20,118 @@ if (config.logging) {
|
||||||
addLogging(config)
|
addLogging(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
let documentHandler: DocumentHandler
|
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]
|
||||||
|
if (
|
||||||
|
item.indexOf('.js') === item.length - 3 &&
|
||||||
|
item.indexOf('.min.js') === -1
|
||||||
|
) {
|
||||||
|
const dest = `${item.substring(0, item.length - 3)}.min${item.substring(
|
||||||
|
item.length - 3,
|
||||||
|
)}`
|
||||||
|
const origCode = fs.readFileSync(
|
||||||
|
getStaticItemDirectory(__dirname, item),
|
||||||
|
'utf8',
|
||||||
|
)
|
||||||
|
|
||||||
buildDocumenthandler(config).then((handler) => {
|
fs.writeFileSync(
|
||||||
documentHandler = handler
|
getStaticItemDirectory(__dirname, dest),
|
||||||
})
|
uglify.minify(origCode).code,
|
||||||
|
'utf8',
|
||||||
// Compress the static javascript assets
|
)
|
||||||
if (config.recompressStaticAssets) {
|
winston.info(`compressed ${item} into ${dest}`)
|
||||||
|
}
|
||||||
const list = fs.readdirSync(getStaticDirectory(__dirname))
|
|
||||||
for (let j = 0; j < list.length; j += 1) {
|
|
||||||
const item = list[j]
|
|
||||||
if (
|
|
||||||
item.indexOf('.js') === item.length - 3 &&
|
|
||||||
item.indexOf('.min.js') === -1
|
|
||||||
) {
|
|
||||||
const dest = `${item.substring(0, item.length - 3)}.min${item.substring(
|
|
||||||
item.length - 3,
|
|
||||||
)}`
|
|
||||||
const origCode = fs.readFileSync(
|
|
||||||
getStaticItemDirectory(__dirname, item),
|
|
||||||
'utf8',
|
|
||||||
)
|
|
||||||
|
|
||||||
fs.writeFileSync(
|
|
||||||
getStaticItemDirectory(__dirname, dest),
|
|
||||||
uglify.minify(origCode).code,
|
|
||||||
'utf8',
|
|
||||||
)
|
|
||||||
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 })
|
||||||
|
|
||||||
if (data) {
|
if (data) {
|
||||||
documentHandler.store?.set(
|
documentHandler.store?.set(
|
||||||
name,
|
name,
|
||||||
data,
|
data,
|
||||||
cb => {
|
cb => {
|
||||||
winston.debug('loaded static document', { success: cb })
|
winston.debug('loaded static document', { success: cb })
|
||||||
},
|
},
|
||||||
true,
|
true,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
winston.warn('failed to load static document', {
|
winston.warn('failed to load static document', {
|
||||||
name,
|
name,
|
||||||
path: documentPath,
|
path: documentPath,
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
const app: Express = express()
|
||||||
|
|
||||||
|
// Rate limit all requests
|
||||||
|
if (config.rateLimits) {
|
||||||
|
config.rateLimits.end = true
|
||||||
|
app.use(connectRateLimit(config.rateLimits))
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
// get raw documents - support getting with extension
|
||||||
const app: Express = express()
|
app.get('/raw/:id', async (request, response) =>
|
||||||
|
documentHandler.handleRawGet(request, response),
|
||||||
// Rate limit all requests
|
)
|
||||||
if (config.rateLimits) {
|
|
||||||
config.rateLimits.end = true
|
app.head('/raw/:id', (request, response) =>
|
||||||
app.use(connectRateLimit(config.rateLimits))
|
documentHandler.handleRawGet(request, response),
|
||||||
}
|
)
|
||||||
|
|
||||||
// get raw documents - support getting with extension
|
// // add documents
|
||||||
app.get('/raw/:id', async (request, response) =>
|
app.post('/documents', (request, response) =>
|
||||||
documentHandler.handleRawGet(request, response),
|
documentHandler.handlePost(request, response),
|
||||||
)
|
)
|
||||||
|
|
||||||
app.head('/raw/:id', (request, response) =>
|
// get documents
|
||||||
documentHandler.handleRawGet(request, response),
|
app.get('/documents/:id', (request, response) =>
|
||||||
)
|
documentHandler.handleGet(request, response),
|
||||||
|
)
|
||||||
// // add documents
|
|
||||||
app.post('/documents', (request, response) =>
|
app.head('/documents/:id', (request, response) =>
|
||||||
documentHandler.handlePost(request, response),
|
documentHandler.handleGet(request, response),
|
||||||
)
|
)
|
||||||
|
|
||||||
// get documents
|
// Otherwise, try to match static files
|
||||||
app.get('/documents/:id', (request, response) =>
|
app.use(
|
||||||
documentHandler.handleGet(request, response),
|
connectSt({
|
||||||
)
|
path: getStaticDirectory(__dirname),
|
||||||
|
content: { maxAge: config.staticMaxAge },
|
||||||
app.head('/documents/:id', (request, response) =>
|
passthrough: true,
|
||||||
documentHandler.handleGet(request, response),
|
index: false,
|
||||||
)
|
}),
|
||||||
|
)
|
||||||
// Otherwise, try to match static files
|
|
||||||
app.use(
|
// Then we can loop back - and everything else should be a token,
|
||||||
connectSt({
|
// so route it back to /
|
||||||
path: getStaticDirectory(__dirname),
|
app.get('/:id', (request: Request, response, next) => {
|
||||||
content: { maxAge: config.staticMaxAge },
|
request.sturl = '/'
|
||||||
passthrough: true,
|
next()
|
||||||
index: false,
|
})
|
||||||
}),
|
|
||||||
)
|
// And match index
|
||||||
|
app.use(
|
||||||
// Then we can loop back - and everything else should be a token,
|
connectSt({
|
||||||
// so route it back to /
|
path: getStaticDirectory(__dirname),
|
||||||
app.get('/:id', (request: Request, response, next) => {
|
content: { maxAge: config.staticMaxAge },
|
||||||
request.sturl = '/'
|
index: 'index.html',
|
||||||
next()
|
}),
|
||||||
})
|
)
|
||||||
|
|
||||||
// And match index
|
app.listen(config.port, config.host, () => {
|
||||||
app.use(
|
winston.info(`listening on ${config.host}:${config.port}`)
|
||||||
connectSt({
|
})
|
||||||
path: getStaticDirectory(__dirname),
|
|
||||||
content: { maxAge: config.staticMaxAge },
|
|
||||||
index: 'index.html',
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
|
|
||||||
app.listen(config.port, config.host, () => {
|
|
||||||
winston.info(`listening on ${config.host}:${config.port}`)
|
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue