|
| 1 | +package {{packageName}} |
| 2 | + |
| 3 | +import io.ktor.server.application.* |
| 4 | +import io.ktor.serialization.gson.* |
| 5 | +import io.ktor.http.* |
| 6 | +{{#featureResources}} |
| 7 | +import io.ktor.server.resources.* |
| 8 | +{{/featureResources}} |
| 9 | +{{#featureCORS}} |
| 10 | +import io.ktor.server.plugins.cors.routing.* |
| 11 | +{{/featureCORS}} |
| 12 | +{{#featureAutoHead}} |
| 13 | +import io.ktor.server.plugins.autohead.* |
| 14 | +{{/featureAutoHead}} |
| 15 | +{{#featureConditionalHeaders}} |
| 16 | +import io.ktor.server.plugins.conditionalheaders.* |
| 17 | +{{/featureConditionalHeaders}} |
| 18 | +{{#featureCompression}} |
| 19 | +import io.ktor.server.plugins.compression.* |
| 20 | +{{/featureCompression}} |
| 21 | +import io.ktor.server.plugins.contentnegotiation.* |
| 22 | +import io.ktor.server.plugins.defaultheaders.* |
| 23 | +{{#featureHSTS}} |
| 24 | +import io.ktor.server.plugins.hsts.* |
| 25 | +{{/featureHSTS}} |
| 26 | +{{#featureMetrics}} |
| 27 | +import com.codahale.metrics.Slf4jReporter |
| 28 | +import io.ktor.server.metrics.dropwizard.* |
| 29 | +import java.util.concurrent.TimeUnit |
| 30 | +{{/featureMetrics}} |
| 31 | +import io.ktor.server.routing.* |
| 32 | +{{#hasAuthMethods}} |
| 33 | +import com.typesafe.config.ConfigFactory |
| 34 | +import io.ktor.client.HttpClient |
| 35 | +import io.ktor.client.engine.apache.Apache |
| 36 | +import io.ktor.server.config.HoconApplicationConfig |
| 37 | +import io.ktor.server.auth.* |
| 38 | +import org.openapitools.server.infrastructure.* |
| 39 | +{{/hasAuthMethods}} |
| 40 | +{{#generateApis}}{{#apiInfo}}{{#apis}}import {{apiPackage}}.{{classname}} |
| 41 | +{{/apis}}{{/apiInfo}}{{/generateApis}} |
| 42 | + |
| 43 | +{{#hasAuthMethods}} |
| 44 | +internal val settings = HoconApplicationConfig(ConfigFactory.defaultApplication(HTTP::class.java.classLoader)) |
| 45 | + |
| 46 | +object HTTP { |
| 47 | + val client = HttpClient(Apache) |
| 48 | +} |
| 49 | +{{/hasAuthMethods}} |
| 50 | + |
| 51 | +fun Application.main() { |
| 52 | + install(DefaultHeaders) |
| 53 | + {{#featureMetrics}} |
| 54 | + install(DropwizardMetrics) { |
| 55 | + val reporter = Slf4jReporter.forRegistry(registry) |
| 56 | + |
| 57 | + .convertRatesTo(TimeUnit.SECONDS) |
| 58 | + .convertDurationsTo(TimeUnit.MILLISECONDS) |
| 59 | + .build() |
| 60 | + reporter.start(10, TimeUnit.SECONDS) |
| 61 | + } |
| 62 | + {{/featureMetrics}} |
| 63 | +{{#generateApis}} |
| 64 | + install(ContentNegotiation) { |
| 65 | + register(ContentType.Application.Json, GsonConverter()) |
| 66 | + } |
| 67 | + {{#featureAutoHead}} |
| 68 | + install(AutoHeadResponse) // see https://ktor.io/docs/autoheadresponse.html |
| 69 | + {{/featureAutoHead}} |
| 70 | + {{#featureConditionalHeaders}} |
| 71 | + install(ConditionalHeaders) // see https://ktor.io/docs/conditional-headers.html |
| 72 | + {{/featureConditionalHeaders}} |
| 73 | + {{#featureCompression}} |
| 74 | + install(Compression, ApplicationCompressionConfiguration()) // see https://ktor.io/docs/compression.html |
| 75 | + {{/featureCompression}} |
| 76 | + {{#featureCORS}} |
| 77 | + install(CORS, ApplicationCORSConfiguration()) // see https://ktor.io/docs/cors.html |
| 78 | + {{/featureCORS}} |
| 79 | + {{#featureHSTS}} |
| 80 | + install(HSTS, ApplicationHstsConfiguration()) // see https://ktor.io/docs/hsts.html |
| 81 | + {{/featureHSTS}} |
| 82 | + {{#featureResources}} |
| 83 | + install(Resources) |
| 84 | + {{/featureResources}} |
| 85 | + {{#hasAuthMethods}} |
| 86 | + install(Authentication) { |
| 87 | + {{#authMethods}} |
| 88 | + {{#isBasicBasic}} |
| 89 | + basic("{{{name}}}") { |
| 90 | + validate { credentials -> |
| 91 | + // TODO: "Apply your basic authentication functionality." |
| 92 | + // Accessible in-method via call.principal<UserIdPrincipal>() |
| 93 | + if (credentials.name == "Swagger" && "Codegen" == credentials.password) { |
| 94 | + UserIdPrincipal(credentials.name) |
| 95 | + } else { |
| 96 | + null |
| 97 | + } |
| 98 | + } |
| 99 | + {{/isBasicBasic}} |
| 100 | + {{#isApiKey}} |
| 101 | + // "Implement API key auth ({{{name}}}) for parameter name '{{{keyParamName}}}'." |
| 102 | + apiKeyAuth("{{{name}}}") { |
| 103 | + validate { apikeyCredential: ApiKeyCredential -> |
| 104 | + when { |
| 105 | + apikeyCredential.value == "keyboardcat" -> ApiPrincipal(apikeyCredential) |
| 106 | + else -> null |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + {{/isApiKey}} |
| 111 | + {{#isOAuth}} |
| 112 | + {{#bodyAllowed}} |
| 113 | + {{/bodyAllowed}} |
| 114 | + {{^bodyAllowed}} |
| 115 | + oauth("{{name}}") { |
| 116 | + client = HttpClient(Apache) |
| 117 | + providerLookup = { applicationAuthProvider([email protected]) } |
| 118 | + urlProvider = { _ -> |
| 119 | + // TODO: define a callback url here. |
| 120 | + "/" |
| 121 | + } |
| 122 | + } |
| 123 | + {{/bodyAllowed}} |
| 124 | + {{/isOAuth}} |
| 125 | + {{/authMethods}} |
| 126 | + } |
| 127 | + {{/hasAuthMethods}} |
| 128 | + install(Routing) { |
| 129 | + {{#apiInfo}} |
| 130 | + {{#apis}} |
| 131 | + {{#operations}} |
| 132 | + {{classname}}() |
| 133 | + {{/operations}} |
| 134 | + {{/apis}} |
| 135 | + {{/apiInfo}} |
| 136 | + } |
| 137 | + |
| 138 | +{{/generateApis}} |
| 139 | +} |
0 commit comments