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

feat(entry point): add versioning property to generate entrypoint #582

Merged
merged 6 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
22 changes: 11 additions & 11 deletions docs/docs/tasks/6-generate-entry-point.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ Whether you'll use generic one also parameter `name` is required.
gradle gep --type [entryPointType]
```

| Reference for **entryPointType** | Name | Additional Options |
|----------------------------------|----------------------------------------|---------------------------------------------------------------------------------------------------|
| generic | Empty Entry Point | --name [name] |
| asynceventhandler | Async Event Handler | |
| graphql | API GraphQL | --pathgql [name path] default /graphql |
| kafka | Kafka Consumer | |
| mq | JMS MQ Client to listen messages | |
| restmvc | API REST (Spring Boot Starter Web) | --server [serverOption] default undertow --authorization [true-false] --from-swagger swagger.yaml |
| rsocket | Rsocket Controller Entry Point | |
| sqs | SQS Listener | |
| webflux | API REST (Spring Boot Starter WebFlux) | --router [true, false] default true --authorization [true-false] --from-swagger swagger.yaml |
| Reference for **entryPointType** | Name | Additional Options |
|----------------------------------|----------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------|
| generic | Empty Entry Point | --name [name] |
| asynceventhandler | Async Event Handler | |
| graphql | API GraphQL | --pathgql [name path] default /graphql |
| kafka | Kafka Consumer | |
| mq | JMS MQ Client to listen messages | |
| restmvc | API REST (Spring Boot Starter Web) | --server [serverOption] default undertow --authorization [true,false] --from-swagger swagger.yaml |
| rsocket | Rsocket Controller Entry Point | |
| sqs | SQS Listener | |
| webflux | API REST (Spring Boot Starter WebFlux) | --router [true, false] default true --authorization [true,false] --from-swagger swagger.yaml --versioning [HEADER, PATH,NONE] default NONE |

Additionally, if you'll use a restmvc, you can specify the web server on which the application will run. By default,
undertow.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package co.com.bancolombia.factory.entrypoints;

import static co.com.bancolombia.Constants.APP_SERVICE;
import static co.com.bancolombia.utils.Utils.buildImplementationFromProject;
import static co.com.bancolombia.utils.Utils.buildTestImplementation;
import static co.com.bancolombia.utils.Utils.*;

import co.com.bancolombia.exceptions.CleanException;
import co.com.bancolombia.exceptions.ParamNotFoundException;
import co.com.bancolombia.factory.ModuleBuilder;
import co.com.bancolombia.factory.ModuleFactory;
import co.com.bancolombia.factory.validations.ReactiveTypeValidation;
Expand All @@ -14,9 +14,13 @@
public class EntryPointWebflux implements ModuleFactory {
@Override
public void buildModule(ModuleBuilder builder) throws IOException, CleanException {
VersioningStrategy versioningStrategy =
(VersioningStrategy) builder.getParam("task-param-versioning-strategy");

builder.runValidations(ReactiveTypeValidation.class);
if (Boolean.TRUE.equals(builder.getBooleanParam("task-param-router"))) {
builder.setupFromTemplate("entry-point/rest-webflux/router-functions");
setupTemplate(builder, versioningStrategy);

} else {
builder.setupFromTemplate("entry-point/rest-webflux");
// to run archunit validations
Expand Down Expand Up @@ -56,4 +60,29 @@ public void buildModule(ModuleBuilder builder) throws IOException, CleanExceptio
.appendToProperties("cors")
.put("allowed-origins", "http://localhost:4200,http://localhost:8080");
}

private static void setupTemplate(ModuleBuilder builder, VersioningStrategy versioningStrategy)
throws IOException, ParamNotFoundException {
String templatePath = "entry-point/rest-webflux/router-functions";

switch (versioningStrategy) {
case HEADER:
templatePath += "/header";
break;
case PATH:
templatePath += "/path";
break;
case NONE:
default:
break;
}

builder.setupFromTemplate(templatePath);
}

public enum VersioningStrategy {
HEADER,
PATH,
NONE
}
}
15 changes: 15 additions & 0 deletions src/main/java/co/com/bancolombia/task/GenerateEntryPointTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static co.com.bancolombia.Constants.PATH_GRAPHQL;

import co.com.bancolombia.factory.entrypoints.EntryPointRestMvcServer.Server;
import co.com.bancolombia.factory.entrypoints.EntryPointWebflux.VersioningStrategy;
import co.com.bancolombia.task.annotations.CATask;
import java.util.Arrays;
import java.util.List;
Expand All @@ -17,6 +18,7 @@ public class GenerateEntryPointTask extends AbstractResolvableTypeTask {
private String pathGraphql = PATH_GRAPHQL;
private String swaggerFile = null;
private Server server = Server.UNDERTOW;
private VersioningStrategy versioning = VersioningStrategy.NONE;
private BooleanOption router = BooleanOption.TRUE;
private BooleanOption swagger = BooleanOption.FALSE;
private BooleanOption eda = BooleanOption.FALSE;
Expand Down Expand Up @@ -44,6 +46,13 @@ public void setFromSwagger(String swaggerFile) {
this.swaggerFile = swaggerFile;
}

@Option(
option = "versioning",
description = "define an api versioning strategy available only with router function")
public void setVersioning(VersioningStrategy versioning) {
this.versioning = versioning;
}

@Option(option = "pathgql", description = "set API GraphQL path")
public void setPathGraphql(String pathgql) {
this.pathGraphql = pathgql;
Expand Down Expand Up @@ -84,9 +93,15 @@ public List<BooleanOption> getAuthorizeOptions() {
return Arrays.asList(BooleanOption.values());
}

@OptionValues("versioning")
public List<VersioningStrategy> getVersioningOptions() {
return Arrays.asList(VersioningStrategy.values());
}

@Override
protected void prepareParams() {
builder.addParam("task-param-server", server);
builder.addParam("task-param-versioning-strategy", versioning);
builder.addParam("task-param-pathgql", pathGraphql);
builder.addParam("task-param-router", router == BooleanOption.TRUE);
builder.addParam("task-param-authorize", authorization == BooleanOption.TRUE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class Handler {
@PreAuthorize("hasRole('permissionGET')")
{{/task-param-authorize}}
public Mono<ServerResponse> listenGETUseCase(ServerRequest serverRequest) {
// usecase.logic();
// useCase.logic();
return ServerResponse.ok().bodyValue("");
}

Expand All @@ -43,7 +43,7 @@ public class Handler {
@PreAuthorize("hasRole('permissionPOST')")
{{/task-param-authorize}}
public Mono<ServerResponse> listenPOSTUseCase(ServerRequest serverRequest) {
// usecase.logic();
// useCase.logic();
return ServerResponse.ok().bodyValue("");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"folders": [
"infrastructure/entry-points/reactive-web/src/test/java/{{packagePath}}/api"
],
"files": {
"entry-point/rest-webflux/router-functions/header/router.java.mustache": "infrastructure/entry-points/reactive-web/src/main/java/{{packagePath}}/api/RouterRest.java",
"entry-point/rest-webflux/router-functions/header/handler.java.mustache": "infrastructure/entry-points/reactive-web/src/main/java/{{packagePath}}/api/HandlerV1.java",
"entry-point/rest-webflux/router-functions/header/handler2.java.mustache": "infrastructure/entry-points/reactive-web/src/main/java/{{packagePath}}/api/HandlerV2.java",
"entry-point/rest-webflux/cors-config.java.mustache": "infrastructure/entry-points/reactive-web/src/main/java/{{packagePath}}/api/config/CorsConfig.java",
"entry-point/rest-webflux/security-headers-filter.java.mustache": "infrastructure/entry-points/reactive-web/src/main/java/{{packagePath}}/api/config/SecurityHeadersConfig.java",
"entry-point/rest-webflux/router-functions/header/router.unit.test.java.mustache": "infrastructure/entry-points/reactive-web/src/test/java/{{packagePath}}/api/RouterRestTest.java",
"entry-point/rest-webflux/build.gradle.mustache": "infrastructure/entry-points/reactive-web/build.gradle"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package {{package}}.api;

{{#lombok}}
import lombok.RequiredArgsConstructor;
{{/lombok}}
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
{{#task-param-authorize}}
import org.springframework.security.access.prepost.PreAuthorize;
{{/task-param-authorize}}
import reactor.core.publisher.Mono;

@Component
{{#lombok}}
@RequiredArgsConstructor
{{/lombok}}
public class HandlerV1 {
//private final UseCase useCase;
//private final UseCase2 useCase2;
{{^lombok}}
public HandlerV1() {
}
{{/lombok}}

{{#task-param-authorize}}
@PreAuthorize("hasRole('permissionGET')")
{{/task-param-authorize}}
public Mono<ServerResponse> listenGETUseCase(ServerRequest serverRequest) {
// useCase.logic();
return ServerResponse.ok().bodyValue("");
}

{{#task-param-authorize}}
@PreAuthorize("hasRole('permissionGETOther')")
{{/task-param-authorize}}
public Mono<ServerResponse> listenGETOtherUseCase(ServerRequest serverRequest) {
// useCase2.logic();
return ServerResponse.ok().bodyValue("");
}

{{#task-param-authorize}}
@PreAuthorize("hasRole('permissionPOST')")
{{/task-param-authorize}}
public Mono<ServerResponse> listenPOSTUseCase(ServerRequest serverRequest) {
// useCase.logic();
return ServerResponse.ok().bodyValue("");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package {{package}}.api;

{{#lombok}}
import lombok.RequiredArgsConstructor;
{{/lombok}}
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
{{#task-param-authorize}}
import org.springframework.security.access.prepost.PreAuthorize;
{{/task-param-authorize}}
import reactor.core.publisher.Mono;

@Component
{{#lombok}}
@RequiredArgsConstructor
{{/lombok}}
public class HandlerV2 {
//private final UseCase useCase;
//private final UseCase2 useCase2;
{{^lombok}}
public HandlerV2() {
}
{{/lombok}}

{{#task-param-authorize}}
@PreAuthorize("hasRole('permissionGET')")
{{/task-param-authorize}}
public Mono<ServerResponse> listenGETUseCase(ServerRequest serverRequest) {
// useCase.logic();
return ServerResponse.ok().bodyValue("");
}

{{#task-param-authorize}}
@PreAuthorize("hasRole('permissionGETOther')")
{{/task-param-authorize}}
public Mono<ServerResponse> listenGETOtherUseCase(ServerRequest serverRequest) {
// useCase2.logic();
return ServerResponse.ok().bodyValue("");
}

{{#task-param-authorize}}
@PreAuthorize("hasRole('permissionPOST')")
{{/task-param-authorize}}
public Mono<ServerResponse> listenPOSTUseCase(ServerRequest serverRequest) {
// useCase.logic();
return ServerResponse.ok().bodyValue("");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package {{package}}.api;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;
import org.springframework.web.reactive.function.server.ServerResponse;

@Configuration
public class RouterRest {

@Bean
public RouterFunction<ServerResponse> route(HandlerV1 handlerV1, HandlerV2 handlerV2) {
return RouterFunctions
.route()
.path("/api", builder -> builder
.nest(RequestPredicates.headers(headers ->
headers.asHttpHeaders().getAccept().contains(MediaType.valueOf("application/vnd.api.v1+json"))),
nestedBuilder -> nestedBuilder
.GET("/usecase/path", handlerV1::listenGETUseCase)
.GET("/otherusercase/path", handlerV1::listenGETOtherUseCase)
.POST("/usecase/otherpath", handlerV1::listenPOSTUseCase)

)
.nest(RequestPredicates.headers(headers ->
headers.asHttpHeaders().getAccept().contains(MediaType.valueOf("application/vnd.api.v2+json"))),
nestedBuilder -> nestedBuilder
.GET("/usecase/path", handlerV2::listenGETUseCase)
.GET("/otherusercase/path", handlerV2::listenGETOtherUseCase)
.POST("/usecase/otherpath", handlerV2::listenPOSTUseCase)
)
)
.build();
}
}

Loading
Loading