Kotlin Compiler Server
The Kotlin Compiler Server is an official service provided by JetBrains aimed at facilitating the compilation and execution of Kotlin code through a REST API. It mainly powers the Kotlin Playground library, making it easy for developers to test their Kotlin code snippets easily and efficiently.
Getting Started :checkered_flag:
Simple Spring Boot Application
To kick-start the Kotlin Compiler Server using a simple Spring Boot application setup, users need to download all necessary Kotlin dependencies and build the executor. This is achieved with the Gradle command as follows:
$ ./gradlew build -x test
Once built, the server can be started, with its main class being com.compiler.server.CompilerApplication
.
Using Docker
For those preferring containerized solutions, the Kotlin Compiler Server can also be run inside a Docker container. This allows for a more isolated and portable environment. Building the app inside Docker is straightforward with the command:
$ ./docker-image-build.sh
This will package the application within a Docker image, ready for execution.
Amazon Lambda
The server can be deployed within Amazon's serverless architecture using AWS Lambda. This option is ideal for creating scalable and cost-effective serverless applications. The application builds with a command:
$ ./gradlew buildLambda
The output will be a .zip
file located under build/distributions
. The Lambda handler used is com.compiler.server.lambdas.StreamLambdaHandler::handleRequest
, and instructions on publishing it can be found in the AWS Lambda documentation.
From Kotless
An alternative to using AWS serverless containers is integrating with Kotless. This requires removing any AWS serverless container dependencies and configuring Kotless appropriately.
API Documentation :page_with_curl:
Kotlin Compiler Server provides API documentation accessible through Swagger. Developers can explore it at http://localhost:8080/swagger-ui/
.
Adding Dependencies
Adding new dependencies to the Kotlin Compiler can be done via the build.gradle.kts
file. A special task called kotlinDependency
is used for this purpose:
kotlinDependency "your dependency"
If additional libraries require operations sensitive to security, the executor.policy
file must be configured to ensure safe execution.
CORS Configuration
The server's CORS (Cross-Origin Resource Sharing) settings can be managed through environment variables, ensuring secure interactions with external domains. Default values are provided, but they can be adjusted according to specific needs.
Logging
Logging is optimized for production with a Spring Active profile that outputs logs in a JSON format. This profile can be set using environment variables or command line arguments, ensuring smooth monitoring and debugging processes, especially important in case of unsuccessful execution attempts which generate detailed log entries.
Kotlin Release Guide :rocket:
Updating and releasing new versions of Kotlin with the server is procedural. The Kotlin version in libs.versions.toml
needs updating followed by building the project using Gradle to ensure compatibility and functionality:
$ ./gradlew build
Branches named using version patterns can be saved optionally.
Gradle Build Scans
Insights into a build are available via Gradle Build Scans, providing useful analytics about the build process. Users can enable these scans by configuring their Gradle properties appropriately and provisioning an access key with:
./gradlew provisionDevelocityAccessKey
This feature aids in optimizing build processes and assessing performance metrics.
In conclusion, the Kotlin Compiler Server stands as a powerful tool for any Kotlin developer, enhancing the development experience through easy integration, robust functionality, and flexible deployment options.