-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make ServerVerticle handler configurable
The `ServerVerticle` attaches request handlers to the route. The request handlers attached to the route cannot be changed at the moment. This change makes the request handlers that the `ServerVerticle` attaches to the route configurable. A `RoutingHandlerFactory` is used to create an instance of the `Handler<RoutingContext>` class. In the `ServerConfig`, a list of `RoutingHandlerFactory` class names is used to specify the `RoutingHandlerFactory`. The handler created by the `RoutingHandlerFactory` is then added to the root route. The request handlers are added in the order specified in the `ServerConfig` list.
- Loading branch information
Showing
22 changed files
with
717 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# ServerVerticle configuration | ||
|
||
## Configuration | ||
|
||
### Handler factories | ||
|
||
Handler factories are used to register routing handlers on the root router in the ServerVerticle. A handler factory must | ||
implement the `io.neonbee.internal.handler.factories.RoutingHandlerFactory` interface and must be configured in the | ||
handlerFactories section of the ServerVerticle configuration. The configured handlerFactories are loaded and added in | ||
the configured order. | ||
The order of handler factories has to take the priority of the returned handlers into account. | ||
See `io.vertx.ext.web.impl.RouteState.weight` If the order is violated, an Exception is thrown. | ||
|
||
#### Default configuration | ||
|
||
```yaml | ||
handlerFactories: | ||
- io.neonbee.internal.handler.factories.LoggerHandlerFactory | ||
- io.neonbee.internal.handler.factories.InstanceInfoHandlerFactory | ||
- io.neonbee.internal.handler.factories.CorrelationIdHandlerFactory | ||
- io.neonbee.internal.handler.factories.TimeoutHandlerFactory | ||
- io.neonbee.internal.handler.factories.SessionHandlerFactory | ||
- io.neonbee.internal.handler.factories.CacheControlHandlerFactory | ||
- io.neonbee.internal.handler.factories.DisallowingFileUploadBodyHandlerFactory | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/io/neonbee/internal/handler/factories/CacheControlHandlerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.neonbee.internal.handler.factories; | ||
|
||
import static io.vertx.core.Future.succeededFuture; | ||
|
||
import io.neonbee.internal.handler.CacheControlHandler; | ||
import io.vertx.core.Future; | ||
import io.vertx.core.Handler; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
/** | ||
* Create the {@link CacheControlHandler}. | ||
*/ | ||
public class CacheControlHandlerFactory implements RoutingHandlerFactory { | ||
|
||
@Override | ||
public Future<Handler<RoutingContext>> createHandler() { | ||
return succeededFuture(new CacheControlHandler()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/io/neonbee/internal/handler/factories/CorrelationIdHandlerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.neonbee.internal.handler.factories; | ||
|
||
import static io.vertx.core.Future.succeededFuture; | ||
|
||
import io.neonbee.NeonBee; | ||
import io.neonbee.config.ServerConfig; | ||
import io.neonbee.internal.handler.CorrelationIdHandler; | ||
import io.vertx.core.Future; | ||
import io.vertx.core.Handler; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
/** | ||
* Create the {@link CorrelationIdHandler}. | ||
*/ | ||
public class CorrelationIdHandlerFactory implements RoutingHandlerFactory { | ||
|
||
@Override | ||
public Future<Handler<RoutingContext>> createHandler() { | ||
ServerConfig serverConfig = NeonBee.get().getServerConfig(); | ||
return succeededFuture(new CorrelationIdHandler(serverConfig.getCorrelationStrategy())); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...n/java/io/neonbee/internal/handler/factories/DisallowingFileUploadBodyHandlerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.neonbee.internal.handler.factories; | ||
|
||
import static io.vertx.core.Future.succeededFuture; | ||
|
||
import io.vertx.core.Future; | ||
import io.vertx.core.Handler; | ||
import io.vertx.ext.web.RoutingContext; | ||
import io.vertx.ext.web.handler.BodyHandler; | ||
|
||
/** | ||
* Create A {@link BodyHandler} that disallows file uploads. | ||
*/ | ||
public class DisallowingFileUploadBodyHandlerFactory implements RoutingHandlerFactory { | ||
|
||
@Override | ||
public Future<Handler<RoutingContext>> createHandler() { | ||
return succeededFuture(BodyHandler.create(false /* do not handle file uploads */)); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/io/neonbee/internal/handler/factories/InstanceInfoHandlerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.neonbee.internal.handler.factories; | ||
|
||
import static io.vertx.core.Future.succeededFuture; | ||
|
||
import io.neonbee.internal.handler.InstanceInfoHandler; | ||
import io.vertx.core.Future; | ||
import io.vertx.core.Handler; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
/** | ||
* Create the {@link InstanceInfoHandler}. | ||
*/ | ||
public class InstanceInfoHandlerFactory implements RoutingHandlerFactory { | ||
|
||
@Override | ||
public Future<Handler<RoutingContext>> createHandler() { | ||
return succeededFuture(new InstanceInfoHandler()); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/io/neonbee/internal/handler/factories/LoggerHandlerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.neonbee.internal.handler.factories; | ||
|
||
import static io.vertx.core.Future.succeededFuture; | ||
|
||
import io.neonbee.internal.handler.LoggerHandler; | ||
import io.vertx.core.Future; | ||
import io.vertx.core.Handler; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
/** | ||
* Create the {@link LoggerHandler}. | ||
*/ | ||
public class LoggerHandlerFactory implements RoutingHandlerFactory { | ||
|
||
@Override | ||
public Future<Handler<RoutingContext>> createHandler() { | ||
return succeededFuture(new LoggerHandler()); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/io/neonbee/internal/handler/factories/RoutingHandlerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.neonbee.internal.handler.factories; | ||
|
||
import io.neonbee.internal.verticle.ServerVerticle; | ||
import io.vertx.core.Future; | ||
import io.vertx.core.Handler; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
/** | ||
* Factory interface for creating routing handlers. | ||
* | ||
* This interface is evaluated when {@link ServerVerticle} is started. The returned handlers will be added to the root | ||
* route. | ||
*/ | ||
public interface RoutingHandlerFactory { | ||
|
||
/** | ||
* Returns an instance of the {@link Handler} object to add to the main route. | ||
* | ||
* @return the {@link Handler} object | ||
*/ | ||
Future<Handler<RoutingContext>> createHandler(); | ||
} |
69 changes: 69 additions & 0 deletions
69
src/main/java/io/neonbee/internal/handler/factories/SessionHandlerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package io.neonbee.internal.handler.factories; | ||
|
||
import java.util.Optional; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
|
||
import io.neonbee.NeonBee; | ||
import io.neonbee.config.ServerConfig; | ||
import io.vertx.core.Future; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.ext.web.RoutingContext; | ||
import io.vertx.ext.web.handler.PlatformHandler; | ||
import io.vertx.ext.web.handler.SessionHandler; | ||
import io.vertx.ext.web.sstore.ClusteredSessionStore; | ||
import io.vertx.ext.web.sstore.LocalSessionStore; | ||
import io.vertx.ext.web.sstore.SessionStore; | ||
|
||
/** | ||
* Creates a SessionHandler. | ||
* | ||
* If ServerConfig.SessionHandling is set to NONE a no-operation handler is returned, which does not perform any session | ||
* handling. | ||
*/ | ||
public class SessionHandlerFactory implements RoutingHandlerFactory { | ||
|
||
@VisibleForTesting | ||
static final class NoOpHandler implements PlatformHandler { | ||
@Override | ||
public void handle(RoutingContext routingContext) { | ||
routingContext.next(); | ||
} | ||
} | ||
|
||
@Override | ||
public Future<Handler<RoutingContext>> createHandler() { | ||
ServerConfig config = NeonBee.get().getServerConfig(); | ||
Handler<RoutingContext> sh = | ||
createSessionStore(NeonBee.get().getVertx(), config.getSessionHandling()).map(SessionHandler::create) | ||
.map(sessionHandler -> sessionHandler.setSessionCookieName(config.getSessionCookieName())) | ||
.map(sessionHandler -> (Handler<RoutingContext>) sessionHandler).orElseGet(NoOpHandler::new); | ||
return Future.succeededFuture(sh); | ||
} | ||
|
||
/** | ||
* Creates a {@linkplain SessionStore} based on the given {@linkplain ServerConfig} to use either local or clustered | ||
* session handling. If no session handling should be used, an empty optional is returned. | ||
* | ||
* @param vertx the Vert.x instance to create the {@linkplain SessionStore} for | ||
* @param sessionHandling the session handling type | ||
* @return an optional session store, suitable for the given Vert.x instance and based on the provided config value | ||
* (none/local/clustered). In case the session handling is set to clustered, but Vert.x does not run in | ||
* clustered mode, fallback to the local session handling. | ||
*/ | ||
@VisibleForTesting | ||
static Optional<SessionStore> createSessionStore(Vertx vertx, ServerConfig.SessionHandling sessionHandling) { | ||
switch (sessionHandling) { | ||
case LOCAL: | ||
return Optional.of(LocalSessionStore.create(vertx)); | ||
case CLUSTERED: | ||
if (!vertx.isClustered()) { // Behaves like clustered in case that instance isn't clustered | ||
return Optional.of(LocalSessionStore.create(vertx)); | ||
} | ||
return Optional.of(ClusteredSessionStore.create(vertx)); | ||
default: /* nothing to do here, no session handling, so neither add a cookie, nor a session handler */ | ||
return Optional.empty(); | ||
} | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/io/neonbee/internal/handler/factories/TimeoutHandlerFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package io.neonbee.internal.handler.factories; | ||
|
||
import static io.vertx.core.Future.succeededFuture; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import io.neonbee.NeonBee; | ||
import io.neonbee.config.ServerConfig; | ||
import io.vertx.core.Future; | ||
import io.vertx.core.Handler; | ||
import io.vertx.ext.web.RoutingContext; | ||
import io.vertx.ext.web.handler.TimeoutHandler; | ||
|
||
/** | ||
* Create the {@link TimeoutHandler}. | ||
*/ | ||
public class TimeoutHandlerFactory implements RoutingHandlerFactory { | ||
|
||
@Override | ||
public Future<Handler<RoutingContext>> createHandler() { | ||
ServerConfig serverConfig = NeonBee.get().getServerConfig(); | ||
return succeededFuture(TimeoutHandler.create(TimeUnit.SECONDS.toMillis(serverConfig.getTimeout()), | ||
serverConfig.getTimeoutStatusCode())); | ||
} | ||
} |
Oops, something went wrong.