mirror of
https://github.com/seejohnrun/haste-server.git
synced 2024-11-01 03:21:21 +00:00
fix pr comments
This commit is contained in:
parent
30c1c486f6
commit
ab47249505
6 changed files with 127 additions and 122 deletions
|
@ -1,3 +1,2 @@
|
||||||
**/*.min.js
|
**/*.min.js
|
||||||
config
|
config
|
||||||
config.js
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"tabWidth": 2,
|
"tabWidth": 2,
|
||||||
"semi": false,
|
"semi": false,
|
||||||
"trailingComma": "all",
|
"trailingComma": "none",
|
||||||
"printWidth": 80,
|
"printWidth": 80,
|
||||||
"arrowParens": "avoid",
|
"arrowParens": "avoid",
|
||||||
"singleQuote": true
|
"singleQuote": true
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
FROM node:16-slim as base
|
FROM node:16-slim as base
|
||||||
|
|
||||||
ARG user
|
ARG user node
|
||||||
RUN mkdir /app && chown -R $user:$user /app
|
RUN mkdir /app && chown -R $user:$user /app
|
||||||
USER $user
|
USER $user
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
25
README.md
25
README.md
|
@ -31,23 +31,24 @@ STDOUT. Check the README there for more details and usages.
|
||||||
1. Download the package, and expand it
|
1. Download the package, and expand it
|
||||||
3. `yarn install`
|
3. `yarn install`
|
||||||
|
|
||||||
## Development
|
## Running the project
|
||||||
|
|
||||||
1. Explore the settings inside of config.js, but the defaults should be good
|
> Explore the settings inside of config.js, but the defaults should be good
|
||||||
2. `yarn install`
|
|
||||||
3. `yarn dev` (you may specify an optional `<config-path>` as well)
|
|
||||||
|
|
||||||
## Production
|
### Development
|
||||||
|
|
||||||
1. Explore the settings inside of config.js, but the defaults should be good
|
1. `yarn install`
|
||||||
2. `yarn install`
|
2. `yarn dev` (you may specify an optional `<config-path>` as well)
|
||||||
3. `yarn build` to build the package
|
|
||||||
4. `yarn start` to start the server
|
|
||||||
|
|
||||||
## Production with Docker
|
### Production
|
||||||
|
|
||||||
1. Explore the settings inside of config.js, but the defaults should be good
|
1. `yarn install`
|
||||||
2. `docker compose up`
|
2. `yarn build` to build the package
|
||||||
|
3. `yarn start` to start the server
|
||||||
|
|
||||||
|
### Production with Docker
|
||||||
|
|
||||||
|
1. `docker compose up`
|
||||||
|
|
||||||
## Settings
|
## Settings
|
||||||
|
|
||||||
|
|
|
@ -18,4 +18,4 @@ const build = async (config: Config) => {
|
||||||
return documentHandler
|
return documentHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
export default build
|
export default build
|
||||||
|
|
217
src/server.ts
217
src/server.ts
|
@ -20,118 +20,123 @@ if (config.logging) {
|
||||||
addLogging(config)
|
addLogging(config)
|
||||||
}
|
}
|
||||||
|
|
||||||
buildDocumenthandler(config).then((documentHandler: DocumentHandler) => {
|
buildDocumenthandler(config)
|
||||||
// Compress the static javascript assets
|
.then((documentHandler: DocumentHandler) => {
|
||||||
if (config.recompressStaticAssets) {
|
// Compress the static javascript assets
|
||||||
const list = fs.readdirSync(getStaticDirectory(__dirname))
|
if (config.recompressStaticAssets) {
|
||||||
for (let j = 0; j < list.length; j += 1) {
|
const list = fs.readdirSync(getStaticDirectory(__dirname))
|
||||||
const item = list[j]
|
for (let j = 0; j < list.length; j += 1) {
|
||||||
if (
|
const item = list[j]
|
||||||
item.indexOf('.js') === item.length - 3 &&
|
if (
|
||||||
item.indexOf('.min.js') === -1
|
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 dest = `${item.substring(
|
||||||
)}`
|
0,
|
||||||
const origCode = fs.readFileSync(
|
item.length - 3,
|
||||||
getStaticItemDirectory(__dirname, item),
|
)}.min${item.substring(item.length - 3)}`
|
||||||
'utf8',
|
const origCode = fs.readFileSync(
|
||||||
)
|
getStaticItemDirectory(__dirname, item),
|
||||||
|
'utf8',
|
||||||
|
)
|
||||||
|
|
||||||
fs.writeFileSync(
|
fs.writeFileSync(
|
||||||
getStaticItemDirectory(__dirname, dest),
|
getStaticItemDirectory(__dirname, dest),
|
||||||
uglify.minify(origCode).code,
|
uglify.minify(origCode).code,
|
||||||
'utf8',
|
'utf8',
|
||||||
)
|
)
|
||||||
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 })
|
||||||
|
|
||||||
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
|
||||||
|
app.get('/raw/:id', async (request, response) =>
|
||||||
|
documentHandler.handleRawGet(request, response),
|
||||||
|
)
|
||||||
|
|
||||||
|
app.head('/raw/:id', (request, response) =>
|
||||||
|
documentHandler.handleRawGet(request, response),
|
||||||
|
)
|
||||||
|
|
||||||
|
// // add documents
|
||||||
|
app.post('/documents', (request, response) =>
|
||||||
|
documentHandler.handlePost(request, response),
|
||||||
|
)
|
||||||
|
|
||||||
|
// get documents
|
||||||
|
app.get('/documents/:id', (request, response) =>
|
||||||
|
documentHandler.handleGet(request, response),
|
||||||
|
)
|
||||||
|
|
||||||
|
app.head('/documents/:id', (request, response) =>
|
||||||
|
documentHandler.handleGet(request, response),
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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) => {
|
||||||
|
request.sturl = '/'
|
||||||
|
next()
|
||||||
|
})
|
||||||
|
|
||||||
|
// And match index
|
||||||
|
app.use(
|
||||||
|
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}`)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
.catch(e => {
|
||||||
const app: Express = express()
|
winston.error(`server couldn't start, an error occured on ${e.message}`)
|
||||||
|
|
||||||
// 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) =>
|
|
||||||
documentHandler.handleRawGet(request, response),
|
|
||||||
)
|
|
||||||
|
|
||||||
app.head('/raw/:id', (request, response) =>
|
|
||||||
documentHandler.handleRawGet(request, response),
|
|
||||||
)
|
|
||||||
|
|
||||||
// // add documents
|
|
||||||
app.post('/documents', (request, response) =>
|
|
||||||
documentHandler.handlePost(request, response),
|
|
||||||
)
|
|
||||||
|
|
||||||
// get documents
|
|
||||||
app.get('/documents/:id', (request, response) =>
|
|
||||||
documentHandler.handleGet(request, response),
|
|
||||||
)
|
|
||||||
|
|
||||||
app.head('/documents/:id', (request, response) =>
|
|
||||||
documentHandler.handleGet(request, response),
|
|
||||||
)
|
|
||||||
|
|
||||||
// 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) => {
|
|
||||||
request.sturl = '/'
|
|
||||||
next()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
// And match index
|
|
||||||
app.use(
|
|
||||||
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