Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.io.IOException;
import java.util.Map;

/**
* @author Max David Günther
Expand All @@ -17,9 +19,9 @@ public class ReactiveVoyagerController {
private VoyagerIndexHtmlTemplate indexTemplate;

@GetMapping(path = "${voyager.mapping:/voyager}")
public ResponseEntity<String> voyager() throws IOException {
public ResponseEntity<String> voyager(@PathVariable Map<String, String> params) throws IOException {
// no context path in spring-webflux
String indexHtmlContent = indexTemplate.fillIndexTemplate("");
String indexHtmlContent = indexTemplate.fillIndexTemplate("", params);
return ResponseEntity.ok()
.contentType(MediaType.valueOf("text/html; charset=UTF-8"))
.body(indexHtmlContent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
* @author Max David Günther
Expand All @@ -18,9 +20,10 @@ public class VoyagerController {
private VoyagerIndexHtmlTemplate indexTemplate;

@RequestMapping(value = "${voyager.mapping:/voyager}")
public ResponseEntity<String> voyager(HttpServletRequest request) throws IOException {
public ResponseEntity<String> voyager(HttpServletRequest request,
@PathVariable Map<String, String> params) throws IOException {
String contextPath = request.getContextPath();
String indexHtmlContent = indexTemplate.fillIndexTemplate(contextPath);
String indexHtmlContent = indexTemplate.fillIndexTemplate(contextPath, params);
return ResponseEntity.ok()
.contentType(MediaType.valueOf("text/html; charset=UTF-8"))
.body(indexHtmlContent);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package graphql.kickstart.voyager.boot;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.text.StrSubstitutor;
import org.apache.commons.text.StringSubstitutor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -36,10 +37,10 @@ public class VoyagerIndexHtmlTemplate {
@Value("${voyager.cdn.version:1.0.0-rc.26}")
private String voyagerCdnVersion;

public String fillIndexTemplate(String contextPath) throws IOException {
public String fillIndexTemplate(String contextPath, Map<String, String> params) throws IOException {
String template = StreamUtils.copyToString(new ClassPathResource("voyager.html").getInputStream(), Charset.defaultCharset());
Map<String, String> replacements = new HashMap<>();
replacements.put("graphqlEndpoint", contextPath + graphqlEndpoint);
replacements.put("graphqlEndpoint", constructGraphQlEndpoint(contextPath, params));
replacements.put("pageTitle", pageTitle);
replacements.put("pageFavicon", getResourceUrl(staticBasePath, "favicon.ico", FAVICON_APIS_GURU));
replacements.put("es6PromiseJsUrl", getResourceUrl(staticBasePath, "es6-promise.auto.min.js",
Expand All @@ -58,7 +59,18 @@ public String fillIndexTemplate(String contextPath) throws IOException {
joinJsDelivrPath(VOYAGER, voyagerCdnVersion, "dist/voyager.worker.min.js")));
replacements.put("contextPath", contextPath);

return StrSubstitutor.replace(template, replacements);
return StringSubstitutor.replace(template, replacements);
}

private String constructGraphQlEndpoint(String contextPath, @RequestParam Map<String, String> params) {
String endpoint = graphqlEndpoint;
for (Map.Entry<String, String> param : params.entrySet()) {
endpoint = endpoint.replaceAll("\\{" + param.getKey() + "}", param.getValue());
}
if (StringUtils.isNotBlank(contextPath) && !endpoint.startsWith(contextPath)) {
return contextPath + endpoint;
}
return endpoint;
}

private String getResourceUrl(String staticBasePath, String staticFileName, String cdnUrl) {
Expand Down