4
4
import java .nio .charset .Charset ;
5
5
import java .util .HashMap ;
6
6
import java .util .Map ;
7
+ import lombok .RequiredArgsConstructor ;
7
8
import org .apache .commons .lang3 .StringUtils ;
8
9
import org .apache .commons .text .StringSubstitutor ;
9
10
import org .springframework .beans .factory .annotation .Value ;
11
+ import org .springframework .boot .context .properties .EnableConfigurationProperties ;
10
12
import org .springframework .core .io .ClassPathResource ;
11
13
import org .springframework .util .StreamUtils ;
12
14
import org .springframework .web .bind .annotation .RequestParam ;
13
15
14
16
/**
15
17
* @author Guilherme Blanco
16
18
*/
19
+ @ RequiredArgsConstructor
17
20
public class VoyagerIndexHtmlTemplate {
18
21
19
22
private static final String CDNJS_CLOUDFLARE_COM_AJAX_LIBS = "//cdnjs.cloudflare.com/ajax/libs/" ;
20
23
private static final String CDN_JSDELIVR_NET_NPM = "//cdn.jsdelivr.net/npm/" ;
21
24
private static final String VOYAGER = "graphql-voyager" ;
22
25
private static final String FAVICON_APIS_GURU = "//apis.guru/graphql-voyager/icons/favicon-16x16.png" ;
23
26
24
- @ Value ("${voyager.endpoint:/graphql}" )
25
- private String graphqlEndpoint ;
26
-
27
- @ Value ("${voyager.pageTitle:Voyager}" )
28
- private String pageTitle ;
29
-
30
- @ Value ("${voyager.basePath:/}" )
31
- private String basePath ;
32
-
33
- @ Value ("${voyager.cdn.enabled:false}" )
34
- private boolean voyagerCdnEnabled ;
35
-
36
- @ Value ("${voyager.cdn.version:1.0.0-rc.31}" )
37
- private String voyagerCdnVersion ;
38
-
39
- @ Value ("${voyager.displayOptions.skipRelay:true}" )
40
- private boolean voyagerDisplayOptionsSkipRelay ;
41
-
42
- @ Value ("${voyager.displayOptions.skipDeprecated:true}" )
43
- private boolean voyagerDisplayOptionsSkipDeprecated ;
44
-
45
- @ Value ("${voyager.displayOptions.rootType:Query}" )
46
- private String voyagerDisplayOptionsRootType ;
47
-
48
- @ Value ("${voyager.displayOptions.sortByAlphabet:false}" )
49
- private boolean voyagerDisplayOptionsSortByAlphabet ;
50
-
51
- @ Value ("${voyager.displayOptions.showLeafFields:true}" )
52
- private boolean voyagerDisplayOptionsShowLeafFields ;
53
-
54
- @ Value ("${voyager.displayOptions.hideRoot:false}" )
55
- private boolean voyagerDisplayOptionsHideRoot ;
56
-
57
- @ Value ("${voyager.hideDocs:false}" )
58
- private boolean voyagerHideDocs ;
59
-
60
- @ Value ("${voyager.hideSettings:false}" )
61
- private boolean voyagerHideSettings ;
27
+ private final VoyagerPropertiesConfiguration voyagerConfiguration ;
62
28
63
29
public String fillIndexTemplate (String contextPath , Map <String , String > params )
64
30
throws IOException {
65
31
String template = StreamUtils
66
32
.copyToString (new ClassPathResource ("voyager.html" ).getInputStream (),
67
33
Charset .defaultCharset ());
34
+
35
+ String basePath = voyagerConfiguration .getBasePath ();
36
+ String voyagerCdnVersion = voyagerConfiguration .getCdn ().getVersion ();
37
+
68
38
Map <String , String > replacements = new HashMap <>();
69
39
replacements .put ("graphqlEndpoint" , constructGraphQlEndpoint (contextPath , params ));
70
- replacements .put ("pageTitle" , pageTitle );
40
+ replacements .put ("pageTitle" , voyagerConfiguration . getPageTitle () );
71
41
replacements
72
42
.put ("pageFavicon" , getResourceUrl (basePath , "favicon.ico" , FAVICON_APIS_GURU ));
73
43
replacements .put ("es6PromiseJsUrl" , getResourceUrl (basePath , "es6-promise.auto.min.js" ,
@@ -85,14 +55,15 @@ public String fillIndexTemplate(String contextPath, Map<String, String> params)
85
55
replacements .put ("voyagerWorkerJsUrl" , getResourceUrl (basePath , "voyager.worker.js" ,
86
56
joinJsDelivrPath (voyagerCdnVersion , "dist/voyager.worker.min.js" )));
87
57
replacements .put ("contextPath" , contextPath );
88
- replacements .put ("voyagerDisplayOptionsSkipRelay" , Boolean .toString (voyagerDisplayOptionsSkipRelay ));
89
- replacements .put ("voyagerDisplayOptionsSkipDeprecated" , Boolean .toString (voyagerDisplayOptionsSkipDeprecated ));
90
- replacements .put ("voyagerDisplayOptionsRootType" , voyagerDisplayOptionsRootType );
91
- replacements .put ("voyagerDisplayOptionsSortByAlphabet" , Boolean .toString (voyagerDisplayOptionsSortByAlphabet ));
92
- replacements .put ("voyagerDisplayOptionsShowLeafFields" , Boolean .toString (voyagerDisplayOptionsShowLeafFields ));
93
- replacements .put ("voyagerDisplayOptionsHideRoot" , Boolean .toString (voyagerDisplayOptionsHideRoot ));
94
- replacements .put ("voyagerHideDocs" , Boolean .toString (voyagerHideDocs ));
95
- replacements .put ("voyagerHideSettings" , Boolean .toString (voyagerHideSettings ));
58
+ replacements .put ("voyagerDisplayOptionsSkipRelay" , Boolean .toString (voyagerConfiguration .getDisplayOptions ().isSkipRelay ()));
59
+ replacements .put ("voyagerDisplayOptionsSkipDeprecated" , Boolean .toString (voyagerConfiguration .getDisplayOptions ().isSkipDeprecated ()));
60
+ replacements .put ("voyagerDisplayOptionsRootType" , voyagerConfiguration .getDisplayOptions ().getRootType ());
61
+ replacements .put ("voyagerDisplayOptionsSortByAlphabet" , Boolean .toString (
62
+ voyagerConfiguration .getDisplayOptions ().isSortByAlphabet ()));
63
+ replacements .put ("voyagerDisplayOptionsShowLeafFields" , Boolean .toString (voyagerConfiguration .getDisplayOptions ().isShowLeafFields ()));
64
+ replacements .put ("voyagerDisplayOptionsHideRoot" , Boolean .toString (voyagerConfiguration .getDisplayOptions ().isHideRoot ()));
65
+ replacements .put ("voyagerHideDocs" , Boolean .toString (voyagerConfiguration .isHideDocs ()));
66
+ replacements .put ("voyagerHideSettings" , Boolean .toString (voyagerConfiguration .isHideSettings ()));
96
67
97
68
98
69
@@ -101,7 +72,7 @@ public String fillIndexTemplate(String contextPath, Map<String, String> params)
101
72
102
73
private String constructGraphQlEndpoint (String contextPath ,
103
74
@ RequestParam Map <String , String > params ) {
104
- String endpoint = graphqlEndpoint ;
75
+ String endpoint = voyagerConfiguration . getEndpoint () ;
105
76
for (Map .Entry <String , String > param : params .entrySet ()) {
106
77
endpoint = endpoint .replaceAll ("\\ {" + param .getKey () + "}" , param .getValue ());
107
78
}
@@ -112,7 +83,7 @@ private String constructGraphQlEndpoint(String contextPath,
112
83
}
113
84
114
85
private String getResourceUrl (String staticBasePath , String staticFileName , String cdnUrl ) {
115
- if (voyagerCdnEnabled && StringUtils .isNotBlank (cdnUrl )) {
86
+ if (voyagerConfiguration . getCdn (). isEnabled () && StringUtils .isNotBlank (cdnUrl )) {
116
87
return cdnUrl ;
117
88
}
118
89
return joinStaticPath (staticBasePath , staticFileName );
0 commit comments