Skip to content

Instantly share code, notes, and snippets.

@a-dminator
Created February 11, 2018 08:24
Show Gist options
  • Save a-dminator/45e23f6c8975b30c716eabf22b7800c6 to your computer and use it in GitHub Desktop.
Save a-dminator/45e23f6c8975b30c716eabf22b7800c6 to your computer and use it in GitHub Desktop.
import de.nielsfalk.playground.ktor.swagger.*
import entity.Magazine
import entity.Publisher
import entity.aggregate.ApplicationSettingsResponse
import exceptions.installExceptions
import io.ktor.application.Application
import io.ktor.application.call
import io.ktor.application.install
import io.ktor.content.resources
import io.ktor.content.static
import io.ktor.features.CallLogging
import io.ktor.features.Compression
import io.ktor.features.ContentNegotiation
import io.ktor.features.DefaultHeaders
import io.ktor.gson.GsonConverter
import io.ktor.http.ContentType
import io.ktor.locations.Location
import io.ktor.locations.Locations
import io.ktor.routing.routing
@Group("main")
@Location("application-settings")
class ApplicationSettings(val appId: Long)
fun Application.main() {
install(CallLogging)
install(Locations)
install(DefaultHeaders)
install(Compression)
install(ContentNegotiation) {
register(ContentType.Application.Json, GsonConverter())
}
install(SwaggerSupport) {
forwardRoot = true
swagger.info = Information(
version = "0.1",
title = "Publisher API",
description = "Sample of Publisher API on KTOR",
contact = Contact(
name = "Sam",
url = "https://nielsfalk.de"
)
)
}
installExceptions()
routing {
static {
resources("public")
}
get<ApplicationSettings>(
responds(ok<ApplicationSettingsResponse>())
) { params ->
call.respondJson(
response = ApplicationSettingsResponse(
appId = params.appId,
isRegistrationEnabled = true,
publisher = Publisher(
id = 1L,
title = "publisher"
),
magazines = listOf(
Magazine(
id = 1L,
publisherId = 1L,
title = "magazine 1"
),
Magazine(
id = 2L,
publisherId = 1L,
title = "magazine 2"
)
)
)
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment