Simple backend
A backend for DivKit can be deployed using the Yandex Cloud Functions service. This requires a Yandex Cloud account.
Creating a function in Kotlin
Getting started with Yandex Cloud is described in detail in the instructions.
First, you need to create the function itself:
- In the management console, go to the directory where you want to create the function.
- Select the Cloud Functions service.
- Click the Create function button.
- Enter a function name.
- Click the Create button.
- Select the Kotlin runtime environment.
- Click the Continue button.
The function editor will appear on the screen where you need to do the following:
- Select the Code editor option for defining the function.
- Click the Create file button.
- Name it
Handler.kt. - To the right of the file list, paste this code:
package cloud.divkit
data class Request(
val httpMethod: String?,
val headers: Map<String, String> = mapOf(),
val body: String = ""
)
data class Response(
val statusCode: Int,
val body: String
)
fun handle(request: Request): Response {
return Response(200, card)
}
private const val card = """
{
"log_id": "cloud_divkit",
"states": [
{
"state_id": 0,
"div": {
"type": "text",
"text": "Hello, Username!"
}
}
]
}
"""
- Specify the entry point
cloud.divkit.Handler::handle. Entry point consists of the package name, handler file name, and handler method. Additional information can be found in this guide. - Click the Save changes button.
Using Kotlin DSL
Kotlin DSL provides a way to describe DivKit layout directly in the Kotlin application code. To use it, you need to connect an external dependency.
Build a small project:
- Create a
build.gradle.ktsfile. - Specify the following code in it:
plugins {
kotlin("jvm") version "2.0.0"
}
repositories {
mavenCentral()
}
dependencies {
implementation("com.fasterxml.jackson.core:jackson-annotations:2.18.3")
implementation("com.fasterxml.jackson.core:jackson-databind:2.18.3")
implementation("com.yandex.div:kotlin-json-builder:31.0.0")
}
- Create a handler file
src/main/kotlin/cloud/divkit/Handler.ktsimilar to the one created in the simple function section. - Modify the
handle(request: Request)method as follows:
fun handle(request: Request): Response {
val card = divan {
data(
logId = "greeting",
div = text("Hello, Username!")
)
}
return Response(200, card.toJson())
}
- Package these files into a zip-archive.
The complete example is available at this link.
Now you can create a function based on it:
- Create a new Cloud function the same way as in the previous section.
- In the editor, select the ZIP archive option.
- Click the Attach file button.
- Specify the path to the created zip-archive.
- Set the entry point to
cloud.divkit.Handler::handle. - Click the Save changes button.
Displaying the Result
You can check the response of the created function in the DivKit demo application:
- Select the desired function from the list.
- Copy the link specified in the Link to invoke section.
- Open the demo application.
- Go to Demo (Android) or Playground (iOS) section.
- Paste the link.
- Click the Show the result button.
It's worth noting that by default all functions are private, and a corresponding token is required to access them. For the demo application to be able to display the layout, you need to make the function public.
Useful Links
- Getting Started with Yandex Cloud
- Guide to Creating Kotlin Functions
- Simple Cloud Function with DivKit Layout
- Cloud Function Using Kotlin DSL
Learn more
You can discuss topics of interest in the DivKit user community in Telegram: https://t.me/divkit_community_en.