Skip to content

Commit

Permalink
connect storage with fastify server as decorator (verdaccio#2299)
Browse files Browse the repository at this point in the history
* connect storage with fastify server as decorator

* remove dep
  • Loading branch information
juanpicado authored Jun 13, 2021
1 parent 8ca5fe6 commit 09a7cf3
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/cli/src/commands/newServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class NewServer extends Command {

process.title = web?.title || DEFAULT_PROCESS_NAME;
// const { version, name } = require('../../package.json');
const ser = await server({ logger });
const ser = await server({ logger, config: configParsed });
await ser.listen(4873);
} catch (err) {
console.error(err);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/server/debug/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const debug = buildDebug('verdaccio:fastify:debug');
logger.info(`config location ${configFile}`);
debug('configParsed %s', configParsed);
process.title = 'fastify-verdaccio';
const ser = await server({ logger });
const ser = await server({ logger, config: configParsed });
await ser.listen(4873);
logger.info('fastify running on port 4873');
} catch (err) {
Expand Down
8 changes: 5 additions & 3 deletions packages/core/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@
"access": "public"
},
"dependencies": {
"@verdaccio/config": "workspace:6.0.0-6-next.7",
"@verdaccio/auth": "workspace:6.0.0-6-next.9",
"@verdaccio/logger": "workspace:6.0.0-6-next.4",
"@verdaccio/store": "workspace:6.0.0-6-next.10",
"debug": "4.3.1",
"fastify": "3.15.1",
"fastify-plugin": "3.0.0",
"debug": "4.3.1"
"fastify-plugin": "3.0.0"
},
"devDependencies": {
"@verdaccio/types": "workspace:11.0.0-6-next.7",
"@verdaccio/config": "workspace:6.0.0-6-next.7",
"ts-node": "9.1.1"
},
"scripts": {
Expand Down
5 changes: 5 additions & 0 deletions packages/core/server/src/endpoints/ping.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
/* eslint-disable no-console */
/* eslint-disable no-invalid-this */
import { logger } from '@verdaccio/logger';

async function pingRoute(fastify) {
fastify.get('/-/ping', async () => {
logger.http('ping endpoint');
// @ts-ignore
console.log('-storage->', fastify.storage);
console.log('-config->', fastify.config);
return {};
});
}
Expand Down
9 changes: 9 additions & 0 deletions packages/core/server/src/plugins/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Storage, IStorageHandler } from '@verdaccio/store';

export async function storageService(fastify, opts, done) {
const { config, filters } = opts;
const storage: IStorageHandler = new Storage(config);
await storage.init(config, filters ?? {});
fastify.decorate('storage', storage);
done();
}
26 changes: 24 additions & 2 deletions packages/core/server/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
import { Config as IConfig } from '@verdaccio/types';
import { Config as AppConfig } from '@verdaccio/config';

import fastify from 'fastify';
import buildDebug from 'debug';
import fp from 'fastify-plugin';

import ping from './endpoints/ping';
import { storageService } from './plugins/storage';

const debug = buildDebug('verdaccio:fastify');

async function startServer({ logger }) {
async function startServer({ logger, config }) {
const configInstance: IConfig = new AppConfig(Object.assign({}, config));
debug('start server');
const app = fastify({ logger });
app.register(ping);

app.decorate('config', configInstance);
app.register(fp(storageService), { config: configInstance });

// api
app.register((instance, opts, done) => {
instance.decorate('utility', new Map());
instance.register(ping);
done();
});

// web
app.register((instance, opts, done) => {
instance.register(ping, { prefix: '/web' });
done();
});

return app;
}

Expand Down
16 changes: 15 additions & 1 deletion packages/core/server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,19 @@
"outDir": "./build"
},
"include": ["src/**/*"],
"exclude": ["src/**/*.test.ts"]
"exclude": ["src/**/*.test.ts"],
"references": [
{
"path": "../store"
},
{
"path": "../config"
},
{
"path": "../auth"
},
{
"path": "../logger"
}
]
}
5 changes: 2 additions & 3 deletions packages/server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import cors from 'cors';
import RateLimit from 'express-rate-limit';
import { HttpError } from 'http-errors';

import { Storage } from '@verdaccio/store';
import { loadPlugin } from '@verdaccio/loaders';
import { Auth } from '@verdaccio/auth';
import apiEndpoint from '@verdaccio/api';
Expand All @@ -17,7 +16,7 @@ import webMiddleware from '@verdaccio/web';
import { ConfigRuntime } from '@verdaccio/types';

import { IAuth, IBasicAuth } from '@verdaccio/auth';
import { IStorageHandler } from '@verdaccio/store';
import { Storage, IStorageHandler } from '@verdaccio/store';
import { logger } from '@verdaccio/logger';
import { log, final, errorReportingMiddleware } from '@verdaccio/middleware';
import AuditMiddleware from 'verdaccio-audit';
Expand Down Expand Up @@ -156,8 +155,8 @@ export default (async function (configHash: ConfigRuntime): Promise<any> {
(plugin: IPluginStorageFilter<IConfig>) => plugin.filter_metadata
);
const storage: IStorageHandler = new Storage(config);
// waits until init calls have been initialized
try {
// waits until init calls have been initialized
await storage.init(config, filters);
} catch (err) {
logger.error({ error: err.msg }, 'storage has failed: @{error}');
Expand Down
6 changes: 5 additions & 1 deletion pnpm-lock.yaml

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

0 comments on commit 09a7cf3

Please sign in to comment.