Last active
July 6, 2023 15:22
-
-
Save ikhoon/ad74b7f49993fa25caa947cc87535ee8 to your computer and use it in GitHub Desktop.
Implementation of a service group that allows multiple services to be grouped together and bound to multiple prefixes.
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
package com.example.armeria; | |
import static org.assertj.core.api.Assertions.assertThat; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Set; | |
import org.junit.jupiter.api.Test; | |
import org.junit.jupiter.api.extension.RegisterExtension; | |
import com.linecorp.armeria.client.BlockingWebClient; | |
import com.linecorp.armeria.common.HttpRequest; | |
import com.linecorp.armeria.common.HttpResponse; | |
import com.linecorp.armeria.server.HttpService; | |
import com.linecorp.armeria.server.HttpServiceWithRoutes; | |
import com.linecorp.armeria.server.Route; | |
import com.linecorp.armeria.server.ServerBuilder; | |
import com.linecorp.armeria.server.ServiceRequestContext; | |
import com.linecorp.armeria.testing.junit5.server.ServerExtension; | |
class ServiceGroupTest { | |
@RegisterExtension | |
static ServerExtension server = new ServerExtension() { | |
@Override | |
protected void configure(ServerBuilder sb) { | |
HttpService barService = (ctx, req) -> HttpResponse.of("bar"); | |
HttpService bazService = (ctx, req) -> HttpResponse.of("baz"); | |
final ServiceGroup serviceGroup = ServiceGroup.builder() | |
.addService("/bar", barService) | |
.addService("/baz", bazService) | |
.build(); | |
sb.service(serviceGroup); | |
sb.serviceUnder("/foo", serviceGroup); | |
} | |
}; | |
@Test | |
void testServiceGroup() { | |
final BlockingWebClient client = server.blockingWebClient(); | |
assertThat(client.get("/bar").contentUtf8()).isEqualTo("bar"); | |
assertThat(client.get("/baz").contentUtf8()).isEqualTo("baz"); | |
assertThat(client.get("/foo/bar").contentUtf8()).isEqualTo("bar"); | |
assertThat(client.get("/foo/baz").contentUtf8()).isEqualTo("baz"); | |
} | |
static class ServiceGroup implements HttpServiceWithRoutes { | |
static ServiceGroupBuilder builder() { | |
return new ServiceGroupBuilder(); | |
} | |
private final Map<Route, HttpService> serviceMap; | |
ServiceGroup(Map<Route, HttpService> serviceMap) { | |
this.serviceMap = serviceMap; | |
} | |
@Override | |
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception { | |
final HttpService service = serviceMap.get(ctx.config().mappedRoute()); | |
assert service != null; | |
return service.serve(ctx, req); | |
} | |
@Override | |
public Set<Route> routes() { | |
return serviceMap.keySet(); | |
} | |
} | |
static class ServiceGroupBuilder { | |
private final Map<Route, HttpService> serviceMap = new HashMap<>(); | |
ServiceGroupBuilder addService(String pathPattern, HttpService service) { | |
serviceMap.put(Route.builder() | |
.path(pathPattern) | |
.build(), | |
service); | |
return this; | |
} | |
ServiceGroupBuilder addService(Route route, HttpService service) { | |
serviceMap.put(route, service); | |
return this; | |
} | |
ServiceGroup build() { | |
return new ServiceGroup(serviceMap); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment