1
1
package org .lowcoder .api ;
2
2
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 ;
3
12
import io .swagger .v3 .oas .models .Components ;
4
13
import io .swagger .v3 .oas .models .OpenAPI ;
5
14
import io .swagger .v3 .oas .models .info .Info ;
6
15
import io .swagger .v3 .oas .models .security .SecurityRequirement ;
7
16
import io .swagger .v3 .oas .models .security .SecurityScheme ;
8
17
import 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 ;
13
20
14
21
@ Configuration
15
22
public class OpenAPIDocsConfiguration {
16
23
@ Autowired
17
24
private CommonConfig commonConfig ;
18
25
26
+ @ Value ("${server.port:8080}" )
27
+ private int serverPort ;
28
+
29
+ @ Value ("${spring.webflux.base-path:/}" )
30
+ private String contextPath ;
31
+
19
32
@ Bean
20
33
OpenAPI customizeOpenAPI () {
21
34
final String securitySchemeName = commonConfig .getCookieName ();
@@ -24,7 +37,14 @@ OpenAPI customizeOpenAPI() {
24
37
.title ("Lowcoder API" )
25
38
.version (commonConfig .getApiVersion ()))
26
39
.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
+ )
28
48
.addSecurityItem (new SecurityRequirement ()
29
49
.addList (securitySchemeName )).components (new Components ()
30
50
.addSecuritySchemes (
@@ -33,6 +53,60 @@ OpenAPI customizeOpenAPI() {
33
53
.name (securitySchemeName )
34
54
.type (SecurityScheme .Type .APIKEY )
35
55
.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 ;
37
111
}
38
112
}
0 commit comments