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

Replace built-in sinks with modules #576

Merged
merged 6 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
build:
strategy:
matrix:
os: [ubuntu-latest, macOS-latest, windows-latest]
os: [ubuntu-latest, macOS-latest]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disable for Windows awaiting eik-lib/common#303

node-version: [18, 20]
runs-on: ${{ matrix.os }}

Expand Down
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
package-lock=false
save-exact=true
70 changes: 69 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,70 @@
# service
# Eik HTTP Service

Eik REST API - As a standalone running HTTP service

wkillerud marked this conversation as resolved.
Show resolved Hide resolved
## Installation

```
npm install @eik/service
```

## Usage

This server can either be run as a Node executable, or as a Fastify plugin.

```sh
npx @eik/service
```

For a production setup, the Fastify plugin method is recommended.
wkillerud marked this conversation as resolved.
Show resolved Hide resolved

```js
import fastify from 'fastify';
import Service from '@eik/service';
import Sink from '@eik/sink-gcs';

// Set up the Google Cloud Storage sink
// https://github.com/eik-lib/sink-gcs?tab=readme-ov-file#example
const sink = new Sink({
credentials: {
client_email: '[email protected]',
private_key: '[ ...snip... ]',
projectId: 'myProject',
},
});
wkillerud marked this conversation as resolved.
Show resolved Hide resolved

// Set up the Eik service as a plugin
const service = new Service({ sink });

// Set up Fastify
const app = fastify({
ignoreTrailingSlash: true,
modifyCoreObjects: false,
trustProxy: true,
});

// Register the Eik service in Fastify
app.register(service.api());

// Append custom HTTP ready checks
app.get('/_/health', (request, reply) => {
reply.send('ok');
});

app.get('/_/ready', (request, reply) => {
reply.send('ok');
});
wkillerud marked this conversation as resolved.
Show resolved Hide resolved

// Start the server
const run = async () => {
await service.health();
await app.listen(
service.config.get('http.port'),
service.config.get('http.address'),
);
};

run();
```

The example above shows the Google Cloud Storage sink. You can also use the [file system sink](https://github.com/eik-lib/sink-file-system), or implement the [sink interface](https://github.com/eik-lib/sink) for your own custom storage backend.
28 changes: 22 additions & 6 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,41 @@ import eik from '@eik/core';
import config from './config.js';
import * as utils from './utils.js';

/**
* @typedef {object} EikServiceOptions
* @property {import('@eik/sink').default} [sink]
* @property {import('@eik/sink').default} [customSink] [Deprecated] Use sink instead
* @property {string} [aliasCacheControl]
* @property {string} [notFoundCacheControl="public, max-age=5"]
*/

const EikService = class EikService {
constructor({
customSink,
notFoundCacheControl,
aliasCacheControl,
} = {}) {
/**
* @param {EikServiceOptions} [options={}]
*/
constructor(options = {}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we decide to keep the "bin-option", then I assume this would cause issues for what is happening here: https://github.com/eik-lib/service/blob/deprecate-built-in-sinks/bin/eik-server.js#L6

But I guess that is perhaps for the actual deprecation pr 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's optional now, so you can new up an instance without passing any opts 👍

let { sink } = options;
const {
customSink,
notFoundCacheControl,
aliasCacheControl,
} = options;
this._notFoundCacheControl = notFoundCacheControl || 'public, max-age=5';
const logger = pino({
// @ts-ignore
level: config.get('log.level'),
name: config.get('name'),
});

let sink;
if (customSink) {
logger.warn('The `customSink` option is deprecated and will be removed at a later stage. Use `sink` to remove this warning.');
sink = customSink;
} else if (config.get('sink.type') === 'mem') {
logger.warn('Using the built-in sinks is deprecated and will be removed at a later stage. Install and use `@eik/sink-memory` to remove this warning.');
logger.info(`Server is running with a in memory sink. Uploaded files will be lost on restart!`);
sink = new eik.sink.MEM();
} else {
logger.warn(`Using the built-in sinks is deprecated and will be removed at a later stage. Install and use \`@eik/sink-file-system\` to remove this warning.`);
logger.info(`Server is running with the file system sink. Uploaded files will be stored under "${config.get('sink.path')}"`);
sink = new eik.sink.FS({ sinkFsRootPath: config.get('sink.path') });
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
},
"homepage": "https://github.com/eik-lib/service#readme",
"dependencies": {
"@eik/core": "1.3.47",
"@eik/core": "1.3.48",
"@eik/sink": "1.2.5",
"@fastify/compress": "6.5.0",
"@fastify/cors": "8.5.0",
"@fastify/jwt": "7.2.4",
Expand Down