Skip to content

Commit

Permalink
Create backend plugin test-plugin-backend-2
Browse files Browse the repository at this point in the history
  • Loading branch information
debsmita1 committed Jul 23, 2024
1 parent 1f6b6c3 commit b76fa48
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 0 deletions.
1 change: 1 addition & 0 deletions plugins/test-plugin-backend-2-backend/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@backstage/cli/config/eslint-factory')(__dirname);
21 changes: 21 additions & 0 deletions plugins/test-plugin-backend-2-backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# test-plugin-backend-2

Welcome to the test-plugin-backend-2 backend plugin!

_This plugin was created through the Backstage CLI_

## Getting started

Your plugin has been added to the example app in this repository, meaning you'll be able to access it by running `yarn
start` in the root directory, wait the server to start and then acessing the plugin endpoints:

* [/test-plugin-backend-2/health](http://localhost:7007/api/test-plugin-backend-2/health): This endpoint will simply return a JSON with content `{"status" : "ok"}` and requires no authentication;
* [/test-plugin-backend-2/hello](http://localhost:7007/api/test-plugin-backend-2/hello): This endpoint will thrown a 401 error if not authenticated and can be called setting the `Authorization` header with the proper token. Once authenticated it will return a JSON with content `{"message": "Hello User"}`, where User is the authenticated user. Here's an example to call it using the `curl` utility:

```
curl -vH "Authorization: Bearer ${RHDH_TOKEN}" http://localhost:7007/api/test-plugin-backend-2/hello
```

You can also serve the plugin in isolation by running `yarn start` in the plugin directory.
This method of serving the plugin provides quicker iteration speed and a faster startup and hot reloads.
It is only meant for local development, and the setup for it can be found inside the [/dev](/dev) directory.
9 changes: 9 additions & 0 deletions plugins/test-plugin-backend-2-backend/dev/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createBackend } from '@backstage/backend-defaults';

const backend = createBackend();

backend.add(import('@backstage/plugin-auth-backend'));
backend.add(import('@backstage/plugin-auth-backend-module-guest-provider'));
backend.add(import('../src'));

backend.start();
51 changes: 51 additions & 0 deletions plugins/test-plugin-backend-2-backend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"name": "@internal/backstage-plugin-test-plugin-backend-2-backend",
"version": "0.1.1",
"main": "src/index.ts",
"types": "src/index.ts",
"license": "Apache-2.0",
"private": true,
"publishConfig": {
"access": "public",
"main": "dist/index.cjs.js",
"types": "dist/index.d.ts"
},
"backstage": {
"role": "backend-plugin"
},
"scripts": {
"start": "backstage-cli package start",
"build": "backstage-cli package build",
"lint": "backstage-cli package lint",
"test": "backstage-cli package test",
"clean": "backstage-cli package clean",
"prepack": "backstage-cli package prepack",
"postpack": "backstage-cli package postpack",
"export-dynamic": "janus-cli package export-dynamic-plugin --clean"
},
"dependencies": {
"@backstage/backend-common": "^0.21.7",
"@backstage/backend-defaults": "^0.3.3",
"@backstage/backend-plugin-api": "0.6.18",
"@backstage/config": "^1.2.0",
"@types/express": "^4.17.6",
"express": "^4.17.1",
"express-promise-router": "^4.1.0",
"winston": "^3.2.1",
"node-fetch": "^2.6.7",
"yn": "^4.0.0"
},
"devDependencies": {
"@backstage/backend-test-utils": "^0.4.3",
"@backstage/cli": "^0.26.10",
"@backstage/plugin-auth-backend": "^0.22.8",
"@backstage/plugin-auth-backend-module-guest-provider": "^0.1.7",
"@janus-idp/cli": "^1.11.1",
"@types/supertest": "^2.0.12",
"supertest": "^6.2.4",
"msw": "^1.0.0"
},
"files": [
"dist"
]
}
2 changes: 2 additions & 0 deletions plugins/test-plugin-backend-2-backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './service/router';
export { plugin as default } from './plugin';
40 changes: 40 additions & 0 deletions plugins/test-plugin-backend-2-backend/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
coreServices,
createBackendPlugin,
} from '@backstage/backend-plugin-api';
import { createRouter } from './service/router';

/**
* test-plugin-backend-2Plugin backend plugin
*
* @public
*/
export const plugin = createBackendPlugin({
pluginId: 'test-plugin-backend-2',
register(env) {
env.registerInit({
deps: {
httpRouter: coreServices.httpRouter,
logger: coreServices.logger,
config: coreServices.rootConfig,
httpAuth: coreServices.httpAuth,
discovery: coreServices.discovery,
},
async init({ httpRouter, logger, config, httpAuth, discovery }) {
logger.info('test-plugin-backend-2 plugin :: init');
httpRouter.use(
await createRouter({
logger,
config,
httpAuth,
discovery
}),
);
httpRouter.addAuthPolicy({
path: '/health',
allow: 'unauthenticated',
});
},
});
},
});
41 changes: 41 additions & 0 deletions plugins/test-plugin-backend-2-backend/src/service/router.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { mockServices } from '@backstage/backend-test-utils';
import express from 'express';
import request from 'supertest';

import { createRouter } from './router';

describe('createRouter', () => {
let app: express.Express;

beforeAll(async () => {
const router = await createRouter({
logger: mockServices.logger.mock(),
config: mockServices.rootConfig(),
discovery: mockServices.discovery(),
httpAuth: mockServices.httpAuth(),
});
app = express().use(router);
});

beforeEach(() => {
jest.resetAllMocks();
});

describe('GET /health', () => {
it('returns ok', async () => {
const response = await request(app).get('/health');

expect(response.status).toEqual(200);
expect(response.body).toEqual({ status: 'ok' });
});
});

describe('GET /hello', () => {
it('returns 200', async () => {
const response = await request(app).get('/hello');

expect(response.status).toEqual(200);
expect(response.body).toEqual({ message: 'Hello user:default/mock' });
});
});
});
39 changes: 39 additions & 0 deletions plugins/test-plugin-backend-2-backend/src/service/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// TODO: We should use the MiddlewareFactory from @backstage/backend-defaults/rootHttpRouter, but it is not available in RHDH 1.2.x
import { MiddlewareFactory } from '@backstage/backend-app-api';
import { createLegacyAuthAdapters } from '@backstage/backend-common';
import {
DiscoveryService,
HttpAuthService,
LoggerService,
} from '@backstage/backend-plugin-api';
import { Config } from '@backstage/config';
import express from 'express';
import Router from 'express-promise-router';

export interface RouterOptions {
logger: LoggerService;
config: Config;
discovery: DiscoveryService;
httpAuth?: HttpAuthService;
}

export async function createRouter(
options: RouterOptions,
): Promise<express.Router> {
const { logger, config } = options;
const { httpAuth } = createLegacyAuthAdapters(options);

const router = Router();
router.use(express.json());
router.get('/health', async (_, res) => res.json({ status: 'ok' }));

router.get('/hello', async (req, res) => {
const caller = await httpAuth.credentials(req, { allow: ['user'] });
res.json({ message: `Hello ${caller.principal.userEntityRef}` });
});

const middleware = MiddlewareFactory.create({ logger, config });

router.use(middleware.error());
return router;
}
1 change: 1 addition & 0 deletions plugins/test-plugin-backend-2-backend/src/setupTests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};

0 comments on commit b76fa48

Please sign in to comment.