fix(server, shared): TODO
This commit is contained in:
@@ -1,11 +1,22 @@
|
|||||||
package at.mocode.server
|
package at.mocode.server
|
||||||
|
|
||||||
import at.mocode.server.plugins.configureDatabase
|
import at.mocode.server.plugins.configureDatabase
|
||||||
|
import io.ktor.http.HttpHeaders
|
||||||
|
import io.ktor.http.HttpMethod
|
||||||
|
import io.ktor.http.HttpStatusCode
|
||||||
|
import io.ktor.serialization.kotlinx.json.json
|
||||||
import io.ktor.server.application.*
|
import io.ktor.server.application.*
|
||||||
import io.ktor.server.netty.*
|
import io.ktor.server.netty.*
|
||||||
|
import io.ktor.server.plugins.calllogging.CallLogging
|
||||||
|
import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
|
||||||
|
import io.ktor.server.plugins.cors.routing.CORS
|
||||||
|
import io.ktor.server.plugins.defaultheaders.DefaultHeaders
|
||||||
|
import io.ktor.server.plugins.statuspages.StatusPages
|
||||||
import io.ktor.server.response.*
|
import io.ktor.server.response.*
|
||||||
import io.ktor.server.routing.*
|
import io.ktor.server.routing.*
|
||||||
|
import kotlinx.serialization.json.Json
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
|
import org.slf4j.event.Level
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Main entry point for the application.
|
* Main entry point for the application.
|
||||||
@@ -28,7 +39,7 @@ fun Application.module() {
|
|||||||
configureDatabase()
|
configureDatabase()
|
||||||
|
|
||||||
// Configure plugins
|
// Configure plugins
|
||||||
// configurePlugins()
|
configurePlugins()
|
||||||
|
|
||||||
// Configure routing
|
// Configure routing
|
||||||
configureRouting()
|
configureRouting()
|
||||||
@@ -39,84 +50,84 @@ fun Application.module() {
|
|||||||
/**
|
/**
|
||||||
* Configures all Ktor plugins for the application
|
* Configures all Ktor plugins for the application
|
||||||
*/
|
*/
|
||||||
//private fun Application.configurePlugins() {
|
private fun Application.configurePlugins() {
|
||||||
// val log = LoggerFactory.getLogger("ApplicationPlugins")
|
val log = LoggerFactory.getLogger("ApplicationPlugins")
|
||||||
// // Add default headers to all responses
|
// Add default headers to all responses
|
||||||
// install(DefaultHeaders) {
|
install(DefaultHeaders) {
|
||||||
// header("X-Engine", "Ktor")
|
header("X-Engine", "Ktor")
|
||||||
// header("X-Content-Type-Options", "nosniff")
|
header("X-Content-Type-Options", "nosniff")
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// // Configure call logging
|
// Configure call logging
|
||||||
// install(CallLogging) {
|
install(CallLogging) {
|
||||||
// level = Level.INFO
|
level = Level.INFO
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// // Configure content negotiation with JSON
|
// Configure content negotiation with JSON
|
||||||
// install(ContentNegotiation) {
|
install(ContentNegotiation) {
|
||||||
// json(Json {
|
json(Json {
|
||||||
// prettyPrint = true
|
prettyPrint = true
|
||||||
// isLenient = true
|
isLenient = true
|
||||||
// ignoreUnknownKeys = true
|
ignoreUnknownKeys = true
|
||||||
// })
|
})
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// // Configure CORS
|
// Configure CORS
|
||||||
// install(CORS) {
|
install(CORS) {
|
||||||
// // Default CORS configuration
|
// Default CORS configuration
|
||||||
// anyHost()
|
anyHost()
|
||||||
// allowMethod(HttpMethod.Options)
|
allowMethod(HttpMethod.Options)
|
||||||
// allowMethod(HttpMethod.Get)
|
allowMethod(HttpMethod.Get)
|
||||||
// allowMethod(HttpMethod.Post)
|
allowMethod(HttpMethod.Post)
|
||||||
// allowMethod(HttpMethod.Put)
|
allowMethod(HttpMethod.Put)
|
||||||
// allowMethod(HttpMethod.Delete)
|
allowMethod(HttpMethod.Delete)
|
||||||
// allowHeader(HttpHeaders.ContentType)
|
allowHeader(HttpHeaders.ContentType)
|
||||||
// allowHeader(HttpHeaders.Authorization)
|
allowHeader(HttpHeaders.Authorization)
|
||||||
//
|
|
||||||
// // Try to read from config to override defaults
|
// Try to read from config to override defaults
|
||||||
// try {
|
try {
|
||||||
// val appEnv = this@configurePlugins.environment.config
|
val appEnv = this@configurePlugins.environment.config
|
||||||
// if (appEnv.propertyOrNull("cors") != null) {
|
if (appEnv.propertyOrNull("cors") != null) {
|
||||||
// val corsConfig = appEnv.config("cors")
|
val corsConfig = appEnv.config("cors")
|
||||||
//
|
|
||||||
// // Clear default anyHost if we have specific hosts
|
// Clear default anyHost if we have specific hosts
|
||||||
// if (corsConfig.propertyOrNull("allowedHosts") != null) {
|
if (corsConfig.propertyOrNull("allowedHosts") != null) {
|
||||||
// val hosts = corsConfig.property("allowedHosts").getList()
|
val hosts = corsConfig.property("allowedHosts").getList()
|
||||||
// if (hosts.isNotEmpty()) {
|
if (hosts.isNotEmpty()) {
|
||||||
// hosts.forEach { host ->
|
hosts.forEach { host ->
|
||||||
// allowHost(host)
|
allowHost(host)
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// // Allow credentials if configured
|
// Allow credentials if configured
|
||||||
// if (corsConfig.propertyOrNull("allowCredentials") != null) {
|
if (corsConfig.propertyOrNull("allowCredentials") != null) {
|
||||||
// allowCredentials = corsConfig.property("allowCredentials").getString().toBoolean()
|
allowCredentials = corsConfig.property("allowCredentials").getString().toBoolean()
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// } catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
// // Log the error but continue with default configuration
|
// Log the error but continue with default configuration
|
||||||
// this@configurePlugins.log.warn("Failed to configure CORS from config, using defaults: ${e.message}")
|
this@configurePlugins.log.warn("Failed to configure CORS from config, using defaults: ${e.message}")
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// // Configure status pages for error handling
|
// Configure status pages for error handling
|
||||||
// install(StatusPages) {
|
install(StatusPages) {
|
||||||
// exception<Throwable> { call, cause ->
|
exception<Throwable> { call, cause ->
|
||||||
// call.respondText(
|
call.respondText(
|
||||||
// text = "500: ${cause.message ?: "Internal Server Error"}",
|
text = "500: ${cause.message ?: "Internal Server Error"}",
|
||||||
// status = HttpStatusCode.InternalServerError
|
status = HttpStatusCode.InternalServerError
|
||||||
// )
|
)
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// status(HttpStatusCode.NotFound) { call, _ ->
|
status(HttpStatusCode.NotFound) { call, _ ->
|
||||||
// call.respondText(
|
call.respondText(
|
||||||
// text = "404: Page Not Found",
|
text = "404: Page Not Found",
|
||||||
// status = HttpStatusCode.NotFound
|
status = HttpStatusCode.NotFound
|
||||||
// )
|
)
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
//}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configures all routes for the application
|
* Configures all routes for the application
|
||||||
|
|||||||
Reference in New Issue
Block a user