|
1 | 1 | package org.openfolder.kotlinasyncapi.springweb |
2 | 2 |
|
| 3 | +import org.openfolder.kotlinasyncapi.model.AsyncApi |
3 | 4 | import org.openfolder.kotlinasyncapi.springweb.context.DefaultInfoProvider |
| 5 | +import org.openfolder.kotlinasyncapi.springweb.context.DefaultResourceProvider |
| 6 | +import org.openfolder.kotlinasyncapi.springweb.context.ResourceProvider |
4 | 7 | import org.openfolder.kotlinasyncapi.springweb.controller.AsyncApiController |
5 | 8 | import org.openfolder.kotlinasyncapi.springweb.service.AsyncApiExtension |
6 | 9 | import org.openfolder.kotlinasyncapi.springweb.service.AsyncApiSerializer |
7 | 10 | import org.openfolder.kotlinasyncapi.springweb.service.AsyncApiService |
8 | 11 | import org.openfolder.kotlinasyncapi.springweb.service.DefaultAsyncApiSerializer |
9 | 12 | import org.openfolder.kotlinasyncapi.springweb.service.DefaultAsyncApiService |
10 | 13 | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean |
| 14 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass |
11 | 15 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean |
| 16 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty |
12 | 17 | import org.springframework.boot.context.properties.ConfigurationProperties |
13 | 18 | import org.springframework.context.ApplicationContext |
14 | 19 | import org.springframework.context.annotation.Bean |
15 | 20 | import org.springframework.context.annotation.Configuration |
16 | 21 | import org.springframework.context.annotation.Import |
| 22 | +import kotlin.script.experimental.host.toScriptSource |
| 23 | +import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost |
17 | 24 |
|
18 | 25 | @Configuration |
19 | 26 | @ConditionalOnBean(AsyncApiMarkerConfiguration.Marker::class) |
@@ -57,3 +64,43 @@ internal open class AsyncApiAutoConfiguration { |
57 | 64 | open fun asyncApiController(service: AsyncApiService, serializer: AsyncApiSerializer): AsyncApiController = |
58 | 65 | AsyncApiController(service, serializer) |
59 | 66 | } |
| 67 | + |
| 68 | +@Configuration |
| 69 | +@ConditionalOnProperty(name = ["asyncapi.script.enabled"], havingValue = "true", matchIfMissing = true) |
| 70 | +@Import(AsyncApiEmbeddedScriptAutoConfiguration::class) |
| 71 | +internal open class AsyncApiScriptAutoConfiguration { |
| 72 | + |
| 73 | + @Bean |
| 74 | + open fun resourceProvider(context: ApplicationContext) = |
| 75 | + DefaultResourceProvider(context) |
| 76 | + |
| 77 | + @Bean |
| 78 | + open fun asyncApiResourceExtension(resourceProvider: ResourceProvider, asyncApiProperties: AsyncApiProperties) = |
| 79 | + resourceProvider.resource(asyncApiProperties.script.resourcePath, AsyncApi::class)?.let { |
| 80 | + AsyncApiExtension.from(resource = it) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +@Configuration |
| 85 | +@ConditionalOnClass(BasicJvmScriptingHost::class) |
| 86 | +internal open class AsyncApiEmbeddedScriptAutoConfiguration { |
| 87 | + |
| 88 | + @Bean |
| 89 | + open fun asyncApiScriptExtension(resourceProvider: ResourceProvider, asyncApiProperties: AsyncApiProperties) = |
| 90 | + resourceProvider.resource(asyncApiProperties.script.sourcePath)?.let { |
| 91 | + AsyncApiExtension.from( |
| 92 | + script = it.inputStream.bufferedReader().use { it.readText() }.toScriptSource() |
| 93 | + ) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +@Configuration |
| 98 | +internal open class AsyncApiMarkerConfiguration { |
| 99 | + |
| 100 | + @Bean |
| 101 | + open fun asyncApiMarkerBean(): Marker { |
| 102 | + return Marker() |
| 103 | + } |
| 104 | + |
| 105 | + class Marker |
| 106 | +} |
0 commit comments