Skip to content

Commit

Permalink
fix(hosts-xml-missing): Fix issue when Vispana fails to load hosts.xm…
Browse files Browse the repository at this point in the history
…l, when not available (#48)

Co-authored-by: Broknloop <broknloop@gmail.com>
  • Loading branch information
broknloop and joaomcclain authored Nov 9, 2023
1 parent 196ffe6 commit efb6322
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.vispana.client.vespa.assemblers;

import static com.vispana.client.vespa.helpers.Request.request;
import static com.vispana.client.vespa.helpers.Request.requestWithDefaultValue;

import com.vispana.api.model.apppackage.ApplicationPackage;
import com.vispana.client.vespa.model.ApplicationSchema;
Expand All @@ -9,10 +10,10 @@ public class AppPackageAssembler {

public static ApplicationPackage assemble(String appUrl) {
var appSchema = request(appUrl, ApplicationSchema.class);
var hostContent = request(appUrl + "/content/hosts.xml", String.class);
var hostContent = requestWithDefaultValue(appUrl + "/content/hosts.xml", String.class, "");
var servicesContent = request(appUrl + "/content/services.xml", String.class);

return new ApplicationPackage(
appSchema.getGeneration().toString(), servicesContent, hostContent);
appSchema.getGeneration().toString(), servicesContent, hostContent.orElse(""));
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/vispana/client/vespa/helpers/Request.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
package com.vispana.client.vespa.helpers;

import java.util.Optional;
import org.springframework.web.client.RestClient;

public class Request {

private static final RestClient restClient = RestClient.create();

public static <T> T request(String url, Class<T> responseType) {
return restClient.get().uri(url).retrieve().body(responseType);
}

public static <T> Optional<T> requestWithDefaultValue(
String url, Class<T> responseType, T defaultValue) {
try {
return Optional.of(request(url, responseType));
} catch (Exception exception) {
return Optional.of(defaultValue);
}
}
}
34 changes: 20 additions & 14 deletions src/main/js/routes/apppackage/app-package.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
// necessary import to bundle CSS into a package with postcss
import '../../../resources/static/main.css'
import SyntaxHighlighter from 'react-syntax-highlighter';
import {androidstudio} from 'react-syntax-highlighter/dist/esm/styles/hljs';

// react imports
import React from 'react'
import {useOutletContext} from "react-router-dom";
import TabView from "../../components/tabs/tab-view";
Expand All @@ -28,20 +24,28 @@ function AppPackage() {
})
.sort((a, b) => a.tabName.localeCompare(b.tabName));

const services = {
// initialize tabs with services.xml
const tabsContent = [{
"tabName": "services.xml",
"payload": vespaState.state.applicationPackage.servicesContent,
"contentType": "xml"
};
}]

const hosts = {
"tabName": "hosts.xml",
"payload": vespaState.state.applicationPackage.hostsContent,
"contentType": "xml"
};
// possibly add hosts.xml
let hostsContent = vespaState.state.applicationPackage.hostsContent;
if (hostsContent) {
tabsContent.push({
"tabName": "hosts.xml",
"payload": hostsContent,
"contentType": "xml"
})
}

// add the schemas
tabsContent.push(...schemas)

const tabs = [services, hosts, ...schemas]
// build common tabs
const tabs = tabsContent
.map(tab => {
return {
"header": tab.tabName,
Expand All @@ -53,11 +57,14 @@ function AppPackage() {
}
)

// build 'about'. This is done separately since it builds a different component inside the tab.
tabs.push({
"header": "about",
"content":
<div className="mt-8 mb-3">
<p><span className="text-yellow-400">Generation:</span> {vespaState.state.applicationPackage.appPackageGeneration}</p>
<p><span
className="text-yellow-400">Generation:</span> {vespaState.state.applicationPackage.appPackageGeneration}
</p>
</div>
})

Expand All @@ -70,7 +77,6 @@ function AppPackage() {

</div>
</div>

);
}

Expand Down
4 changes: 0 additions & 4 deletions src/main/js/routes/layout/layout.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
// necessary import to bundle CSS into a package with postcss
import '../../../resources/static/main.css'

// react imports
import React, {useEffect, useState} from 'react'
import {NavLink, Outlet, useFetcher, useLoaderData} from "react-router-dom";
import VispanaApiClient from "../../client/vispana-api-client";
Expand Down

0 comments on commit efb6322

Please sign in to comment.