Skip to content

Commit

Permalink
Merge pull request #3012 from HenrikJannsen/api-refactorings
Browse files Browse the repository at this point in the history
Refactor Api module
  • Loading branch information
HenrikJannsen authored Nov 17, 2024
2 parents c681660 + 59914ac commit ddd933a
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 87 deletions.

This file was deleted.

34 changes: 28 additions & 6 deletions apps/rest-api-app/src/main/java/bisq/rest_api/RestApiApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,55 @@

import bisq.application.Executable;
import bisq.common.threading.ThreadName;
import bisq.rest_api.util.StaticFileHandler;
import com.sun.net.httpserver.HttpServer;
import com.typesafe.config.Config;
import lombok.extern.slf4j.Slf4j;
import org.glassfish.jersey.jdkhttp.JdkHttpServerFactory;

import java.net.URI;
import java.util.Optional;

/**
* JAX-RS application for the Bisq REST API
* Swagger docs at: http://localhost:8082/doc/v1/index.html
*/
@Slf4j
public class RestApiApp extends Executable<RestApiApplicationService> {
public static void main(String[] args) {
ThreadName.set(RestApiApp.class, "main");
new RestApiApp(args);
}

private JaxRsApplication jaxRsApplication;
public static final String BASE_PATH = "/api/v1";
private String baseUrl;

private RestApiResourceConfig restApiResourceConfig;
private Optional<HttpServer> httpServer = Optional.empty();

public RestApiApp(String[] args) {
super(args);
}

@Override
protected void launchApplication(String[] args) {
jaxRsApplication = new JaxRsApplication(args,applicationService);
Config restApiConfig = applicationService.getRestApiConfig();
String host = restApiConfig.getString("host");
int port = restApiConfig.getInt("port");
baseUrl = host + ":" + port + BASE_PATH;

restApiResourceConfig = new RestApiResourceConfig(applicationService, baseUrl);

super.launchApplication(args);
}

@Override
protected void onApplicationServiceInitialized(Boolean result, Throwable throwable) {
jaxRsApplication.initialize();
var server = JdkHttpServerFactory.createHttpServer(URI.create(baseUrl), restApiResourceConfig);
server.createContext("/doc", new StaticFileHandler("/doc/v1/"));
server.createContext("/node-monitor", new StaticFileHandler("/node-monitor/"));
log.info("Server started at {}.", baseUrl);
httpServer = Optional.of(server);
}

@Override
Expand All @@ -53,9 +77,7 @@ protected RestApiApplicationService createApplicationService(String[] args) {

@Override
public void shutdown() {
if (jaxRsApplication != null) {
jaxRsApplication.shutdown();
}
httpServer.ifPresent(httpServer -> httpServer.stop(1));

super.shutdown();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,15 @@ public enum State {
private final SystemNotificationService systemNotificationService;
private final TradeService tradeService;
private final BisqEasyService bisqEasyService;
private final com.typesafe.config.Config restApiConfig;

private final Observable<State> state = new Observable<>(State.INITIALIZE_APP);

public RestApiApplicationService(String[] args) {
super("rest_api", args);

restApiConfig= getConfig("restApi");

securityService = new SecurityService(persistenceService, SecurityService.Config.from(getConfig("security")));
com.typesafe.config.Config bitcoinWalletConfig = getConfig("bitcoinWallet");
BitcoinWalletSelection bitcoinWalletSelection = bitcoinWalletConfig.getEnum(BitcoinWalletSelection.class, "bitcoinWalletSelection");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package bisq.rest_api;

import bisq.common.rest_api.error.CustomExceptionMapper;
import bisq.common.rest_api.error.RestApiException;
import bisq.rest_api.report.ReportRestApi;
import bisq.rest_api.util.SerializationModule;
import bisq.rest_api.util.SwaggerResolution;
import bisq.user.identity.UserIdentityRestApi;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.glassfish.jersey.internal.inject.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;

@Slf4j
public class RestApiResourceConfig extends ResourceConfig {
public RestApiResourceConfig(RestApiApplicationService applicationService, String baseUrl) {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new SerializationModule());

register(CustomExceptionMapper.class)
.register(RestApiException.Mapper.class)
.register(mapper);

// Swagger/OpenApi does not work when using instances at register instead of classes.
// As we want to pass the dependencies in the constructor, so we need the hack
// with AbstractBinder to register resources as classes for Swagger
register(SwaggerResolution.class);
register(UserIdentityRestApi.class);
register(ReportRestApi.class);

register(new AbstractBinder() {
@Override
protected void configure() {
bind(new SwaggerResolution(baseUrl)).to(SwaggerResolution.class);
bind(new UserIdentityRestApi(applicationService.getUserService().getUserIdentityService())).to(UserIdentityRestApi.class);
bind(new ReportRestApi(applicationService.getNetworkService(), applicationService.getBondedRolesService())).to(ReportRestApi.class);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@
@Path("/report")
@Produces(MediaType.APPLICATION_JSON)
@Tag(name = "Report API")
public class ReportApi {
public class ReportRestApi {
private final NetworkService networkService;
private final BondedRolesService bondedRolesService;

public ReportApi(NetworkService networkService, BondedRolesService bondedRolesService) {
public ReportRestApi(NetworkService networkService, BondedRolesService bondedRolesService) {
this.networkService = networkService;
this.bondedRolesService = bondedRolesService;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package bisq.rest_api.util;

import bisq.rest_api.JaxRsApplication;
import io.swagger.v3.core.util.Json;
import io.swagger.v3.jaxrs2.Reader;
import io.swagger.v3.oas.annotations.Hidden;
Expand All @@ -39,6 +38,11 @@
@Hidden
public class SwaggerResolution {
private static String swaggerJson;
private final String baseUrl;

public SwaggerResolution(String baseUrl) {
this.baseUrl = baseUrl;
}

@GET
public String swagIt(@Context Application application) {
Expand All @@ -48,14 +52,11 @@ public String swagIt(@Context Application application) {
Info info = new Info()
.title("Bisq 2 REST API")
.description("This is the rest API description for Bisq2, For more Information about Bisq, see https://bisq.network")
// .termsOfService("http://swagger.io/terms/")
// .contact(new Contact()
// .email("[email protected]"))
.license(new License()
.name("GNU Affero General Public License")
.url("https://github.com/bisq-network/bisq2/blob/main/LICENSE"));

api.info(info).addServersItem(new Server().url(JaxRsApplication.BASE_URL));
api.info(info).addServersItem(new Server().url(baseUrl));
SwaggerConfiguration configuration = new SwaggerConfiguration().openAPI(api);
Reader reader = new Reader(configuration);
OpenAPI openAPI = reader.read(application.getClasses());
Expand Down
5 changes: 5 additions & 0 deletions apps/rest-api-app/src/main/resources/rest_api.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ application {
memoryReportIntervalSec = 120
includeThreadListInMemoryReport = true

restApi = {
host = "http://localhost"
port = 8082
}

security = {
keyBundle = {
defaultTorPrivateKey = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Tag(name = "User Identity API")
public class UserIdentityServiceApi {
public class UserIdentityRestApi {
private final UserIdentityService userIdentityService;

public UserIdentityServiceApi(UserIdentityService userIdentityService) {
public UserIdentityRestApi(UserIdentityService userIdentityService) {
this.userIdentityService = userIdentityService;
}

Expand Down

0 comments on commit ddd933a

Please sign in to comment.