Skip to content

Commit

Permalink
Service offering UI
Browse files Browse the repository at this point in the history
  • Loading branch information
pbochynski committed Nov 13, 2024
1 parent 36ceed9 commit 98ee803
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 34 deletions.
6 changes: 4 additions & 2 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ if (gzipEnabled)
if (process.env.NODE_ENV === 'development') {
app.use(cors({ origin: '*' }));
}
app.get('/proxy', proxyHandler);
app.use('/proxy', proxyHandler);

let server = null;

Expand Down Expand Up @@ -83,13 +83,15 @@ const isDocker = process.env.IS_DOCKER === 'true';
const handleRequest = makeHandleRequest();

if (isDocker) {
console.log('Running in dev mode');
// yup, order matters here
serveMonaco(app);
app.use('/backend', handleRequest);
serveStaticApp(app, '/', '/core-ui');
} else {
console.log('Running in prod mode');
handleTracking(app);
app.use(handleRequest);
app.use('/backend', handleRequest);
}

process.on('SIGINT', function() {
Expand Down
14 changes: 5 additions & 9 deletions backend/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { request as httpRequest } from 'http';
import { URL } from 'url';

async function proxyHandler(req, res) {
console.log('Proxy request received:', req.method, req.query.url);
const targetUrl = req.query.url;
if (!targetUrl) {
return res.status(400).send('Target URL is required as a query parameter');
Expand All @@ -21,10 +22,7 @@ async function proxyHandler(req, res) {
method: req.method,
headers: { ...req.headers, host: parsedUrl.host },
};
if (req.headers['x-authorization']) {
options.headers.authorization = req.headers['x-authorization'];
}

console.log('Proxy request options:', options);
// Create the proxy request
const proxyReq = libRequest(options, proxyRes => {
// Forward status and headers from the target response
Expand All @@ -38,12 +36,10 @@ async function proxyHandler(req, res) {
proxyReq.on('error', error => {
res.status(500).send(`Error making proxy request: ${error.message}`);
});

// Pipe the request data from the client to the target server (for POST, PUT, etc.)
if (req.method !== 'GET' && req.method !== 'HEAD') {
req.pipe(proxyReq);
if (Buffer.isBuffer(req.body)) {
proxyReq.end(req.body); // If the body is already buffered, use it directly.
} else {
proxyReq.end();
req.pipe(proxyReq); // Otherwise, pipe the request for streamed or chunked data.
}
} catch (error) {
res.status(500).send(`Error processing request: ${error.message}`);

Check warning

Code scanning / CodeQL

Exception text reinterpreted as HTML Medium

Exception text
is reinterpreted as HTML without escaping meta-characters.
Expand Down
3 changes: 1 addition & 2 deletions examples/web-component-ext/general.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
resource:
kind: VolumeSnapshot
group: snapshot.storage.k8s.io
kind: Secret
version: v1
urlPath: service-management-ui
category: Kyma
Expand Down
56 changes: 35 additions & 21 deletions examples/web-component-ext/script.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
class MyComponent1 extends HTMLElement {
connectedCallback() {
this.innerHTML = '';
const shadow = this.attachShadow({ mode: 'open' });
let style = styling();
shadow.appendChild(style);

// create elements programmatically
const button = document.createElement('ui5-button');
button.textContent = 'Offerings';
button.textContent = 'Load offerings';
button.design = 'Emphasized';
this.appendChild(button);
shadow.appendChild(button);
const contentDiv = document.createElement('div');
this.appendChild(contentDiv);
shadow.appendChild(contentDiv);
button.addEventListener('click', async () => {
const fetchFn = window.kymaFetchFn;
if (!fetchFn) {
Expand All @@ -18,12 +21,22 @@ class MyComponent1 extends HTMLElement {
let token = await getServiceManagerToken(secret);
let offerings = await getServiceOfferings(token, secret.sm_url);
contentDiv.innerHTML = '';
for (let offering of offerings) {
contentDiv.appendChild(offeringCard(offering));
for (let i = 0; i < offerings.length; i++) {
contentDiv.appendChild(offeringCard(offerings[i], i, offerings.length));
}
});
}
}

function styling() {
const style = document.createElement('style');
style.textContent = `
.content-group {
padding: 1rem;
}
`;
return style;
}
if (!customElements.get('my-component-1')) {
customElements.define('my-component-1', MyComponent1);
}
Expand Down Expand Up @@ -51,7 +64,7 @@ async function getServiceManagerToken(secret) {
const tokenurl = secret.tokenurl + secret.tokenurlsuffix;
const grant_type = 'client_credentials';
//fetch token
let resp = await fetch(tokenurl, {
let resp = await proxyFetch(tokenurl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Expand Down Expand Up @@ -86,27 +99,28 @@ function getProxyAddress() {

function proxyFetch(url, options = {}) {
let proxyUrl = getProxyAddress() + `?url=${url}`;
let newOptions = {
...options,
headers: {
...options.headers,
},
};
if (options.headers && options.headers['authorization']) {
newOptions.headers['x-authorization'] = options.headers['authorization'];
delete newOptions.headers['authorization'];
}
return fetch(proxyUrl, options);
}

function offeringCard(offering) {
function offeringCard(offering, index, total) {
const greyImg =
'data:image/svg+xml;base64,PHN2ZyBpZD0icGxhY2Vob2xkZXIiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDU2IDU2Ij48ZGVmcz48c3R5bGU+LmNscy0xe2ZpbGw6IzVhN2E5NDt9LmNscy0ye2ZpbGw6IzA0OTFkMDt9PC9zdHlsZT48L2RlZnM+PHRpdGxlPnBsYWNlaG9sZGVyPC90aXRsZT48cGF0aCBjbGFzcz0iY2xzLTEiIGQ9Ik00Ni45NTMsMjAuNTg4YTQuMzYzLDQuMzYzLDAsMCwwLTEuODM3LS40NTksMy4yOTEsMy4yOTEsMCwwLDAtMy40LDMuMzc2LDQuMDg0LDQuMDg0LDAsMCwwLC45LDIuNjI1LDMuMDExLDMuMDExLDAsMCwwLDIuNSwxLjEyNiwzLjA4NSwzLjA4NSwwLDAsMCwxLjQ2Mi0uMzc1LDcuNTEyLDcuNTEyLDAsMCwwLDEuMzItLjg5MSwxMC4xMzUsMTAuMTM1LDAsMCwxLDEuMjI2LS44OTEsMi4yNywyLjI3LDAsMCwxLDEuMTc5LS4zNzVBMS41LDEuNSwwLDAsMSw1MiwyNi40MTJWMzkuMDcxYTIuODQzLDIuODQzLDAsMCwxLS41NzYsMiwyLjkyNiwyLjkyNiwwLDAsMS0yLjE1OS42MjZxLTIuOTIzLDAtNC4zODUuMDQ3dC0yLjEyMi4wNDdINDEuOTFhMy4zMjEsMy4zMjEsMCwwLDAs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uOTM4cTAtLjc1Ljg0OC0uNzVoNS42NThxMS4yMjYsMCwxLjctMS41VjM1LjM0MUgyOC4zNWMuNTU3LDAsMS4wNTQuNTE5LDEuNDg5LjhhMTIuMjkxLDEyLjI5MSwwLDAsMSwxLjIyNi44OTFxLjU2NS40NjksMS4xNzkuODlhMy43ODYsMy43ODYsMCwwLDAsMS44MTYuNDIyLDMuMjU2LDMuMjU2LDAsMCwwLDMuMDg3LTEuNDA2LDUuMTE5LDUuMTE5LDAsMCwwLC45OS0zQTQuNzg4LDQuNzg4LDAsMCwwLDM3LjE0NywzMS4wNzRaIi8+PC9zdmc+';
let card = document.createElement('ui5-card');
let header = document.createElement('ui5-card-header');
// header.slot = 'header';
header.setAttribute('title-text', offering.catalog_name);
header.setAttribute('subtitle-text', offering.description);
header.setAttribute('title-text', offering.metadata.displayName);
header.setAttribute('subtitle-text', offering.catalog_name);
header.setAttribute('status', `${index + 1} of ${total}`);
const avatar = document.createElement('ui5-avatar');
avatar.slot = 'avatar';
const img = document.createElement('img');
img.src = offering.metadata?.imageUrl || greyImg;
avatar.appendChild(img);
header.appendChild(avatar);

let cardContent = document.createElement('div');
cardContent.innerHTML = `<br/><p><b>TODO:</b> service plans</p></br>`;
cardContent.setAttribute('class', 'content-group');
cardContent.innerHTML = `${offering.description}`;
card.appendChild(header);
card.appendChild(cardContent);
return card;
Expand Down

0 comments on commit 98ee803

Please sign in to comment.