|
1 | 1 | package com.oembedler.moon.voyager.boot; |
2 | 2 |
|
3 | | -import org.apache.commons.lang3.StringUtils; |
4 | | -import org.apache.commons.lang3.text.StrSubstitutor; |
5 | | -import org.springframework.beans.factory.annotation.Value; |
6 | | -import org.springframework.core.io.ClassPathResource; |
| 3 | +import org.springframework.beans.factory.annotation.Autowired; |
| 4 | +import org.springframework.http.MediaType; |
| 5 | +import org.springframework.http.ResponseEntity; |
7 | 6 | import org.springframework.stereotype.Controller; |
8 | | -import org.springframework.util.StreamUtils; |
9 | 7 | import org.springframework.web.bind.annotation.RequestMapping; |
10 | 8 |
|
11 | 9 | import javax.servlet.http.HttpServletRequest; |
12 | | -import javax.servlet.http.HttpServletResponse; |
13 | 10 | import java.io.IOException; |
14 | | -import java.nio.charset.Charset; |
15 | | -import java.util.HashMap; |
16 | | -import java.util.Map; |
17 | 11 |
|
18 | 12 | /** |
19 | | - * @author Guilherme Blanco |
| 13 | + * @author Max David Günther |
20 | 14 | */ |
21 | 15 | @Controller |
22 | 16 | public class VoyagerController { |
23 | | - |
24 | | - private static final String CDNJS_CLOUDFLARE_COM_AJAX_LIBS = "//cdnjs.cloudflare.com/ajax/libs/"; |
25 | | - private static final String CDN_JSDELIVR_NET_NPM = "//cdn.jsdelivr.net/npm/"; |
26 | | - private static final String VOYAGER = "graphql-voyager"; |
27 | | - private static final String FAVICON_APIS_GURU = "//apis.guru/graphql-voyager/icons/favicon-16x16.png"; |
28 | | - |
29 | | - @Value("${voyager.endpoint:/graphql}") |
30 | | - private String graphqlEndpoint; |
31 | | - |
32 | | - @Value("${voyager.pageTitle:Voyager}") |
33 | | - private String pageTitle; |
34 | | - |
35 | | - @Value("${voyager.static.basePath:/}") |
36 | | - private String staticBasePath; |
37 | | - |
38 | | - @Value("${voyager.cdn.enabled:false}") |
39 | | - private Boolean voyagerCdnEnabled; |
40 | | - |
41 | | - @Value("${voyager.cdn.version:1.0.0-rc.26}") |
42 | | - private String voyagerCdnVersion; |
| 17 | + @Autowired |
| 18 | + private VoyagerIndexHtmlTemplate indexTemplate; |
43 | 19 |
|
44 | 20 | @RequestMapping(value = "${voyager.mapping:/voyager}") |
45 | | - public void voyager(HttpServletRequest request, HttpServletResponse response) throws IOException { |
| 21 | + public ResponseEntity<String> voyager(HttpServletRequest request) throws IOException { |
46 | 22 | String contextPath = request.getContextPath(); |
47 | | - response.setContentType("text/html; charset=UTF-8"); |
48 | | - |
49 | | - String template = StreamUtils.copyToString(new ClassPathResource("voyager.html").getInputStream(), Charset.defaultCharset()); |
50 | | - Map<String, String> replacements = new HashMap<>(); |
51 | | - replacements.put("graphqlEndpoint", contextPath + graphqlEndpoint); |
52 | | - replacements.put("pageTitle", pageTitle); |
53 | | - replacements.put("pageFavicon", getResourceUrl(staticBasePath, "favicon.ico", FAVICON_APIS_GURU)); |
54 | | - replacements.put("es6PromiseJsUrl", getResourceUrl(staticBasePath, "es6-promise.auto.min.js", |
55 | | - joinCdnjsPath("es6-promise", "4.1.1", "es6-promise.auto.min.js"))); |
56 | | - replacements.put("fetchJsUrl", getResourceUrl(staticBasePath, "fetch.min.js", |
57 | | - joinCdnjsPath("fetch", "2.0.4", "fetch.min.js"))); |
58 | | - replacements.put("reactJsUrl", getResourceUrl(staticBasePath, "react.min.js", |
59 | | - joinCdnjsPath("react", "16.8.3", "umd/react.production.min.js"))); |
60 | | - replacements.put("reactDomJsUrl", getResourceUrl(staticBasePath, "react-dom.min.js", |
61 | | - joinCdnjsPath("react-dom", "16.8.3", "umd/react-dom.production.min.js"))); |
62 | | - replacements.put("voyagerCssUrl", getResourceUrl(staticBasePath, "voyager.css", |
63 | | - joinJsDelivrPath(VOYAGER, voyagerCdnVersion, "dist/voyager.css"))); |
64 | | - replacements.put("voyagerJsUrl", getResourceUrl(staticBasePath, "voyager.min.js", |
65 | | - joinJsDelivrPath(VOYAGER, voyagerCdnVersion, "dist/voyager.min.js"))); |
66 | | - replacements.put("voyagerWorkerJsUrl", getResourceUrl(staticBasePath, "voyager.worker.js", |
67 | | - joinJsDelivrPath(VOYAGER, voyagerCdnVersion, "dist/voyager.worker.min.js"))); |
68 | | - replacements.put("contextPath", contextPath); |
69 | | - |
70 | | - response.getOutputStream().write(StrSubstitutor.replace(template, replacements).getBytes(Charset.defaultCharset())); |
71 | | - } |
72 | | - |
73 | | - private String getResourceUrl(String staticBasePath, String staticFileName, String cdnUrl) { |
74 | | - if (voyagerCdnEnabled && StringUtils.isNotBlank(cdnUrl)) { |
75 | | - return cdnUrl; |
76 | | - } |
77 | | - return joinStaticPath(staticBasePath, staticFileName); |
78 | | - } |
79 | | - |
80 | | - private String joinStaticPath(String staticBasePath, String staticFileName) { |
81 | | - return staticBasePath + "vendor/voyager/" + staticFileName; |
82 | | - } |
83 | | - |
84 | | - private String joinCdnjsPath(String library, String cdnVersion, String cdnFileName) { |
85 | | - return CDNJS_CLOUDFLARE_COM_AJAX_LIBS + library + "/" + cdnVersion + "/" + cdnFileName; |
86 | | - } |
87 | | - |
88 | | - private String joinJsDelivrPath(String library, String cdnVersion, String cdnFileName) { |
89 | | - return CDN_JSDELIVR_NET_NPM + library + "@" + cdnVersion + "/" + cdnFileName; |
| 23 | + String indexHtmlContent = indexTemplate.fillIndexTemplate(contextPath); |
| 24 | + return ResponseEntity.ok() |
| 25 | + .contentType(MediaType.valueOf("text/html; charset=UTF-8")) |
| 26 | + .body(indexHtmlContent); |
90 | 27 | } |
91 | 28 | } |
0 commit comments