@@ -27,113 +27,111 @@ import org.koin.logger.slf4jLogger
2727private val logger = KotlinLogging .logger {}
2828
2929suspend fun main () {
30- // Start Koin for dependency injection
31- val koinApp = startKoin {
32- slf4jLogger()
33- modules(AppModule ().module)
34- }
30+ // Start Koin for dependency injection
31+ val koinApp = startKoin {
32+ slf4jLogger()
33+ modules(AppModule ().module)
34+ }
3535
36- val config = koinApp.koin.get<ClusterConfig >()
36+ val config = koinApp.koin.get<ClusterConfig >()
3737
38- logger.info { " Starting OpenBMCLAPI Kotlin version ${Version .current} " }
39- logger.info { " Cluster ID: ${config.clusterId} " }
38+ logger.info { " Starting OpenBMCLAPI Kotlin version ${Version .current} " }
39+ logger.info { " Cluster ID: ${config.clusterId} " }
4040
41- // Determine if we should use HTTPS and setup certificates
42- var useHttps = false
43- var keystorePath: String? = null
41+ // Determine if we should use HTTPS and setup certificates
42+ var useHttps = false
43+ var keystorePath: String? = null
4444
45- try {
46- val tokenManager = koinApp.koin.get<TokenManager >()
47- val clusterService = koinApp.koin.get<ClusterService >()
48- val certificateService = koinApp.koin.get<CertificateService >()
45+ try {
46+ val tokenManager = koinApp.koin.get<TokenManager >()
47+ val clusterService = koinApp.koin.get<ClusterService >()
48+ val certificateService = koinApp.koin.get<CertificateService >()
4949
50- // Get token first
51- tokenManager.getToken()
50+ // Get token first
51+ tokenManager.getToken()
5252
53- // Connect to cluster (needed for non-BYOC certificate requests)
54- clusterService.connect()
53+ // Connect to cluster (needed for non-BYOC certificate requests)
54+ clusterService.connect()
5555
56- // Setup certificates (will either load local or request from server)
57- useHttps = certificateService.setupCertificates()
56+ // Setup certificates (will either load local or request from server)
57+ useHttps = certificateService.setupCertificates()
5858
59- if (useHttps) {
60- keystorePath = certificateService.getKeystorePath()
61- logger.info { " HTTPS enabled with keystore at: $keystorePath " }
62- } else {
63- logger.info { " HTTP mode - no certificates available" }
64- }
65- } catch (e: Exception ) {
66- logger.warn(e) { " Failed to setup certificates, using HTTP" }
67- useHttps = false
59+ if (useHttps) {
60+ keystorePath = certificateService.getKeystorePath()
61+ logger.info { " HTTPS enabled with keystore at: $keystorePath " }
62+ } else {
63+ logger.info { " HTTP mode - no certificates available" }
6864 }
65+ } catch (e: Exception ) {
66+ logger.warn(e) { " Failed to setup certificates, using HTTP" }
67+ useHttps = false
68+ }
69+
70+ val env = koinApp.koin.get<ApplicationEnvironment >()
71+
72+ // Create and start embedded server with appropriate configuration
73+ val server =
74+ embeddedServer(
75+ Netty ,
76+ env,
77+ configure = {
78+ // Add HTTPS connector if certificates are available, otherwise HTTP
79+ if (useHttps && keystorePath != null && File (keystorePath).exists()) {
80+ try {
81+ val keyStore = KeyStore .getInstance(" JKS" )
82+ FileInputStream (keystorePath).use { fis ->
83+ keyStore.load(fis, " openbmclapi" .toCharArray())
84+ }
6985
70- val env = koinApp.koin.get<ApplicationEnvironment >()
71-
72- // Create and start embedded server with appropriate configuration
73- val server =
74- embeddedServer(
75- Netty ,
76- env,
77- configure = {
78- // Add HTTPS connector if certificates are available, otherwise HTTP
79- if (useHttps && keystorePath != null && File (keystorePath).exists()) {
80- try {
81- val keyStore = KeyStore .getInstance(" JKS" )
82- FileInputStream (keystorePath).use { fis ->
83- keyStore.load(fis, " openbmclapi" .toCharArray())
84- }
85-
86- sslConnector(
87- keyStore = keyStore,
88- keyAlias = " openbmclapi" ,
89- keyStorePassword = { " openbmclapi" .toCharArray() },
90- privateKeyPassword = { " openbmclapi" .toCharArray() },
91- ) {
92- port = config.port
93- host = " 0.0.0.0"
94- }
95- logger.info { " HTTPS connector configured on port ${config.port} " }
96- } catch (e: Exception ) {
97- logger.error(e) {
98- " Failed to configure HTTPS connector, falling back to HTTP"
99- }
100- connector {
101- port = config.port
102- host = " 0.0.0.0"
103- }
104- }
105- } else {
106- // HTTP connector
107- connector {
108- port = config.port
109- host = " 0.0.0.0"
110- }
86+ sslConnector(
87+ keyStore = keyStore,
88+ keyAlias = " openbmclapi" ,
89+ keyStorePassword = { " openbmclapi" .toCharArray() },
90+ privateKeyPassword = { " openbmclapi" .toCharArray() },
91+ ) {
92+ port = config.port
93+ host = " 0.0.0.0"
94+ }
95+ logger.info { " HTTPS connector configured on port ${config.port} " }
96+ } catch (e: Exception ) {
97+ logger.error(e) { " Failed to configure HTTPS connector, falling back to HTTP" }
98+ connector {
99+ port = config.port
100+ host = " 0.0.0.0"
111101 }
112- },
113- Application ::module,
114- )
102+ }
103+ } else {
104+ // HTTP connector
105+ connector {
106+ port = config.port
107+ host = " 0.0.0.0"
108+ }
109+ }
110+ },
111+ Application ::module,
112+ )
115113
116- server.start(wait = true )
114+ server.start(wait = true )
117115}
118116
119117suspend fun Application.module () {
120- configureSerialization()
121- configureMonitoring()
122- configureHTTP()
123- configureRouting()
124-
125- val bootstrapService by inject<BootstrapService >()
126-
127- monitor.subscribe(ServerReady ) { runBlocking { bootstrapService.bootstrap() } }
128-
129- // Register shutdown hook
130- monitor.subscribe(ApplicationStopPreparing ) {
131- runBlocking {
132- try {
133- bootstrapService.shutdown()
134- } catch (e: Exception ) {
135- logger.error(e) { " Shutdown failed" }
136- }
137- }
118+ configureSerialization()
119+ configureMonitoring()
120+ configureHTTP()
121+ configureRouting()
122+
123+ val bootstrapService by inject<BootstrapService >()
124+
125+ monitor.subscribe(ServerReady ) { runBlocking { bootstrapService.bootstrap() } }
126+
127+ // Register shutdown hook
128+ monitor.subscribe(ApplicationStopPreparing ) {
129+ runBlocking {
130+ try {
131+ bootstrapService.shutdown()
132+ } catch (e: Exception ) {
133+ logger.error(e) { " Shutdown failed" }
134+ }
138135 }
136+ }
139137}
0 commit comments