Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] openapi && swagger #234

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 80 additions & 41 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.alipay.sofa</groupId>
<artifactId>registry-parent</artifactId>
Expand Down Expand Up @@ -61,7 +61,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<bolt.version>1.6.3</bolt.version>
<commons.lang.version>2.6</commons.lang.version>
<google.guava.version>18.0</google.guava.version>
<google.guava.version>23.0</google.guava.version>
<sofa.common.tools.version>1.0.12</sofa.common.tools.version>
<junit.version>4.12</junit.version>
<junit.jupiter.version>5.0.2</junit.jupiter.version>
Expand All @@ -79,10 +79,12 @@
<metrics.version>4.0.2</metrics.version>
<commons-io.version>2.4</commons-io.version>
<jetty.version>[9.4.17.v20190418,9.4.19.v20190610]</jetty.version>
<jackson.version>2.12.0</jackson.version>
<rocksdbjni.version>6.4.6</rocksdbjni.version>
<guava-retrying.version>2.0.0</guava-retrying.version>
<zstd-jni.version>1.3.6-1</zstd-jni.version>
<log4j2.version>2.17.0</log4j2.version>
<swagger2.version>1.6.2</swagger2.version>
<main.user.dir>${user.dir}</main.user.dir>
<argLine>-Dnetwork_interface_denylist=docker0</argLine>
<!-- for linke 添加isSkipUT, isSkipIT参数声明,防止编译时产生错误 -->
Expand Down Expand Up @@ -372,6 +374,7 @@
<version>0.9.0</version>
</dependency>


<!-- connect db -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -450,6 +453,41 @@
<artifactId>zstd-jni</artifactId>
<version>${zstd-jni.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
<version>2.3.9.RELEASE</version>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to define a variable like `spring-boot.version

</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
<version>${swagger2.version}</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>3.22.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.hk2</groupId>
<artifactId>spring-bridge</artifactId>
<version>2.5.0-b34</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -516,7 +554,8 @@
</executions>
<configuration>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties
</generateGitPropertiesFilename>
<includeOnlyProperties>
<includeOnlyProperty>^git.build.(time|version)$</includeOnlyProperty>
<includeOnlyProperty>^git.commit.id.(abbrev|full)$</includeOnlyProperty>
Expand All @@ -525,45 +564,45 @@
</configuration>
</plugin>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>1.20.0</version>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>1.20.0</version>
<executions>
<execution>
<goals>
<goal>apply</goal>
</goals>
<phase>compile</phase>
</execution>
<execution>
<goals>
<goal>apply</goal>
</goals>
<phase>compile</phase>
</execution>
</executions>
<configuration>
<formats>
<format>
<!-- define the files to apply to -->
<includes>
<include>.xml</include>
<include>.gitignore</include>
<include>.properties</include>
</includes>
<trimTrailingWhitespace/>
<endWithNewline/>
</format>
</formats>
<java>
<!-- include all java files in "java" folders under "src" -->
<includes>
<include>src/**/java/**/*.java</include>
</includes>
<removeUnusedImports />
<googleJavaFormat>
<version>1.7</version>
<style>GOOGLE</style>
</googleJavaFormat>
<licenseHeader>
<file>${user.dir}/tools/codestyle/HEADER</file>
</licenseHeader>
</java>
</configuration>
<configuration>
<formats>
<format>
<!-- define the files to apply to -->
<includes>
<include>.xml</include>
<include>.gitignore</include>
<include>.properties</include>
</includes>
<trimTrailingWhitespace/>
<endWithNewline/>
</format>
</formats>
<java>
<!-- include all java files in "java" folders under "src" -->
<includes>
<include>src/**/java/**/*.java</include>
</includes>
<removeUnusedImports/>
<googleJavaFormat>
<version>1.7</version>
<style>GOOGLE</style>
</googleJavaFormat>
<licenseHeader>
<file>${user.dir}/tools/codestyle/HEADER</file>
</licenseHeader>
</java>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
Expand Down Expand Up @@ -608,7 +647,7 @@
</includes>
<excludes>
<!-- 这里需要根据自己的需要指定要排除单元测试 -->
<!-- <exclude>com/alipay/**/*.java</exclude>-->
<!-- <exclude>com/alipay/**/*.java</exclude>-->
</excludes>
<!-- 如无特殊需求,将forkMode设置为once -->
<forkMode>once</forkMode>
Expand Down
4 changes: 4 additions & 0 deletions server/remoting/http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,9 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@
import java.util.concurrent.atomic.AtomicBoolean;
import javax.ws.rs.ProcessingException;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.server.handler.ContextHandler;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Slf4jLog;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.glassfish.jersey.internal.guava.ThreadFactoryBuilder;
import org.glassfish.jersey.jetty.JettyHttpContainer;
Expand All @@ -47,11 +50,11 @@
* @author shangyu.wh
* @version $Id: Jersey.java, v 0.1 2018-01-29 17:57 shangyu.wh Exp $
*/
public class JerseyJettyServer implements Server {
public class JettyServer implements Server {
private static final String NCSA_FORMAT = "%{client}a \"%r\" %s %O";
private static final Logger LOGGER = LoggerFactory.getLogger(JerseyJettyServer.class);
private static final Logger LOGGER = LoggerFactory.getLogger(JettyServer.class);

private final ResourceConfig resourceConfig;
private final Handler handler;

private final URI baseUri;

Expand All @@ -62,11 +65,11 @@ public class JerseyJettyServer implements Server {
/**
* constructor
*
* @param resourceConfig
* @param handler
* @param baseUri
*/
public JerseyJettyServer(ResourceConfig resourceConfig, URI baseUri) {
this.resourceConfig = resourceConfig;
public JettyServer(Handler handler, URI baseUri) {
this.handler = handler;
this.baseUri = baseUri;
}

Expand All @@ -75,7 +78,7 @@ public void startServer() {
if (isStarted.compareAndSet(false, true)) {
try {
Log.setLog(new Slf4jLog());
server = createServer(getBaseUri(), resourceConfig, true);
server = createServer(getBaseUri(), handler, true);
} catch (Throwable e) {
isStarted.set(false);
LOGGER.error("Start Jetty jersey server error!", e);
Expand All @@ -84,15 +87,29 @@ public void startServer() {
}
}

public static Handler createSwaggerHandler() {
return createStaticHandler("/swagger-doc", "swagger-doc", "index.html");
}

public static Handler createStaticHandler(
String contextPath, String resourceName, String welcomeFile) {
ResourceHandler staticHandler = new ResourceHandler();
staticHandler.setDirectoriesListed(true);
staticHandler.setWelcomeFiles(new String[] {welcomeFile});

ContextHandler staticContextHandler = new ContextHandler();
staticContextHandler.setContextPath(contextPath);
staticContextHandler.setBaseResource(Resource.newClassPathResource(resourceName));
staticContextHandler.setHandler(staticHandler);
return staticContextHandler;
}

public static org.eclipse.jetty.server.Server createServer(
final URI uri, final ResourceConfig resourceConfig, final boolean start) {
final URI uri, final Handler handler, final boolean start) {
if (uri == null) {
throw new IllegalArgumentException(LocalizationMessages.URI_CANNOT_BE_NULL());
}

JettyHttpContainer handler =
ContainerFactory.createContainer(JettyHttpContainer.class, resourceConfig);

int defaultPort = Container.DEFAULT_HTTP_PORT;

final int port = (uri.getPort() == -1) ? defaultPort : uri.getPort();
Expand All @@ -107,9 +124,7 @@ public static org.eclipse.jetty.server.Server createServer(
http.setAcceptQueueSize(512);
http.setPort(port);
server.setConnectors(new Connector[] {http});
if (handler != null) {
server.setHandler(handler);
}
server.setHandler(handler);

if (start) {
try {
Expand Down Expand Up @@ -222,4 +237,8 @@ public URI getBaseUri() {
public int getChannelCount() {
return 0;
}

public static Handler createHandler(ResourceConfig config) {
return ContainerFactory.createContainer(JettyHttpContainer.class, config);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.registry.remoting.jersey.config;

import io.swagger.jaxrs.listing.SwaggerSerializers;
import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;

/** @Author dzdx @Date 2022/6/8 14:13 @Version 1.0 */
public abstract class JerseyConfig extends ResourceConfig {

public JerseyConfig() {
this.register(JacksonFeature.class);
this.register(SwaggerSerializers.class);
}
}
Loading