Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cloud Community Day Kochi - Developing Rest API...

Cloud Community Day Kochi - Developing Rest APIs with Ktor and GCP

Developing Rest APIs with Ktor and GCP
Will talk about developing a REST API with Ktor and GCP, and deploying the same in GCP.
Topics Covered
- Why Ktor
- Getting Started with Ktor
- Using DB with GCP
- Selecting proper GCP DB for the App
- Deploying in GCP

Rivu Chakraborty

June 10, 2023
Tweet

More Decks by Rivu Chakraborty

Other Decks in Programming

Transcript

  1. • India’s first GDE (Google Developer Expert) for Kotlin •

    More than decade in the Industry • Android Architect @ Viacom18 • Previously ◦ Byju’s ◦ Paytm ◦ Gojek ◦ Meesho • Author (wrote multiple Kotlin books) • Speaker • Community Person • YouTuber (?) 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] Who am I? 󰞦 Rivu Chakraborty
  2. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] What’s Ktor? 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected]

    A multiplatform toolkit built by JetBrains for creating Web applications in Kotlin. It makes use of coroutines for high scalability and offers an easy-to-use API.
  3. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] What’s Ktor? fun Application.configureRouting() { install(Resources)

    routing { get("/") { call.respondText("Hello World!") } get<Articles> { article -> // Get all articles ... call.respond(article.sort) } } }
  4. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] Why Ktor? • Power of Kotlin

    and Coroutines • Lightweight and Flexible • JVM • Backed and Developed by Jetbrains • Support for Kotlin Multiplatform
  5. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] Application 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] fun

    Application.module() { // Install Ktor plugins configureHTTP()//Caching Headers etc configureSerialization()//Enable Serialisation configureRouting()//Routing configureCORS()//CORS init()//Init DB }
  6. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] Serialization (Content Negotiation) 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty

    @[email protected] fun Application.configureSerialization() { install(ContentNegotiation) { json() } routing { get("/json/kotlinx-serialization") { call.respond(mapOf("hello" to "world")) } } }
  7. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] Enable CORS 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected]

    fun Application.configureCORS() { val allowedOrigins: List<String> = listOf() install(CORS) { allowedOrigins.forEach { allowHost(host = it, schemes = listOf("http", "https")) anyHost() allowHeader(HttpHeaders.ContentType) } } }
  8. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] Setup DB 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected]

    fun createDataSource(DBConfig: DBConfig): DataSource { val config = HikariConfig() with(DBConfig) { config.password = password config.username = user config.jdbcUrl = "jdbc:postgresql:///$name" config.addDataSourceProperty("socketFactory", "com.google.cloud.sql.postgres.SocketFactory") config.addDataSourceProperty("cloudSqlInstance", "rivuchk-dev:asia-south1:rivutalks") config.addDataSourceProperty("ipTypes", "PUBLIC,PRIVATE") } config.validate() return HikariDataSource(config) }
  9. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] Routing 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] fun

    Application.configureRouting() { install(Resources) install(Locations) install(StatusPages) { exception<Throwable> { call, cause -> call.respondText(text = "500: $cause", status = HttpStatusCode.InternalServerError) } } routing { MainRoute() } }
  10. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] Routing 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] @Location("/")

    object Root fun Route.RootApi() { get<Root> { returnResponse(Success( data = RootResponse(), statusCode = HttpStatusCode.OK )) } } @Serializable data class RootResponse ( val message: String = "Welcome" ): BaseResponse(isSuccess = true)
  11. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] Routing 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] @Location("/")

    object Root fun Route.RootApi() { get<Root> { returnResponse(Success( data = RootResponse(), statusCode = HttpStatusCode.OK )) } } @Serializable data class RootResponse ( val message: String = "Welcome" ): BaseResponse(isSuccess = true)
  12. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] App Engine build.gradle.kts - plugins 🌐https://www.rivu.dev/

    youtube.com/@rivutalks @rivuchakraborty @[email protected] id("com.github.johnrengelman.shadow") version "7.1.2" id("com.google.cloud.tools.appengine") version "2.4.2"
  13. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] App Engine

    build.gradle.kts configure<AppEngineAppYamlExtension> { stage { setArtifact("build/libs/${project.name }-all.jar") } deploy { version = "GCLOUD_CONFIG" projectId = "GCLOUD_CONFIG" } }
  14. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] App Engine

    settings.gradle.kt s pluginManagement { repositories { gradlePluginPortal() mavenCentral() } resolutionStrategy { eachPlugin { if (requested.id.id.startsWith("com.googl e.cloud.tools.appengine")) { useModule("com.google.cloud.tools:appe ngine-gradle-plugin:${requested.versio n}") } } }
  15. 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] 🌐https://www.rivu.dev/ youtube.com/@rivutalks @rivuchakraborty @[email protected] • https://ktor.io/

    • https://ktor.io/docs/welcome.html • https://ktor.io/docs/google-app-e ngine.html • https://cloud.google.com/sql/docs /postgres/connect-connectors Resources