A Ktor-based REST API backend application with PostgreSQL database integration, JWT authentication, and Docker containerization.
- Kotlin
- Ktor 3.0.0
- PostgreSQL 15
- Exposed ORM
- HikariCP connection pooling
- JWT authentication
- Docker and Docker Compose
- Gradle build system
src/main/kotlin/
├── Application.kt # Application entry point and module configuration
├── Databases.kt # Database connection configuration using HikariCP
├── Security.kt # JWT authentication setup
├── Routing.kt # API routes and status page configuration
├── Monitoring.kt # Request logging configuration
├── Serialization.kt # JSON serialization setup
├── UsersSchema.kt # User entity and CRUD operations using Exposed ORM
└── CitySchema.kt # City entity and CRUD operations using JDBC
- PostgreSQL database integration with HikariCP connection pooling
- Exposed ORM for database operations
- JWT-based authentication
- Request/response logging
- Exception handling with status pages
- JSON content negotiation with kotlinx.serialization
- Docker containerization for database
- pgAdmin for database management
The application uses PostgreSQL running in a Docker container with the following configuration:
- Host: localhost
- Port: 5433
- Database: nivi_db
- Username: myuser
- Password: mypassword
Database connection is managed through HikariCP with the following settings:
- Maximum pool size: 3
- Transaction isolation: REPEATABLE_READ
- Auto-commit: disabled
- JDK 11 or higher
- Docker and Docker Compose
- Gradle (included via wrapper)
-
Clone the repository
-
Start the PostgreSQL database using Docker Compose:
docker-compose up -dThis will start:
- PostgreSQL database on port 5433
- pgAdmin web interface on port 5050
- Access pgAdmin (optional):
- URL: http://localhost:5050
- Email: [email protected]
- Password: admin
- Build the application:
./gradlew build- Run the application:
./gradlew runThe server will start on http://localhost:8080
| Task | Description |
|---|---|
./gradlew build |
Build the application |
./gradlew test |
Run tests |
./gradlew run |
Run the application |
./gradlew buildFatJar |
Build executable JAR with all dependencies |
- id: Integer (auto-increment primary key)
- name: Varchar(50)
- age: Integer
Uses Exposed ORM for CRUD operations.
- id: Serial (auto-increment primary key)
- name: Varchar(255)
- population: Integer
Uses JDBC for CRUD operations.
GET /
Response: "Hello guys kaise ho aplog!"
GET /json/kotlinx-serialization
Response: {"hello": "world"}
The application includes JWT authentication configuration with the following default settings:
- Algorithm: HMAC256
- Audience: jwt-audience
- Domain: https://jwt-provider-domain/
- Realm: ktor sample app
Contains application configuration including:
- Server port (8080)
- Database connection settings
- JWT configuration
Defines services for:
- PostgreSQL database
- pgAdmin interface
The application includes global exception handling that returns 500 status codes with error details for unhandled exceptions.
Request logging is enabled for all endpoints using Ktor's CallLogging plugin with INFO level.
- The application uses Ktor's EngineMain for server initialization
- Database tables are automatically created on application startup
- All database operations are executed asynchronously using coroutines
- Connection pooling ensures efficient database resource management
If you encounter database connection errors:
- Ensure Docker containers are running:
docker ps- Check container logs:
docker logs nivi_db-
Verify database connection settings in application.yaml match docker-compose.yml
-
Ensure port 5433 is not in use by another application
This project was created using the Ktor Project Generator.