11package org .lowcoder .api ;
22
3+ import java .util .Arrays ;
4+
5+ import org .lowcoder .sdk .config .CommonConfig ;
6+ import org .springframework .beans .factory .annotation .Autowired ;
7+ import org .springframework .beans .factory .annotation .Value ;
8+ import org .springframework .context .annotation .Bean ;
9+ import org .springframework .context .annotation .Configuration ;
10+
11+ import io .swagger .v3 .oas .annotations .enums .SecuritySchemeIn ;
312import io .swagger .v3 .oas .models .Components ;
413import io .swagger .v3 .oas .models .OpenAPI ;
514import io .swagger .v3 .oas .models .info .Info ;
615import io .swagger .v3 .oas .models .security .SecurityRequirement ;
716import io .swagger .v3 .oas .models .security .SecurityScheme ;
817import io .swagger .v3 .oas .models .servers .Server ;
9- import org .springframework .context .annotation .Bean ;
10- import org .springframework .context .annotation .Configuration ;
11- import org .lowcoder .sdk .config .CommonConfig ;
12- import org .springframework .beans .factory .annotation .Autowired ;
18+ import io .swagger .v3 .oas .models .servers .ServerVariable ;
19+ import io .swagger .v3 .oas .models .servers .ServerVariables ;
1320
1421@ Configuration
1522public class OpenAPIDocsConfiguration {
1623 @ Autowired
1724 private CommonConfig commonConfig ;
1825
26+ @ Value ("${server.port:8080}" )
27+ private int serverPort ;
28+
29+ @ Value ("${spring.webflux.base-path:/}" )
30+ private String contextPath ;
31+
1932 @ Bean
2033 OpenAPI customizeOpenAPI () {
2134 final String securitySchemeName = commonConfig .getCookieName ();
@@ -24,7 +37,14 @@ OpenAPI customizeOpenAPI() {
2437 .title ("Lowcoder API" )
2538 .version (commonConfig .getApiVersion ()))
2639 .addServersItem (new Server ()
27- .url ("/" ))
40+ .url (createLocalServerUrl ("localhost" , serverPort , contextPath ))
41+ .description ("Local development API service" )
42+ )
43+ .addServersItem (createCustomServer ())
44+ .addServersItem (new Server ()
45+ .url ("https://api-service.lowcoder.cloud/" )
46+ .description ("Lowcoder Community Edition: Public Cloud API Access" )
47+ )
2848 .addSecurityItem (new SecurityRequirement ()
2949 .addList (securitySchemeName )).components (new Components ()
3050 .addSecuritySchemes (
@@ -33,6 +53,60 @@ OpenAPI customizeOpenAPI() {
3353 .name (securitySchemeName )
3454 .type (SecurityScheme .Type .APIKEY )
3555 .in (SecurityScheme .In .COOKIE )
36- ));
56+ )
57+ .addSecuritySchemes (
58+ "API Key" ,
59+ new SecurityScheme ()
60+ .name ("API key" )
61+ .type (SecurityScheme .Type .HTTP )
62+ .scheme ("bearer" )
63+ .bearerFormat ("JWT" )
64+ )
65+ );
66+ }
67+
68+
69+ private static String createLocalServerUrl (String domain , int port , String contextPath )
70+ {
71+ StringBuilder sb = new StringBuilder ("http" );
72+
73+ if (port == 443 )
74+ {
75+ sb .append ("s" );
76+ }
77+ sb .append ("://" ).append (domain );
78+
79+ if (port != 80 && port != 443 )
80+ {
81+ sb .append (":" ).append (port );
82+ }
83+ sb .append (contextPath );
84+
85+ return sb .toString ();
86+ }
87+
88+ private Server createCustomServer ()
89+ {
90+ String url = "{scheme}://{domain}:{port}{basePath}" ;
91+
92+ Server server = new Server ()
93+ .description ("Lowcoder Self-hosted Installation API Access" )
94+ .url (url )
95+ .variables (new ServerVariables ()
96+ .addServerVariable ("scheme" , new ServerVariable ()
97+ ._default ("http" )
98+ .description ("HTTP scheme" )
99+ ._enum (Arrays .asList ("http" , "https" )))
100+ .addServerVariable ("domain" , new ServerVariable ()
101+ .description ("Lowcoder IP address or domain" )
102+ ._default ("localhost" ))
103+ .addServerVariable ("port" , new ServerVariable ()
104+ .description ("Port" )
105+ ._default ("3000" ))
106+ .addServerVariable ("basePath" , new ServerVariable ()
107+ .description ("Base path" )
108+ ._default (contextPath ))
109+ );
110+ return server ;
37111 }
38112}
0 commit comments