Skip to content

Commit af01df0

Browse files
committed
new: enhance server section of OpenAPI spec
1 parent 8ee4e1e commit af01df0

File tree

2 files changed

+82
-6
lines changed

2 files changed

+82
-6
lines changed
Lines changed: 80 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
package 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;
312
import io.swagger.v3.oas.models.Components;
413
import io.swagger.v3.oas.models.OpenAPI;
514
import io.swagger.v3.oas.models.info.Info;
615
import io.swagger.v3.oas.models.security.SecurityRequirement;
716
import io.swagger.v3.oas.models.security.SecurityScheme;
817
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;
1320

1421
@Configuration
1522
public 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
}

server/api-service/lowcoder-server/src/main/resources/selfhost/ce/application.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ spring:
1515
allow-circular-references: true
1616
codec:
1717
max-in-memory-size: 20MB
18+
webflux:
19+
context-path: /
1820

1921
server:
2022
compression:

0 commit comments

Comments
 (0)