yifan-c commented on code in PR #200:
URL: https://github.com/apache/cassandra-sidecar/pull/200#discussion_r1980412430


##########
server/src/main/java/org/apache/cassandra/sidecar/modules/ApiModule.java:
##########
@@ -0,0 +1,166 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.cassandra.sidecar.modules;
+
+import com.google.inject.AbstractModule;
+import com.google.inject.Provides;
+import com.google.inject.Singleton;
+import com.google.inject.multibindings.ProvidesIntoMap;
+import io.netty.handler.codec.http.HttpResponseStatus;
+import io.vertx.core.Vertx;
+import io.vertx.core.VertxOptions;
+import io.vertx.core.file.FileSystemOptions;
+import io.vertx.ext.dropwizard.DropwizardMetricsOptions;
+import io.vertx.ext.dropwizard.Match;
+import io.vertx.ext.dropwizard.MatchType;
+import io.vertx.ext.web.Router;
+import io.vertx.ext.web.handler.ErrorHandler;
+import io.vertx.ext.web.handler.LoggerHandler;
+import io.vertx.ext.web.handler.TimeoutHandler;
+import org.apache.cassandra.sidecar.common.ApiEndpointsV1;
+import org.apache.cassandra.sidecar.config.FileSystemOptionsConfiguration;
+import org.apache.cassandra.sidecar.config.SidecarConfiguration;
+import org.apache.cassandra.sidecar.config.VertxConfiguration;
+import org.apache.cassandra.sidecar.config.VertxMetricsConfiguration;
+import org.apache.cassandra.sidecar.handlers.JsonErrorHandler;
+import org.apache.cassandra.sidecar.handlers.RouteBuilder;
+import org.apache.cassandra.sidecar.handlers.RoutingOrder;
+import org.apache.cassandra.sidecar.handlers.TimeSkewHandler;
+import org.apache.cassandra.sidecar.logging.SidecarLoggerHandler;
+import org.apache.cassandra.sidecar.metrics.MetricRegistryFactory;
+import org.apache.cassandra.sidecar.modules.multibindings.KeyClassMapKey;
+import 
org.apache.cassandra.sidecar.modules.multibindings.MultiBindingTypeResolver;
+import org.apache.cassandra.sidecar.modules.multibindings.RouteClassKey;
+import org.apache.cassandra.sidecar.modules.multibindings.VertxRouteMapKeys;
+import org.apache.cassandra.sidecar.routes.SettableVertxRoute;
+import org.apache.cassandra.sidecar.routes.VertxRoute;
+
+import static 
org.apache.cassandra.sidecar.common.ApiEndpointsV1.API_V1_ALL_ROUTES;
+
+/**
+ * Provides the glue code for API definition and the related, i.e. Vertx, 
handlers, etc.
+ * <p>Note that feature-specific routes are defined in the corresponding 
modules, e.g. {@link HealthCheckModule}
+ */
+public class ApiModule extends AbstractModule
+{
+    @Provides
+    @Singleton
+    Router vertxRouter(Vertx vertx, MultiBindingTypeResolver<VertxRoute> 
resolver)
+    {
+        Router router = Router.router(vertx);
+        resolver.resolve().forEach((routeClassKey, route) -> {
+            try
+            {
+                if (RouteClassKey.class.isAssignableFrom(routeClassKey))
+                {
+                    //noinspection unchecked
+                    Class<? extends RouteClassKey> key = (Class<? extends 
RouteClassKey>) routeClassKey;
+                    SettableVertxRoute settableVertxRoute = 
(SettableVertxRoute) route;
+                    settableVertxRoute.setRouteClassKey(key);
+                }
+                route.mountTo(router);
+            }
+            catch (Throwable cause)
+            {
+                throw new RuntimeException("Failed to mount route: " + 
routeClassKey.getSimpleName(), cause);
+            }
+        });
+        return router;
+    }
+
+    @Provides
+    @Singleton
+    Vertx vertx(SidecarConfiguration sidecarConfiguration, 
MetricRegistryFactory metricRegistryFactory)
+    {
+        VertxMetricsConfiguration metricsConfig = 
sidecarConfiguration.metricsConfiguration().vertxConfiguration();
+        Match serverRouteMatch = new 
Match().setValue(API_V1_ALL_ROUTES).setType(MatchType.REGEX);
+        DropwizardMetricsOptions dropwizardMetricsOptions
+        = new DropwizardMetricsOptions().setEnabled(metricsConfig.enabled())
+                                        
.setJmxEnabled(metricsConfig.exposeViaJMX())
+                                        
.setJmxDomain(metricsConfig.jmxDomainName())
+                                        
.setMetricRegistry(metricRegistryFactory.getOrCreate())
+                                        // Monitor all V1 endpoints.
+                                        // Additional filtering is done by 
configuring yaml fields 'metrics.include|exclude'
+                                        
.addMonitoredHttpServerRoute(serverRouteMatch);
+
+        VertxOptions vertxOptions = new 
VertxOptions().setMetricsOptions(dropwizardMetricsOptions);
+        VertxConfiguration vertxConfiguration = 
sidecarConfiguration.vertxConfiguration();
+        FileSystemOptionsConfiguration fsOptions = vertxConfiguration != null 
? vertxConfiguration.filesystemOptionsConfiguration() : null;
+
+        if (fsOptions != null)
+        {
+            vertxOptions.setFileSystemOptions(new FileSystemOptions()
+                                              
.setClassPathResolvingEnabled(fsOptions.classpathResolvingEnabled())
+                                              
.setFileCacheDir(fsOptions.fileCacheDir())
+                                              
.setFileCachingEnabled(fsOptions.fileCachingEnabled()));
+        }
+
+        return Vertx.vertx(vertxOptions);
+    }
+
+    @Provides
+    @Singleton
+    ErrorHandler errorHandler()
+    {
+        return new JsonErrorHandler();
+    }
+
+    @Provides
+    @Singleton
+    LoggerHandler loggerHandler()
+    {
+        return SidecarLoggerHandler.create(LoggerHandler.create());
+    }
+
+    @ProvidesIntoMap
+    @KeyClassMapKey(VertxRouteMapKeys.GlobalUtilityHandlerKey.class)
+    VertxRoute globalUtilityHandler(SidecarConfiguration sidecarConfiguration,
+                                    LoggerHandler loggerHandler)
+    {
+        return VertxRoute.create(router -> {

Review Comment:
   The router builder is used to define the APIs (route with endpoint and 
verb). For this case, it only needs to mount the handler to the root route. The 
handlers does not target any specific API. I, therefore, named them global 
utility handlers. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org
For additional commands, e-mail: pr-h...@cassandra.apache.org

Reply via email to