Umbau zu SCS
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
package at.mocode.gateway.config
|
||||
|
||||
import io.ktor.server.application.*
|
||||
import org.jetbrains.exposed.sql.Database
|
||||
import org.jetbrains.exposed.sql.SchemaUtils
|
||||
import org.jetbrains.exposed.sql.transactions.transaction
|
||||
|
||||
/**
|
||||
* Database configuration for the API Gateway.
|
||||
*
|
||||
* Sets up database connections and schema initialization for all bounded contexts.
|
||||
*/
|
||||
fun Application.configureDatabase() {
|
||||
val databaseUrl = environment.config.propertyOrNull("database.url")?.getString()
|
||||
?: "jdbc:postgresql://localhost:5432/meldestelle"
|
||||
val databaseUser = environment.config.propertyOrNull("database.user")?.getString()
|
||||
?: "meldestelle_user"
|
||||
val databasePassword = environment.config.propertyOrNull("database.password")?.getString()
|
||||
?: "meldestelle_password"
|
||||
|
||||
// Initialize database connection
|
||||
Database.connect(
|
||||
url = databaseUrl,
|
||||
driver = "org.postgresql.Driver",
|
||||
user = databaseUser,
|
||||
password = databasePassword
|
||||
)
|
||||
|
||||
// Initialize database schemas for all contexts
|
||||
transaction {
|
||||
// Import table definitions from all contexts
|
||||
try {
|
||||
// Master Data Context tables
|
||||
SchemaUtils.createMissingTablesAndColumns(
|
||||
at.mocode.masterdata.infrastructure.repository.LandTable
|
||||
)
|
||||
|
||||
// Member Management Context tables
|
||||
SchemaUtils.createMissingTablesAndColumns(
|
||||
at.mocode.members.infrastructure.repository.PersonTable,
|
||||
at.mocode.members.infrastructure.repository.VereinTable
|
||||
)
|
||||
|
||||
// Horse Registry Context tables
|
||||
SchemaUtils.createMissingTablesAndColumns(
|
||||
at.mocode.horses.infrastructure.repository.HorseTable
|
||||
)
|
||||
|
||||
log.info("Database schemas initialized successfully")
|
||||
} catch (e: Exception) {
|
||||
log.error("Failed to initialize database schemas: ${e.message}")
|
||||
// In production, you might want to fail fast here
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package at.mocode.gateway.config
|
||||
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.plugins.calllogging.*
|
||||
import io.ktor.server.plugins.statuspages.*
|
||||
import io.ktor.server.request.*
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.response.*
|
||||
import at.mocode.dto.base.BaseDto
|
||||
import org.slf4j.event.Level
|
||||
|
||||
/**
|
||||
* Monitoring and logging configuration for the API Gateway.
|
||||
*
|
||||
* Configures request logging, error handling, and status pages.
|
||||
*/
|
||||
fun Application.configureMonitoring() {
|
||||
install(CallLogging) {
|
||||
level = Level.INFO
|
||||
filter { call -> call.request.path().startsWith("/api") }
|
||||
format { call ->
|
||||
val status = call.response.status()
|
||||
val httpMethod = call.request.httpMethod.value
|
||||
val userAgent = call.request.headers["User-Agent"]
|
||||
"$status: $httpMethod ${call.request.path()} - $userAgent"
|
||||
}
|
||||
}
|
||||
|
||||
install(StatusPages) {
|
||||
exception<Throwable> { call, cause ->
|
||||
call.application.log.error("Unhandled exception", cause)
|
||||
call.respond(
|
||||
HttpStatusCode.InternalServerError,
|
||||
BaseDto.error<Any>("Internal server error: ${cause.message}")
|
||||
)
|
||||
}
|
||||
|
||||
status(HttpStatusCode.NotFound) { call, status ->
|
||||
call.respond(
|
||||
status,
|
||||
BaseDto.error<Any>("Endpoint not found: ${call.request.path()}")
|
||||
)
|
||||
}
|
||||
|
||||
status(HttpStatusCode.Unauthorized) { call, status ->
|
||||
call.respond(
|
||||
status,
|
||||
BaseDto.error<Any>("Authentication required")
|
||||
)
|
||||
}
|
||||
|
||||
status(HttpStatusCode.Forbidden) { call, status ->
|
||||
call.respond(
|
||||
status,
|
||||
BaseDto.error<Any>("Access forbidden")
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package at.mocode.gateway.config
|
||||
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.plugins.cors.routing.*
|
||||
import io.ktor.http.*
|
||||
|
||||
/**
|
||||
* Security configuration for the API Gateway.
|
||||
*
|
||||
* Configures CORS, authentication, and other security-related settings.
|
||||
*/
|
||||
fun Application.configureSecurity() {
|
||||
install(CORS) {
|
||||
allowMethod(HttpMethod.Options)
|
||||
allowMethod(HttpMethod.Put)
|
||||
allowMethod(HttpMethod.Delete)
|
||||
allowMethod(HttpMethod.Patch)
|
||||
allowHeader(HttpHeaders.Authorization)
|
||||
allowHeader(HttpHeaders.ContentType)
|
||||
allowHeader("X-Requested-With")
|
||||
|
||||
// Allow requests from common development origins
|
||||
allowHost("localhost:3000")
|
||||
allowHost("localhost:8080")
|
||||
allowHost("127.0.0.1:3000")
|
||||
allowHost("127.0.0.1:8080")
|
||||
|
||||
// In production, configure specific allowed origins
|
||||
anyHost() // This should be restricted in production
|
||||
}
|
||||
|
||||
// TODO: Add JWT authentication configuration
|
||||
// install(Authentication) {
|
||||
// jwt("auth-jwt") {
|
||||
// // JWT configuration
|
||||
// }
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package at.mocode.gateway.config
|
||||
|
||||
import io.ktor.serialization.kotlinx.json.*
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.plugins.contentnegotiation.*
|
||||
import kotlinx.serialization.json.Json
|
||||
|
||||
/**
|
||||
* Serialization configuration for the API Gateway.
|
||||
*
|
||||
* Configures JSON serialization settings that are consistent across all bounded contexts.
|
||||
*/
|
||||
fun Application.configureSerialization() {
|
||||
install(ContentNegotiation) {
|
||||
json(Json {
|
||||
prettyPrint = true
|
||||
isLenient = true
|
||||
ignoreUnknownKeys = true
|
||||
encodeDefaults = true
|
||||
explicitNulls = false
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user