Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(usage): Use express server #20

Merged
merged 6 commits into from
Mar 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 40 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,11 @@ Implementation of in-memory [IPFS Pinning Service API](https://ipfs.github.io/pi
## Install

```
npm i mock-ipfs-pinning-service
npm i -D mock-ipfs-pinning-service @types/express
```

## Usage

```js
const http = require("http")
const { setup } = require("mock-ipfs-pinning-service")
const port = 3000

const main = async () => {
const service = await setup({ token: "secret" })
const server = http.createServer(http)
server.listen(port)
}
```

State of the pins is exposed via `service.locals.state` which you can monkeypatch or replace. Each new request will be served based on that value. All requests perform that perform state updates do them in immutable style and swap the whole value, in other words references are guaranteed to not mutate.

You can see the status of the server and test individual functions from the SwaggerUI by navigating to `http://localhost:${port}/docs/`, switching _Server_ to `/` and then entering access token under _Authorize_ button.

### CLI Usage

Expand All @@ -36,6 +21,45 @@ npx mock-ipfs-pinning-service --port 3000 --token secret

If token is not passed it will not preform authentification.

### Code Usage

```js
const { setup } = require('mock-ipfs-pinning-service')
const port = 3005

const main = async () => {
/**
* @type {import('express').Application}
*/
const service = await setup({ token: 'secret' })
const server = service.listen(port, () => {
console.log(`server running on port ${port}`)
})

const cleanupEvents = ['beforeExit', 'SIGTERM', 'SIGINT', 'SIGHUP']

// Express server cleanup handling.
const cleanup = () => {
// To prevent duplicated cleanup, remove the process listeners on cleanup
cleanupEvents.forEach((event) => process.off(event, cleanup))
server.close((err) => {
if (err) {
console.error(err)
return
}
console.log(`server stopped listening on port ${port}`)
})
}
// close the server when your process exits
cleanupEvents.forEach((event) => process.on(event, cleanup))
}

```

State of the pins is exposed via `service.locals.state` which you can monkeypatch or replace. Each new request will be served based on that value. All requests perform that perform state updates do them in immutable style and swap the whole value, in other words references are guaranteed to not mutate.

You can see the status of the server and test individual functions from the SwaggerUI by navigating to `http://localhost:${port}/docs/`, switching _Server_ to `/` and then entering access token under _Authorize_ button.

### Log levels

Pass:
Expand Down