Skip to content

catalystbyzoho/example-kotlin-docker

Repository files navigation

Kotlin REST API Application

A modern, containerized Kotlin web application built with Ktor framework, featuring three REST API endpoints and a beautiful responsive HTML UI for interacting with the APIs.

🚀 Features

  • Three REST API Endpoints:

    • GET /api/greeting - Returns a personalized greeting with timestamp
    • POST /api/calculate - Performs arithmetic operations (add, subtract, multiply, divide)
    • GET /api/time - Returns current server time and timezone information
  • Responsive Web UI:

    • Modern, gradient-based design with glassmorphism effects
    • Fully responsive (mobile and desktop)
    • Interactive forms to test each API endpoint
    • Real-time result display with smooth animations
  • Containerized:

    • Docker support for easy deployment
    • Multi-stage build for optimized image size
    • Platform-specific build support (linux/amd64)

🛠️ Technology Stack

  • Language: Kotlin 1.9.21
  • Framework: Ktor 2.3.7
  • Build Tool: Gradle 8.5
  • Runtime: OpenJDK 17
  • Server: Netty (embedded)

📋 API Endpoints

1. Greeting API

GET /api/greeting?name=YourName

Response:

{
  "message": "Hello, YourName! Welcome to Kotlin REST API.",
  "timestamp": "2025-12-19T16:50:01+05:30"
}

2. Calculator API

POST /api/calculate
Content-Type: application/json

{
  "operation": "add",
  "a": 10,
  "b": 5
}

Response:

{
  "result": 15.0,
  "operation": "add",
  "expression": "10.0 + 5.0 = 15.0"
}

Supported operations: add, subtract, multiply, divide

3. Server Time API

GET /api/time

Response:

{
  "currentTime": "2025-12-19 16:50:01",
  "timezone": "Asia/Kolkata",
  "epochMillis": 1734609601000
}

🐳 Docker Instructions

Building the Docker Image

Build the Docker image for linux/amd64 platform:

docker build --platform=linux/amd64 -t kotlin-docker-app .

This will:

  1. Use Gradle to build the application
  2. Create a fat JAR with all dependencies
  3. Package it in a lightweight OpenJDK 17 runtime image

Running the Container

Run the container and map port 9000:

docker run -p 9000:9000 kotlin-docker-app

For running in detached mode:

docker run -d -p 9000:9000 --name kotlin-api kotlin-docker-app

Accessing the Application

Once the container is running, access the application at:

💻 Local Development

Prerequisites

  • JDK 17 or higher
  • Gradle 8.5 or higher (or use the Gradle wrapper)

Running Locally

  1. Build the application:
./gradlew build
  1. Run the application:
./gradlew run

The application will start on port 9000.

Testing the APIs

Using curl:

# Test greeting endpoint
curl "http://localhost:9000/api/greeting?name=Developer"

# Test calculator endpoint
curl -X POST http://localhost:9000/api/calculate \
  -H "Content-Type: application/json" \
  -d '{"operation":"multiply","a":7,"b":6}'

# Test time endpoint
curl http://localhost:9000/api/time

📱 UI Features

The web interface provides:

  • Beautiful gradient background with purple-to-violet theme
  • Glassmorphism cards for each API endpoint
  • Interactive forms with real-time validation
  • Smooth animations and hover effects
  • Responsive design that works on all screen sizes
  • JSON response display with proper formatting

🔧 Project Structure

kotlin-docker/
├── src/
│   └── main/
│       └── kotlin/
│           └── com/
│               └── example/
│                   └── Application.kt
├── build.gradle.kts
├── settings.gradle.kts
├── Dockerfile
└── README.md

📝 License

This project is open source and available for educational and commercial use.

About

Demo kotlin app deployed on Catalyst Appsail

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published