Skip to content

Commit e487c93

Browse files
committed
Added changes for issue 33
1 parent 8ce3ad5 commit e487c93

5 files changed

Lines changed: 110 additions & 1 deletion

File tree

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,27 @@ Stores important bits of knowledge in structured JSON-LD objects:
2626
1. **Open and Free**—expose all contributions immediately without charge to write or read;
2727
1. **Attributed and Versioned**—always include asserted ownership and transaction metadata so consumers can evaluate trustworthiness and relevance.
2828

29+
### Programmatic usage
30+
This project exposes a single public entry point at the package root (`index.js`). Only a few
31+
functions are exported – everything else lives in internal modules and is intentionally
32+
kept private. Example:
33+
34+
```js
35+
import { app, createServer, start } from 'rerum_server'
36+
37+
// `app` is the configured Express application; you can pass it to Supertest or reuse it
38+
// inside another HTTP stack.
39+
40+
const server = createServer(8080) // returns a http.Server but does not listen
41+
server.listen()
42+
43+
// or simply
44+
start(8080) // convenience helper that both creates and listens
45+
```
46+
47+
Consumers no longer need to reach into `./app.js` or other deep paths – if it isn't
48+
exported here it isn't part of the stable API.
49+
2950
## What we add
3051
You will find a `__rerum` property on anything you read from this repository. This is written onto
3152
all objects by the server and is not editable by the client applications. While applications may assert

__tests__/public_api.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { app, createServer, start } from '../index.js'
2+
3+
describe('public API entry point', () => {
4+
test('exports an express app instance', () => {
5+
expect(app).toBeDefined()
6+
expect(typeof app.use).toBe('function') // express app
7+
})
8+
9+
test('createServer returns a http.Server', () => {
10+
const server = createServer(0) // port 0 for ephemeral
11+
expect(server).toBeDefined()
12+
expect(typeof server.listen).toBe('function')
13+
server.close()
14+
})
15+
16+
test('start starts the server and returns it', (done) => {
17+
const server = start(0)
18+
server.on('listening', () => {
19+
server.close(() => done())
20+
})
21+
})
22+
})

__tests__/routes_mounted.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
import request from "supertest"
99
import api_routes from "../routes/api-routes.js"
10-
import app from "../app.js"
10+
// leverage the public entry point instead of a deep path
11+
import app from "../index.js"
1112
import fs from "fs"
1213

1314
let app_stack = app.router.stack

index.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Public entry point for the RERUM API v1 server library
2+
// Only the things exported here are considered a supported, stable
3+
// API. Internal helpers and modules (controllers, database, routes,
4+
// etc.) remain private and are not re-exported. Consumers of the
5+
// package should be able to import from the package root rather than
6+
// reach into deep paths.
7+
8+
import http from 'http'
9+
import app from './app.js'
10+
11+
/**
12+
* Express application instance used throughout the project. Exported
13+
* primarily for testing or embedding inside another server.
14+
*
15+
* ```js
16+
* import { app } from 'rerum_server'
17+
* ```
18+
*/
19+
export { app }
20+
21+
/**
22+
* Default export is the express app largely for backwards compatibility
23+
* with consumers that do `import app from 'rerum_server'`.
24+
*/
25+
export default app
26+
27+
/**
28+
* Helper that creates an HTTP server for the configured express app.
29+
* The returned server is **not** listening yet; caller may attach
30+
* additional listeners or configure timeouts before calling
31+
* `server.listen(...)`.
32+
*
33+
* @param {number|string} [port=process.env.PORT||3001] port to assign to
34+
* the express app and eventually listen on
35+
* @returns {import('http').Server} http server instance
36+
*/
37+
export function createServer(port = process.env.PORT ?? 3001) {
38+
app.set('port', port)
39+
const server = http.createServer(app)
40+
41+
// mirror the configuration from bin/rerum_v1.js so that programmatic
42+
// users get the same keep-alive behaviour as the CLI entry point.
43+
server.keepAliveTimeout = 8 * 1000
44+
server.headersTimeout = 8.5 * 1000
45+
46+
return server
47+
}
48+
49+
/**
50+
* Convenience function to start the server immediately. Returns the
51+
* server instance so callers can close it in tests or hook events.
52+
*
53+
* @param {number|string} [port] optional port override
54+
* @returns {import('http').Server}
55+
*/
56+
export function start(port) {
57+
const p = port ?? process.env.PORT ?? 3001
58+
const server = createServer(p)
59+
server.listen(p)
60+
server.on('listening', () => {
61+
console.log('LISTENING ON ' + p)
62+
})
63+
return server
64+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"node": ">=24.12.0",
2424
"npm": ">=11.7.0"
2525
},
26+
"main": "index.js",
2627
"scripts": {
2728
"start": "node ./bin/rerum_v1.js",
2829
"test": "jest",

0 commit comments

Comments
 (0)