Skip to content

Commit

Permalink
[Fastify] Add ping endpoint (verdaccio#2246)
Browse files Browse the repository at this point in the history
* chore: add ping endpoint

* Update package.json

* rebase from master

* add debug code and logger

* Update index.ts

* Create heavy-ravens-lay.md

* endpoint as plugin

* chore: format

* Update packages/core/server/debug/index.ts

Co-authored-by: Manuel Spigolon <[email protected]>

* add logger instance to fastify

* 4873 port

* format file

* add logger

Co-authored-by: Manuel Spigolon <[email protected]>
  • Loading branch information
juanpicado and Eomm authored May 22, 2021
1 parent 4ebf188 commit 55ee3fd
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .changeset/heavy-ravens-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@verdaccio/cli': minor
'@verdaccio/fastify-migration': minor
---

[Fastify] Add ping endpoint
6 changes: 5 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"build:types": "tsc --emitDeclarationOnly -p tsconfig.build.json",
"build:js": "babel src/ --out-dir build/ --copy-files --extensions \".ts,.tsx\" --source-maps",
"watch": "pnpm build:js --F --watch",
"build": "pnpm run build:js && pnpm run build:types"
"build": "pnpm run build:js && pnpm run build:types",
"start": "ts-node src/index.ts"
},
"dependencies": {
"@verdaccio/config": "workspace:6.0.0-6-next.6",
Expand All @@ -53,6 +54,9 @@
"kleur": "3.0.3",
"semver": "7.3.2"
},
"devDependencies": {
"ts-node": "9.1.1"
},
"funding": {
"type": "opencollective",
"url": "https://opencollective.com/verdaccio"
Expand Down
20 changes: 17 additions & 3 deletions packages/cli/src/commands/newServer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Command, Option } from 'clipanion';
import { findConfigFile, parseConfigFile } from '@verdaccio/config';
import { setup, logger } from '@verdaccio/logger';
import server from '@verdaccio/fastify-migration';
import { ConfigRuntime } from '@verdaccio/types';

export const DEFAULT_PROCESS_NAME: string = 'verdaccio';

Expand All @@ -19,17 +21,29 @@ export class NewServer extends Command {
description: 'use this configuration file (default: ./config.yaml)',
});

private initLogger(logConfig: ConfigRuntime) {
try {
if (logConfig.logs) {
process.emitWarning('config.logs is deprecated, rename configuration to "config.log"');
}
// FUTURE: remove fallback when is ready
setup(logConfig.log || logConfig.logs);
} catch {
throw new Error('error on init logger');
}
}

public async execute() {
try {
const configPathLocation = findConfigFile(this.config as string);
const configParsed = parseConfigFile(configPathLocation);
const { web } = configParsed;
this.initLogger(configParsed);

process.title = web?.title || DEFAULT_PROCESS_NAME;
// const { version, name } = require('../../package.json');
const ser = await server();
await ser.listen(4000);
console.log('fastify running on port 4000');
const ser = await server({ logger });
await ser.listen(4873);
} catch (err) {
console.error(err);
process.exit(1);
Expand Down
39 changes: 39 additions & 0 deletions packages/core/server/debug/fastify-conf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
storage: ./storage
plugins: ./plugins
web:
title: Verdaccio

auth:
htpasswd:
file: ./htpasswd
uplinks:
npmjs:
url: https://registry.npmjs.org/

packages:
'@*/*':
access: $all
publish: $authenticated
unpublish: $authenticated
proxy: npmjs

'**':
access: $all
publish: $authenticated
unpublish: $authenticated
proxy: npmjs

server:
keepAliveTimeout: 60

middlewares:
audit:
enabled: true

log: { type: stdout, format: pretty, level: http }
flags:
token: false
search: false

i18n:
web: en-US
30 changes: 30 additions & 0 deletions packages/core/server/debug/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import path from 'path';
import buildDebug from 'debug';
import { parseConfigFile } from '@verdaccio/config';
import { setup, logger } from '@verdaccio/logger';
import server from '../src/index';

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

/**
* This file is intended for fast development and debug, it should
* be removed eventually and the app start from @verdaccio/cli package.
*/
(async () => {
try {
const configFile = path.join(__dirname, './fastify-conf.yaml');
debug('configFile %s', configFile);
const configParsed = parseConfigFile(configFile);
setup(configParsed.log);
logger.info(`config location ${configFile}`);
debug('configParsed %s', configParsed);
process.title = 'fastify-verdaccio';
const ser = await server({ logger });
await ser.listen(4873);
logger.info('fastify running on port 4873');
} catch (err) {
// eslint-disable-next-line no-console
console.error(err);
process.exit(1);
}
})();
12 changes: 9 additions & 3 deletions packages/core/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,24 @@
"access": "public"
},
"dependencies": {
"fastify": "3.14.2"
"@verdaccio/logger": "workspace:6.0.0-6-next.4",
"fastify": "3.15.1",
"fastify-plugin": "3.0.0",
"debug": "4.3.1"
},
"devDependencies": {
"@verdaccio/types": "workspace:11.0.0-6-next.7"
"@verdaccio/types": "workspace:11.0.0-6-next.7",
"@verdaccio/config": "workspace:6.0.0-6-next.6",
"ts-node": "9.1.1"
},
"scripts": {
"clean": "rimraf ./build",
"type-check": "tsc --noEmit -p tsconfig.build.json",
"build:types": "tsc --emitDeclarationOnly -p tsconfig.build.json",
"build:js": "babel src/ --out-dir build/ --copy-files --extensions \".ts,.tsx\" --source-maps",
"watch": "pnpm build:js -- --watch",
"build": "pnpm run build:js && pnpm run build:types"
"build": "pnpm run build:js && pnpm run build:types",
"start": "ts-node debug/index.ts"
},
"funding": {
"type": "opencollective",
Expand Down
10 changes: 10 additions & 0 deletions packages/core/server/src/endpoints/ping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { logger } from '@verdaccio/logger';

async function pingRoute(fastify) {
fastify.get('/-/ping', async () => {
logger.http('ping endpoint');
return {};
});
}

export default pingRoute;
12 changes: 7 additions & 5 deletions packages/core/server/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import fastify from 'fastify';
import buildDebug from 'debug';

async function startServer() {
const app = fastify();
import ping from './endpoints/ping';

app.get('/', async (request, reply) => {
return { hello: 'world' };
});
const debug = buildDebug('verdaccio:fastify');

async function startServer({ logger }) {
debug('start server');
const app = fastify({ logger });
app.register(ping);
return app;
}

Expand Down
40 changes: 36 additions & 4 deletions pnpm-lock.yaml

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

0 comments on commit 55ee3fd

Please sign in to comment.