Skip to content

Commit

Permalink
feat: express plugin, deploy plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
tpluscode committed Oct 4, 2024
1 parent cca64b6 commit fd489b3
Show file tree
Hide file tree
Showing 20 changed files with 369 additions and 76 deletions.
6 changes: 6 additions & 0 deletions .changeset/dirty-mugs-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@kopflos-cms/plugin-deploy-resources": minor
"@kopflos-cms/plugin-express": minor
---

First version
2 changes: 1 addition & 1 deletion .changeset/real-numbers-visit.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
"@kopflos-cms/express": minor
---

The main export now returns and object `{ middleware: RequestHandler; instance: Kopflos }`
The main export now returns `Promise<{ middleware: RequestHandler; instance: Kopflos }>`
7 changes: 7 additions & 0 deletions .changeset/stupid-ads-destroy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@kopflos-cms/express": patch
"@kopflos-cms/core": patch
"kopflos": patch
---

Added plugin with `onStart` hook
6 changes: 6 additions & 0 deletions .changeset/tame-cobras-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@kopflos-cms/plugin-express": minor
"@kopflos-cms/express": minor
---

Added express-only middleware hooks
14 changes: 13 additions & 1 deletion example/kopflos.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as url from 'node:url'
import type { KopflosConfig } from '@kopflos-cms/core'

export default <KopflosConfig> {
deploy: ['resources', 'resources.dev'],
baseIri: 'http://localhost:1429',
apiGraphs: ['http://localhost:1429/api'],
sparql: {
Expand All @@ -10,4 +10,16 @@ export default <KopflosConfig> {
updateUrl: 'http://localhost:7878/update',
},
},
plugins: [
['@kopflos-cms/plugin-deploy-resources', {
paths: ['resources', 'resources.dev'],
}],
['@kopflos-cms/plugin-express', {
before: [
'cors',
['compression', { level: 0 }],
url.fileURLToPath(new URL('.', import.meta.url) + 'lib/static.js'),
],
}],
],
}
5 changes: 5 additions & 0 deletions example/lib/static.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import express from 'express'

export default (): express.RequestHandler => {
return express.static('public')
}
12 changes: 10 additions & 2 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@
"private": true,
"type": "module",
"scripts": {
"start": "kopflos serve"
"start": "kopflos serve --trust-proxy"
},
"dependencies": {
"kopflos": "*"
"express": "^4.21.0",
"kopflos": "*",
"@kopflos-cms/plugin-deploy-resources": "*",
"@kopflos-cms/plugin-express": "*",
"cors": "^2.8.5",
"compression": "^1.7.4"
},
"devDependencies": {
"@types/express": "^4.17.21"
}
}
1 change: 1 addition & 0 deletions example/public/foo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bar
150 changes: 129 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ Options:
-c, --config <config> Path to config file
-p, --port <port> Port to listen on (default: 1429)
-h, --host <host> Host to bind to (default: "0.0.0.0")
--deploy <paths...> Resource paths to be deployed
--auto-deploy Deploy resources from the resources directory (default: true)
--no-auto-deploy Disable auto deployment
--help display help for command
```

Expand Down
36 changes: 10 additions & 26 deletions packages/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import type { CosmiconfigResult } from 'cosmiconfig'
import { cosmiconfig } from 'cosmiconfig'
import kopflos from '@kopflos-cms/express'
import { log } from '@kopflos-cms/core'
import { ResourcePerGraphStore } from '@hydrofoil/resource-store'
import { bootstrap } from '@hydrofoil/talos-core/bootstrap.js'
import { fromDirectories } from '@hydrofoil/talos-core'

const explorer = cosmiconfig('kopflos')

Expand All @@ -19,10 +16,8 @@ program.command('serve')
.option('-c, --config <config>', 'Path to config file')
.option('-p, --port <port>', 'Port to listen on (default: 1429)', parseInt)
.option('-h, --host <host>', 'Host to bind to (default: "0.0.0.0")')
.option('--deploy <paths...>', 'Resource paths to be deployed')
.option('--auto-deploy', 'Deploy resources from the resources directory (default: true)')
.option('--no-auto-deploy', 'Disable auto deployment')
.action(async ({ config, port, host, ...options }) => {
.option('--trust-proxy [proxy]', 'Trust the X-Forwarded-Host header')
.action(async ({ config, port = 1429, host = '0.0.0.0', trustProxy, ...options }) => {
let ccResult: CosmiconfigResult
if (config) {
ccResult = await explorer.load(config)
Expand All @@ -31,33 +26,22 @@ program.command('serve')
}

const finalOptions = {
port: 1429,
host: '0.0.0.0',
autoDeploy: true,
port,
host,
...ccResult?.config || {},
...options,
}

const app = express()

const { instance, middleware } = kopflos(finalOptions)
app.use(middleware)
if (trustProxy) {
app.set('trust proxy', trustProxy)
}

if (finalOptions.autoDeploy) {
if (finalOptions.deploy?.length) {
log.info(`Auto deploy enabled. Deploying from: ${finalOptions.deploy}`)
const { instance, middleware } = await kopflos(finalOptions)
app.use(middleware)

const publicBaseIri = finalOptions.baseIri
await bootstrap({
dataset: await fromDirectories(finalOptions.deploy, publicBaseIri),
store: new ResourcePerGraphStore(instance.env.sparql.default.stream, instance.env),
})
} else {
log.info('No resource paths specified. Skipping deployment')
}
} else {
log.info('Auto deploy disabled. Skipping deployment')
}
await instance.start()

app.listen(port, host, () => {
log.info(`Server running on ${port}. API URL: ${finalOptions.baseIri}`)
Expand Down
2 changes: 0 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
"kopflos": "./bin/kopflos.sh"
},
"dependencies": {
"@hydrofoil/resource-store": "^0.2.2",
"@hydrofoil/talos-core": "^0.2",
"@kopflos-cms/core": "^0.3.0-beta.8",
"@kopflos-cms/express": "^0.0.1-beta.4",
"commander": "^12.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type { KopflosResponse } from './lib/Kopflos.js'
export type { KopflosResponse, KopflosPlugin } from './lib/Kopflos.js'
export type { Kopflos, KopflosConfig, Body, Query } from './lib/Kopflos.js'
export { default } from './lib/Kopflos.js'
export { loadHandler as defaultHandlerLookup } from './lib/handler.js'
Expand Down
Loading

0 comments on commit fd489b3

Please sign in to comment.