|
19 | 19 | * @author Guilherme Blanco |
20 | 20 | */ |
21 | 21 | @Controller |
22 | | -public class VoyagerController |
23 | | -{ |
| 22 | +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 FETCH = "fetch"; |
| 27 | + private static final String ES_6_PROMISE = "es6-promise"; |
| 28 | + private static final String REACT = "react"; |
| 29 | + private static final String REACT_DOM = "react-dom"; |
| 30 | + private static final String VOYAGER = "voyager"; |
| 31 | + |
24 | 32 | @Value("${voyager.endpoint:/graphql}") |
25 | 33 | private String graphqlEndpoint; |
26 | 34 |
|
27 | 35 | @Value("${voyager.pageTitle:Voyager}") |
28 | 36 | private String pageTitle; |
29 | 37 |
|
| 38 | + @Value("${voyager.static.basePath:/}") |
| 39 | + private String staticBasePath; |
| 40 | + |
30 | 41 | @Value("${voyager.cdn.enabled:false}") |
31 | 42 | private Boolean voyagerCdnEnabled; |
32 | 43 |
|
33 | | - @Value("${voyager.cdn.version:v1.x}") |
| 44 | + @Value("${voyager.cdn.version:@1.0.0-rc.26}") |
34 | 45 | private String voyagerCdnVersion; |
35 | 46 |
|
36 | 47 | @RequestMapping(value = "${voyager.mapping:/voyager}") |
37 | | - public void voyager(HttpServletRequest request, HttpServletResponse response) throws IOException |
38 | | - { |
| 48 | + public void voyager(HttpServletRequest request, HttpServletResponse response) throws IOException { |
39 | 49 | String contextPath = request.getContextPath(); |
40 | 50 | response.setContentType("text/html; charset=UTF-8"); |
41 | 51 |
|
42 | | - String voyagerCssUrl = contextPath + "/vendor/voyager.css"; |
43 | | - String voyagerJsUrl = contextPath + "/vendor/voyager.min.js"; |
44 | | - |
45 | | - if (voyagerCdnEnabled && StringUtils.isNotBlank(voyagerCdnVersion)) { |
46 | | - voyagerCssUrl = "//apis.guru/graphql-voyager/releases/" + voyagerCdnVersion + "/voyager.css"; |
47 | | - voyagerJsUrl = "//apis.guru/graphql-voyager/releases/" + voyagerCdnVersion + "/voyager.min.js"; |
48 | | - } |
49 | | - |
50 | 52 | String template = StreamUtils.copyToString(new ClassPathResource("voyager.html").getInputStream(), Charset.defaultCharset()); |
51 | 53 | Map<String, String> replacements = new HashMap<>(); |
52 | 54 | replacements.put("graphqlEndpoint", contextPath + graphqlEndpoint); |
53 | 55 | replacements.put("pageTitle", pageTitle); |
54 | | - replacements.put("voyagerCssUrl", voyagerCssUrl); |
55 | | - replacements.put("voyagerJsUrl", voyagerJsUrl); |
| 56 | + replacements.put("es6PromiseJsUrl", getCdnJsUrl(staticBasePath, voyagerCdnEnabled, ES_6_PROMISE, |
| 57 | + "4.1.1", "es6-promise.auto.min.js")); |
| 58 | + replacements.put("fetchJsUrl", getCdnJsUrl(staticBasePath, voyagerCdnEnabled, FETCH, |
| 59 | + "3.0.0", "fetch.min.js")); |
| 60 | + replacements.put("reactJsUrl", getCdnJsUrl(staticBasePath, voyagerCdnEnabled, REACT, |
| 61 | + "16.8.3", "react.production.min.js")); |
| 62 | + replacements.put("reactDomJsUrl", getCdnJsUrl(staticBasePath, voyagerCdnEnabled, REACT_DOM, |
| 63 | + "16.8.3", "react-dom.production.min.js")); |
| 64 | + |
| 65 | + replacements.put("voyagerCssUrl", getJsDeliverUrl(staticBasePath, voyagerCdnEnabled, VOYAGER, |
| 66 | + voyagerCdnVersion, "dist/voyager.css", "voyager.css")); |
| 67 | + replacements.put("voyagerJsUrl", getJsDeliverUrl(staticBasePath, voyagerCdnEnabled, VOYAGER, |
| 68 | + voyagerCdnVersion, "dist/voyager.min.js", "voyager.worker.min.js")); |
| 69 | + replacements.put("voyagerWorkerJsUrl", getJsDeliverUrl(staticBasePath, voyagerCdnEnabled, VOYAGER, |
| 70 | + voyagerCdnVersion, "dist/voyager.worker.min.js", "voyager.worker.min.js")); |
56 | 71 | replacements.put("contextPath", contextPath); |
57 | 72 |
|
58 | 73 | response.getOutputStream().write(StrSubstitutor.replace(template, replacements).getBytes(Charset.defaultCharset())); |
59 | 74 | } |
| 75 | + |
| 76 | + private String getCdnJsUrl(String staticBasePath, Boolean isCdnEnabled, String library, |
| 77 | + String cdnVersion, String filename) { |
| 78 | + if (isCdnEnabled && StringUtils.isNotBlank(cdnVersion)) { |
| 79 | + return CDNJS_CLOUDFLARE_COM_AJAX_LIBS + library + cdnVersion + "/" + filename; |
| 80 | + } |
| 81 | + return staticBasePath + "vendor/" + filename; |
| 82 | + } |
| 83 | + |
| 84 | + private String getJsDeliverUrl(String staticBasePath, Boolean isCdnEnabled, String library, |
| 85 | + String cdnVersion, String cdnFileName, String filename) { |
| 86 | + if (isCdnEnabled && StringUtils.isNotBlank(cdnVersion)) { |
| 87 | + return CDN_JSDELIVR_NET_NPM + library + "/" + cdnVersion + "/" + cdnFileName; |
| 88 | + } |
| 89 | + return staticBasePath + "vendor/" + filename; |
| 90 | + } |
60 | 91 | } |
0 commit comments