logo
icon

Kotlin Spring Boot Starter

This repository serves as a starter template for building applications with Kotlin and Spring Boot. It provides a basic structure and setup, including database migration with Flyway, Docker configuration, and a sample API for managing user data.

PlatformZeabur
Deployed8
PublisherUranusLin
Deployed8 times
PublisherUranusLin
Created2024-07-08
Services
service icon
Tags
APIStarterDatabase

Kotlin Spring Boot Starter

This repository serves as a starter template for building applications with Kotlin and Spring Boot. It provides a basic structure and setup, including database migration with Flyway, Docker configuration, and a sample API for managing user data.

Features

  • Kotlin and Spring Boot: Leverage the powerful combination of Kotlin and Spring Boot for building robust and maintainable applications.
  • Flyway: Integrated database migration management to handle schema changes effortlessly.
  • PostgreSQL: Pre-configured PostgreSQL database for development and production environments.
  • Docker: Docker and Docker Compose configurations for containerized development and deployment.
  • Gradle: Gradle build system for dependency management and build automation.
  • Sample API: Includes a sample API for user management with basic CRUD operations.
  • Testing: Configured for testing with JUnit 5 and MockMvc.

Getting Started

Prerequisites

Running the Application

  1. Clone the repository:

    git clone https://github.com/UranusLin/kotlin-springboot-starter.git
    cd kotlin-springboot-starter
    
  2. Build the application:

    make build
    
  3. Run the application:

    make run
    
  4. Run with Docker:

    make docker-up
    
  5. ktlintCheck:

    make llint
    
  6. ktlintFormat:

    make format
    
  7. Run Tests:

    make test
    

Project Structure

kotlin-springboot-starter/
├── build.gradle.kts
├── Makefile
├── src/
│   ├── main/
│   │   ├── kotlin/
│   │   │   └── com/
│   │   │       └── starter/
│   │   │           ├── controller/
│   │   │           │   └── UserController.kt
│   │   │           ├── service/
│   │   │           │   └── UserService.kt
│   │   │           ├── repository/
│   │   │           │   └── UserRepository.kt
│   │   │           ├── model/
│   │   │           │   └── User.kt
│   │   │           ├── dto/
│   │   │           │   └── UserDto.kt
│   │   │           ├── config/
│   │   │           │   └── SecurityConfig.kt
│   │   │           └── KotlinSpringbootStarterApplication.kt
│   │   ├── resources/
│   │   │   ├── db/
│   │   │   │   └── migration/
│   │   │   │       └── V1__Initial_schema.sql
│   │   │   ├── static/
│   │   │   ├── templates/
│   │   │   ├── application.yml
│   │   │   └── application-dev.yml
│   │   │── docker/
│   │   │   └── Dockerfile
│   │   │   └── docker-compose.yml
│   └── test/
│       ├── kotlin/
│       │   └── com/
│       │       └── starter/
│       │           └── UserControllerTest.kt
│       └── resources/
│           └── application-test.yml
└── README.md

Configuration

application.yml

spring:
  datasource:
    url: jdbc:postgresql://db:5432/gym_management
    username: your_db_username
    password: your_db_password
    driver-class-name: org.postgresql.Driver
  jpa:
    hibernate:
      ddl-auto: none
    show-sql: true
  flyway:
    enabled: true
    locations: classpath:db/migration
    baseline-on-migrate: true

Docker Compose

docker-compose.yml

version: '3.8'

services:
  app:
    image: kotlin-springboot-starter
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8080:8080"
    depends_on:
      - db
    environment:
      SPRING_DATASOURCE_URL: jdbc:postgresql://db:5432/springboot-starter
      SPRING_DATASOURCE_USERNAME: your_db_username
      SPRING_DATASOURCE_PASSWORD: your_db_password
      SPRING_FLYWAY_ENABLED: true
      SPRING_FLYWAY_LOCATIONS: classpath:db/migration
      SPRING_FLYWAY_BASELINE_ON_MIGRATE: true

  db:
    image: postgres:13
    environment:
      POSTGRES_DB: springboot-starter
      POSTGRES_USER: your_db_username
      POSTGRES_PASSWORD: your_db_password
    ports:
      - "5432:5432"
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:

Database Migration

This project uses Flyway for database migrations. Migration scripts are located in src/main/resources/db/migration.

Applying Migrations

To apply migrations, run:

make migrate

Swagger

Swagger UI is available at http://localhost:8080/swagger-ui/index.html#/

Contributing

Contributions are welcome! Please feel free to submit a PR or open an issue.

License

This project is licensed under the MIT License.

TODO

  • [x] Add Swagger
  • [ ] Add JWT
  • [ ] Add Logging
  • [ ] Add environment variables
  • [ ] Add Exception Handling
  • [ ] Add Integration Tests
  • [ ] Add CI/CD Pipeline
  • [ ] Add more features