/** * Binds the specified {@link Service} at the specified path pattern of the default {@link VirtualHost}. * * @deprecated Use {@link #service(String, Service)} instead. */ @Deprecated public ServerBuilder serviceAt(String pathPattern, Service<HttpRequest, HttpResponse> service) { return service(pathPattern, service); }
private static void addPrometheusExposition(ArmeriaSettings armeriaSettings, ServerBuilder server, PrometheusMeterRegistry registry) { requireNonNull(armeriaSettings, "armeriaSettings"); requireNonNull(server, "server"); requireNonNull(registry, "registry"); final String metricsPath = armeriaSettings.getMetricsPath(); if (metricsPath == null) { return; } final CollectorRegistry prometheusRegistry = registry.getPrometheusRegistry(); server.service(metricsPath, new PrometheusExpositionService(prometheusRegistry)); }
private static void addPrometheusExposition(ArmeriaSettings armeriaSettings, ServerBuilder server, PrometheusMeterRegistry registry) { requireNonNull(armeriaSettings, "armeriaSettings"); requireNonNull(server, "server"); requireNonNull(registry, "registry"); final String metricsPath = armeriaSettings.getMetricsPath(); if (metricsPath == null) { return; } final CollectorRegistry prometheusRegistry = registry.getPrometheusRegistry(); server.service(metricsPath, new PrometheusExpositionService(prometheusRegistry)); }
@Override protected void configure(ServerBuilder sb) throws Exception { sb.service("/hello", THttpService.of(helloService)); } };
/** * Adds HTTP services to the specified {@link ServerBuilder}. */ public static void configureHttpServices( ServerBuilder server, List<HttpServiceRegistrationBean> beans, @Nullable MeterIdPrefixFunctionFactory meterIdPrefixFunctionFactory) { requireNonNull(server, "server"); requireNonNull(beans, "beans"); beans.forEach(bean -> { Service<HttpRequest, HttpResponse> service = bean.getService(); for (Function<Service<HttpRequest, HttpResponse>, ? extends Service<HttpRequest, HttpResponse>> decorator : bean.getDecorators()) { service = service.decorate(decorator); } service = setupMetricCollectingService(service, bean, meterIdPrefixFunctionFactory); server.service(bean.getPathMapping(), service); }); }
public static void main(String[] args) throws Exception { final SamlServiceProvider ssp = samlServiceProvider(); final Server server = new ServerBuilder() .https(8443) // You can add this certificate to your trust store in order to make your web browser happy. .tls(new File(ClassLoader.getSystemResource("localhost.crt").toURI()), new File(ClassLoader.getSystemResource("localhost.key").toURI())) // Decorate you service with SAML decorator. .annotatedService("/", new MyService(), ssp.newSamlDecorator()) // Add SAML service to your server which handles a SAML response and a metadata request. .service(ssp.newSamlService()) .build(); Runtime.getRuntime().addShutdownHook(new Thread(() -> { server.stop().join(); logger.info("Server has been stopped."); })); server.start().join(); logger.info("Server has been started."); } }
/** * Adds HTTP services to the specified {@link ServerBuilder}. */ public static void configureHttpServices( ServerBuilder server, List<HttpServiceRegistrationBean> beans, @Nullable MeterIdPrefixFunctionFactory meterIdPrefixFunctionFactory) { requireNonNull(server, "server"); requireNonNull(beans, "beans"); beans.forEach(bean -> { Service<HttpRequest, HttpResponse> service = bean.getService(); for (Function<Service<HttpRequest, HttpResponse>, ? extends Service<HttpRequest, HttpResponse>> decorator : bean.getDecorators()) { service = service.decorate(decorator); } service = setupMetricCollectingService(service, bean, meterIdPrefixFunctionFactory); server.service(bean.getPathMapping(), service); }); }
@Override protected void configure(ServerBuilder sb) throws Exception { sb.service("/serverIp", THttpService.of(handler)); } }
private static ServerBuilder configureService(ServerBuilder sb, HttpHandler httpHandler, DataBufferFactoryWrapper<?> factoryWrapper, @Nullable String serverHeader) { final ArmeriaHttpHandlerAdapter handler = new ArmeriaHttpHandlerAdapter(httpHandler, factoryWrapper); return sb.service(PathMapping.ofCatchAll(), (ctx, req) -> { final CompletableFuture<HttpResponse> future = new CompletableFuture<>(); final HttpResponse response = HttpResponse.from(future); final Disposable disposable = handler.handle(ctx, req, future, serverHeader).subscribe(); response.completionFuture().whenComplete((unused, cause) -> { if (cause != null) { logger.debug("{} Response stream has been cancelled.", ctx, cause); disposable.dispose(); } }); return response; }); }
@Override protected void configure(ServerBuilder sb) throws Exception { loggingService = new MockedStructuredLoggingService<>( THttpService.of((HelloService.Iface) name -> "Hello " + name)); sb.service("/hello", loggingService); } };
@Override protected void configure(ServerBuilder sb) throws Exception { sb.meterRegistry(registry); sb.service("/helloservice", THttpService.of((Iface) name -> { if ("world".equals(name)) { return "success"; } throw new IllegalArgumentException("bad argument"); }).decorate(MetricCollectingService.newDecorator( MeterIdPrefixFunction.ofDefault("armeria.server.HelloService")))); } };
@Override protected void configure(ServerBuilder sb) throws Exception { sb.meterRegistry(registry); final THttpService helloService = THttpService.of((Iface) name -> { if ("world".equals(name)) { return "success"; } throw new IllegalArgumentException("bad argument"); }); sb.service("/foo", helloService.decorate( MetricCollectingService.newDecorator(new MeterIdPrefixFunctionImpl("server", "Foo")))); sb.service("/bar", helloService.decorate( MetricCollectingService.newDecorator(new MeterIdPrefixFunctionImpl("server", "Bar")))); sb.service("/internal/prometheus/metrics", new PrometheusExpositionService(prometheusRegistry)); } };
@Override protected void configure(ServerBuilder sb) throws Exception { // Used for testing if changing the timeout dynamically works. sb.service("/sleep", ThriftCallService.of(sleepService) .decorate(DynamicTimeoutService::new) .decorate(THttpService.newDecorator())); // Used for testing if disabling the timeout dynamically works. sb.service("/fakeSleep", ThriftCallService.of(fakeSleepService) .decorate(TimeoutDisablingService::new) .decorate(THttpService.newDecorator())); sb.defaultRequestTimeout(Duration.ofSeconds(1)); } };
@Override protected void configure(ServerBuilder sb) { sb.service("/trailers", (ctx, req) -> { HttpClient client = HttpClient.of(backendServer.uri("/")); return client.get("/trailers"); }); sb.service("/trailers-only", (ctx, req) -> { HttpClient client = HttpClient.of(backendServer.uri("/")); return client.get("/trailers-only"); }); sb.decorator(LoggingService.newDecorator()); } };
@Override protected void configure(ServerBuilder sb) { sb.service("/thrift-never", ThriftCallService.of(serviceHandler) .decorate(ThrottlingRpcService.newDecorator(never())) .decorate(THttpService.newDecorator())); sb.service("/thrift-always", ThriftCallService.of(serviceHandler) .decorate(ThrottlingRpcService.newDecorator(always())) .decorate(THttpService.newDecorator())); } };
@Override protected void configure(ServerBuilder sb) throws Exception { final SslContext sslContext = SslContextBuilder.forServer(serverCert.certificateFile(), serverCert.privateKeyFile()) .trustManager(InsecureTrustManagerFactory.INSTANCE) .clientAuth(ClientAuth.REQUIRE) .build(); sb.tls(sslContext) .service("/", (ctx, req) -> HttpResponse.of("success")) .decorator(new LoggingServiceBuilder().newDecorator()); } };
@Override protected void configure(ServerBuilder sb) throws Exception { sb.gracefulShutdownTimeout(1000L, 2000L); sb.defaultRequestTimeoutMillis(0); // Disable RequestTimeoutException. sb.service("/sleep", THttpService.of( (AsyncIface) (milliseconds, resultHandler) -> RequestContext.current().eventLoop().schedule( () -> resultHandler.onComplete(milliseconds), milliseconds, MILLISECONDS))); } };
@Before public void startServers() { servers = new ArrayList<>(); for (Endpoint endpoint : sampleEndpoints) { final Server server = new ServerBuilder().http(endpoint.port()) .service("/", new EchoService()) .build(); final ServerListener listener = new ZooKeeperUpdatingListenerBuilder( instance().connectString().get(), zNode) .sessionTimeoutMillis(sessionTimeoutMillis) .endpoint(endpoint) .build(); server.addListener(listener); server.start().join(); servers.add(server); } }