com.trib3.server.modules.DefaultApplicationModule.kt Maven / Gradle / Ivy
package com.trib3.server.modules
import com.codahale.metrics.MetricRegistry
import com.codahale.metrics.health.HealthCheck
import com.codahale.metrics.health.HealthCheckRegistry
import com.google.inject.Scopes
import com.google.inject.multibindings.ProvidesIntoSet
import com.palominolabs.metrics.guice.MetricsInstrumentationModule
import com.trib3.config.modules.KMSModule
import com.trib3.json.modules.ObjectMapperModule
import com.trib3.server.config.TribeApplicationConfig
import com.trib3.server.config.dropwizard.HoconConfigurationFactoryFactory
import com.trib3.server.filters.RequestIdFilter
import com.trib3.server.healthchecks.PingHealthCheck
import com.trib3.server.healthchecks.VersionHealthCheck
import com.trib3.server.resources.PingResource
import dev.misfitlabs.kotlinguice4.multibindings.KotlinMultibinder
import io.dropwizard.Configuration
import io.dropwizard.configuration.ConfigurationFactoryFactory
import org.eclipse.jetty.servlets.CrossOriginFilter
import javax.servlet.Filter
data class ServletFilterConfig(
val name: String,
val filterClass: Class,
val initParameters: Map = emptyMap()
)
/**
* The default guice module, binds things common to dropwizard and serverless execution
*/
class DefaultApplicationModule : TribeApplicationModule() {
override fun configure() {
install(KMSModule())
install(ObjectMapperModule())
// bind HOCON configuration parser
bind>().to>()
// Bind common health checks
val healthChecks = KotlinMultibinder.newSetBinder(kotlinBinder)
healthChecks.addBinding().to()
healthChecks.addBinding().to()
val filterBinder = KotlinMultibinder.newSetBinder(kotlinBinder)
filterBinder.addBinding().toInstance(
ServletFilterConfig(RequestIdFilter::class.java.simpleName, RequestIdFilter::class.java)
)
// Bind ping and graphql resources
resourceBinder().addBinding().to()
// set up metrics for guice created instances
val registry = MetricRegistry()
bind().toInstance(registry)
install(MetricsInstrumentationModule.builder().withMetricRegistry(registry).build())
bind().`in`(Scopes.SINGLETON)
}
/**
* Configure CORS headers to allow the service to be hit from pages hosted
* by the admin port, the app port, or standard HTTP ports on the configured
* [TribeApplicationConfig.corsDomain]
*/
@ProvidesIntoSet
fun provideCorsFilter(appConfig: TribeApplicationConfig): ServletFilterConfig {
val corsDomain =
appConfig.corsDomains.map {
"https?://*.?$it," +
"https?://*.?$it:${appConfig.appPort}"
}.joinToString(",")
val paramMap = mapOf(
CrossOriginFilter.ALLOWED_ORIGINS_PARAM to corsDomain,
CrossOriginFilter.ALLOWED_METHODS_PARAM to "GET,POST,PUT,DELETE,OPTIONS,PATCH,HEAD",
CrossOriginFilter.ALLOW_CREDENTIALS_PARAM to "true"
)
return ServletFilterConfig(
CrossOriginFilter::class.java.simpleName,
CrossOriginFilter::class.java,
paramMap
)
}
// allow multiple installations so that multiple other modules can install this one
override fun equals(other: Any?): Boolean {
return other is DefaultApplicationModule
}
override fun hashCode(): Int {
return this::class.hashCode()
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy