Skip to content

Commit

Permalink
[AMORO-3256] Amoro Open Api with Swagger in Ams (#3296)
Browse files Browse the repository at this point in the history
  • Loading branch information
mansonliwh authored Nov 26, 2024
1 parent b5f6372 commit 9e0f057
Show file tree
Hide file tree
Showing 9 changed files with 2,531 additions and 8 deletions.
97 changes: 96 additions & 1 deletion amoro-ams/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
<url>https://amoro.apache.org</url>

<properties>
<httpmime.version>4.5.13</httpmime.version>
<openapi-generator-maven-plugin.version>6.0.0</openapi-generator-maven-plugin.version>
<swagger-ui.version>5.17.14</swagger-ui.version>
<git-commit-id-plugin.fail-on-no-git-dir>false</git-commit-id-plugin.fail-on-no-git-dir>
</properties>

Expand Down Expand Up @@ -279,7 +282,7 @@
<groupId>software.amazon.awssdk</groupId>
<artifactId>url-connection-client</artifactId>
</dependency>

<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3-transfer-manager</artifactId>
Expand Down Expand Up @@ -408,6 +411,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>${swagger-ui.version}</version>
</dependency>

<!-- testcontainers dependencies -->
<dependency>
<groupId>org.testcontainers</groupId>
Expand Down Expand Up @@ -544,5 +553,91 @@
<git-commit-id-plugin.fail-on-no-git-dir>true</git-commit-id-plugin.fail-on-no-git-dir>
</properties>
</profile>
<profile>
<id>generate-sdk</id>
<build>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${openapi-generator-maven-plugin.version}</version>
<executions>
<execution>
<id>generate-sdk</id>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/openapi/openapi.yaml</inputSpec>
<generatorName>java</generatorName>
<output>${project.build.directory}/generated-sources/openapi</output>
<apiPackage>com.amoro.sdk.api</apiPackage>
<modelPackage>com.amoro.sdk.model</modelPackage>
<invokerPackage>com.amoro.sdk.invoker</invokerPackage>
<configOptions>
<library>apache-httpclient</library>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>package-sdk</id>
<goals>
<goal>jar</goal>
</goals>
<phase>package</phase>
<configuration>
<classifier>sdk</classifier>
<classesDirectory>
${project.build.directory}/generated-sources/openapi/src/main/java
</classesDirectory>
<outputDirectory>${project.build.directory}</outputDirectory>
<finalName>sdk</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>${httpmime.version}</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>generate-doc</id>
<build>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>${openapi-generator-maven-plugin.version}</version>
<executions>
<execution>
<id>generate-doc</id>
<goals>
<goal>generate</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/openapi/openapi.yaml</inputSpec>
<generatorName>html2</generatorName>
<output>${project.build.directory}/generated-docs/openapi</output>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import io.javalin.Javalin;
import io.javalin.http.HttpCode;
import io.javalin.http.staticfiles.Location;
import org.apache.amoro.Constants;
import org.apache.amoro.OptimizerProperties;
import org.apache.amoro.api.AmoroTableMetastore;
Expand Down Expand Up @@ -242,11 +243,14 @@ private void initHttpService() {
Javalin.create(
config -> {
config.addStaticFiles(dashboardServer.configStaticFiles());
config.addStaticFiles("/META-INF/resources/webjars", Location.CLASSPATH);
config.sessionHandler(SessionHandler::new);
config.enableCorsForAllOrigins();
config.jsonMapper(JavalinJsonMapper.createDefaultJsonMapper());
config.showJavalinBanner = false;
config.enableWebjars();
});

httpServer.routes(
() -> {
dashboardServer.endpoints().addEndpoints();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public class DashboardServer {
private static final String AUTH_TYPE_BASIC = "basic";
private static final String X_REQUEST_SOURCE_HEADER = "X-Request-Source";
private static final String X_REQUEST_SOURCE_WEB = "Web";
private static final String SWAGGER_PATH = "/openapi-ui";

private final CatalogController catalogController;
private final HealthCheckController healthCheckController;
Expand Down Expand Up @@ -118,7 +119,6 @@ public DashboardServer(
}

private String indexHtml = "";

// read index.html content
public String getIndexFileContent() {
try {
Expand Down Expand Up @@ -170,6 +170,17 @@ public EndpointGroup endpoints() {
path(
"",
() -> {
get(
"/swagger-docs",
ctx -> {
InputStream openapiStream =
getClass().getClassLoader().getResourceAsStream("openapi/openapi.yaml");
if (openapiStream == null) {
ctx.status(404).result("OpenAPI specification file not found");
} else {
ctx.result(openapiStream);
}
});
// static files
get(
"/{page}",
Expand Down Expand Up @@ -358,6 +369,9 @@ private EndpointGroup apiGroup() {
public void preHandleRequest(Context ctx) {
String uriPath = ctx.path();
String requestSource = ctx.header(X_REQUEST_SOURCE_HEADER);
if (uriPath.startsWith(SWAGGER_PATH)) {
return;
}
if (needApiKeyCheck(uriPath) && !X_REQUEST_SOURCE_WEB.equalsIgnoreCase(requestSource)) {
if (AUTH_TYPE_BASIC.equalsIgnoreCase(authType)) {
BasicAuthCredentials cred = ctx.basicAuthCredentials();
Expand Down Expand Up @@ -410,6 +424,9 @@ public void handleException(Exception e, Context ctx) {
"/index.html",
"/favicon.ico",
"/assets/*",
"/openapi-ui",
"/openapi-ui/*",
"/swagger-docs",
RestCatalogService.ICEBERG_REST_API_PREFIX + "/*"
};

Expand Down
Loading

0 comments on commit 9e0f057

Please sign in to comment.