Skip to content

Latest commit

Β 

History

75 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

TalkFlow API πŸš€

Java Spring Boot PostgreSQL Keycloak Docker

πŸ“– Overview

TalkFlow is a modern, real-time chat and messaging backend API built with Java and Spring Boot 3.5.14. It provides a robust architecture for handling user synchronization, secure communication via WebSockets, and message state management with file upload capabilities. The application is secured using OAuth2 Resource Server configuration with Keycloak integration.

✨ Key Features

  • Real-time Messaging: Powered by Spring WebSocket for instant bi-directional chat communication.
  • Secure Authentication: OAuth2/JWT authentication using Keycloak with custom JWT converter (KeycloakJwtAuthenticationConverter).
  • User Synchronization: Request filters (UserSynchronizerFilter & UserSynchronizer) for seamless user mapping and database sync.
  • Message Management: Full lifecycle support with message states (SENT, DELIVERED, SEEN) and multiple message types.
  • File Upload & Media: Support for media file uploads (up to 500MB) with configurable storage path.
  • Auditing: Base auditing entity (BaseAuditingEntity) to track creation and modification timestamps automatically.
  • Database Migrations: Schema management using Flyway for reliable database versioning.
  • Environment Configuration: Easy .env file management using spring-dotenv library.
  • Docker Support: Docker Compose setup for quick local development environment (PostgreSQL + Keycloak).

πŸ› οΈ Technology Stack

  • Backend Framework: Java 17, Spring Boot 3.5.14
  • Core Spring Modules: Spring Web, Spring Data JPA, Spring Validation, Spring Security
  • Real-time Communication: Spring WebSocket
  • Authentication & Authorization: Spring OAuth2 Resource Server, Keycloak 26.0.0
  • Database: PostgreSQL 15
  • Database Migrations: Flyway with PostgreSQL dialect
  • Code Generation: Lombok (automatic getters, setters, constructors)
  • Configuration Management: spring-dotenv 4.0.0
  • Containerization: Docker & Docker Compose

πŸ“‚ Project Architecture

api/
β”œβ”€β”€ src/main/java/com/yavuzahmet/talkflow/
β”‚   β”œβ”€β”€ chat/                # Chat management (entities, services, controllers, mappers)
β”‚   β”‚   β”œβ”€β”€ Chat.java                    # Chat entity
β”‚   β”‚   β”œβ”€β”€ ChatRepository.java          # Database access layer
β”‚   β”‚   β”œβ”€β”€ ChatService.java             # Business logic
β”‚   β”‚   β”œβ”€β”€ ChatController.java          # REST endpoints
β”‚   β”‚   β”œβ”€β”€ ChatMapper.java              # DTO mapping
β”‚   β”‚   β”œβ”€β”€ ChatResponse.java            # Response DTO
β”‚   β”‚   └── ChatConstants.java           # Constants
β”‚   β”œβ”€β”€ message/             # Message handling (entities, states, file management)
β”‚   β”‚   β”œβ”€β”€ Message.java                 # Message entity
β”‚   β”‚   β”œβ”€β”€ MessageRequest.java          # Request DTO
β”‚   β”‚   β”œβ”€β”€ MessageResponse.java         # Response DTO
β”‚   β”‚   β”œβ”€β”€ MessageState.java            # Enum (SENT, DELIVERED, SEEN)
β”‚   β”‚   β”œβ”€β”€ MessageType.java             # Enum (TEXT, MEDIA, etc.)
β”‚   β”‚   β”œβ”€β”€ MessageService.java          # Business logic
β”‚   β”‚   β”œβ”€β”€ MessageController.java       # REST endpoints
β”‚   β”‚   β”œβ”€β”€ MessageMapper.java           # DTO mapping
β”‚   β”‚   β”œβ”€β”€ MessageRepository.java       # Database access layer
β”‚   β”‚   β”œβ”€β”€ MessageConstants.java        # Constants
β”‚   β”‚   └── FileService.java             # File upload handling
β”‚   β”œβ”€β”€ user/                # User management
β”‚   β”‚   β”œβ”€β”€ User.java                    # User entity
β”‚   β”‚   β”œβ”€β”€ UserRepository.java          # Database access layer
β”‚   β”‚   β”œβ”€β”€ UserMapper.java              # DTO mapping
β”‚   β”‚   └── UserConstants.java           # Constants
β”‚   β”œβ”€β”€ security/            # OAuth2 & Keycloak configuration
β”‚   β”‚   β”œβ”€β”€ SecurityConfig.java          # Spring Security configuration
β”‚   β”‚   └── KeycloakJwtAuthenticationConverter.java  # JWT token conversion
β”‚   β”œβ”€β”€ interceptor/         # Request/Response filtering
β”‚   β”‚   β”œβ”€β”€ UserSynchronizerFilter.java  # Filter for user sync
β”‚   β”‚   └── UserSynchronizer.java        # User sync logic
β”‚   β”œβ”€β”€ common/              # Shared utilities
β”‚   β”‚   β”œβ”€β”€ BaseAuditingEntity.java      # Base class with audit fields
β”‚   β”‚   └── StringResponse.java          # Generic response wrapper
β”‚   β”œβ”€β”€ file/                # File utilities
β”‚   β”‚   β”œβ”€β”€ FileService.java             # File operations
β”‚   β”‚   └── FileUtils.java               # Utility methods
β”‚   └── TalkflowApiApplication.java      # Main application entry point
β”œβ”€β”€ src/main/resources/
β”‚   └── application.yml                  # Application configuration
β”œβ”€β”€ pom.xml                              # Maven dependencies
└── .mvn/                                # Maven wrapper configuration

docker-compose.yml          # Docker setup (PostgreSQL + Keycloak)
.env-example                # Environment variables template

πŸš€ Getting Started

Prerequisites

  • Java 17 or higher
  • Maven 3.6+
  • Docker & Docker Compose (for local development)
  • PostgreSQL 15 (or use Docker Compose)
  • Keycloak 26.0.0 (or use Docker Compose)

Environment Setup

  1. Clone the repository:

    git clone <repository-url>
    cd TalkFlow
  2. Create .env file from template:

    cp .env-example .env
  3. Configure environment variables (.env):

    # Database Configuration
    DB_USER=talkflow_user
    DB_PASSWORD=your_secure_password
    DB_NAME=talkflow_db
    
    # Keycloak Configuration
    KC_ADMIN=admin
    KC_ADMIN_PASSWORD=admin_password
  4. Start Docker Compose services:

    docker-compose up -d

    This starts:

    • PostgreSQL 15 on localhost:5432
    • Keycloak 26.0.0 on localhost:9090
  5. Build and run the application:

    cd api
    mvn clean install
    mvn spring-boot:run

    The API will be available at http://localhost:8080

πŸ“‘ API Endpoints

Chat Management (/api/v1/chats)

  • POST /api/v1/chats - Create a new chat between two users
    POST /api/v1/chats?sender-id=USER1&receiver-id=USER2
    Response: { "response": "CHAT_ID" }
  • GET /api/v1/chats - Get all chats for the authenticated user
    GET /api/v1/chats
    Authorization: Bearer <JWT_TOKEN>

Message Management (/api/v1/messages)

  • POST /api/v1/messages - Send a text message
    POST /api/v1/messages
    Content-Type: application/json
    {
      "chat-id": "CHAT_ID",
      "content": "Message text",
      "type": "TEXT"
    }
  • POST /api/v1/messages/upload-media - Upload media file
    POST /api/v1/messages/upload-media?chat-id=CHAT_ID
    Content-Type: multipart/form-data
    file: <binary_file> (max 500MB)
    Authorization: Bearer <JWT_TOKEN>
  • PATCH /api/v1/messages - Mark messages as seen
    PATCH /api/v1/messages?chat-id=CHAT_ID
    Authorization: Bearer <JWT_TOKEN>
  • GET /api/v1/messages/chat/{chat-id} - Retrieve chat messages
    GET /api/v1/messages/chat/CHAT_ID

πŸ” Security Features

Authentication & Authorization

  • OAuth2 Resource Server with JWT tokens from Keycloak
  • JWT Token Validation: Tokens are validated against Keycloak's issuer-uri (http://localhost:9090/realms/talkflow)
  • Custom JWT Converter: KeycloakJwtAuthenticationConverter extracts claims and roles from tokens
  • Automatic User Sync: UserSynchronizerFilter intercepts requests and ensures user data is synced to the database

Message Security

  • All message operations require authentication
  • User isolation: Users can only access their own chats and messages
  • File upload size limit: 500MB per file

πŸ“‹ Configuration

Application Configuration (api/src/main/resources/application.yml)

spring:
  datasource:
    url: jdbc:postgresql://localhost:5432/${DB_NAME}
    username: ${DB_USER}
    password: ${DB_PASSWORD}
  jpa:
    hibernate:
      ddl-auto: update  # Auto-update schema (use 'validate' in production)
    show-sql: false
    open-in-view: false
    database: postgresql
  security:
    oauth2:
      resourceserver:
        jwt:
          issuer-uri: http://localhost:9090/realms/talkflow
  servlet:
    multipart:
      max-file-size: 500MB

application:
  file:
    uploads:
      media-output-path: ./upload  # Media storage path

Database Migrations

  • Flyway is configured but currently disabled (flyway.enabled: false)
  • Schema management is handled by Hibernate (ddl-auto: update)
  • To enable Flyway migrations: Set spring.flyway.enabled: true and add migration files to db/migration/

πŸ§ͺ Testing

Run unit tests:

cd api
mvn test

πŸ“ Database Schema

User Table

  • id - UUID primary key
  • username - Unique username from Keycloak
  • email - User email
  • created_at - Creation timestamp (audited)
  • updated_at - Last update timestamp (audited)

Chat Table

  • id - UUID primary key
  • sender_id - Foreign key to User
  • receiver_id - Foreign key to User
  • created_at - Creation timestamp
  • updated_at - Last update timestamp

Message Table

  • id - UUID primary key
  • chat_id - Foreign key to Chat
  • sender_id - Foreign key to User
  • content - Message text
  • type - Message type (TEXT, MEDIA)
  • state - Message state (SENT, DELIVERED, SEEN)
  • file_path - Path to uploaded file (if media)
  • created_at - Creation timestamp
  • updated_at - Last update timestamp

πŸ“¦ Dependencies Management

Maven

All dependencies are specified in api/pom.xml. Key dependencies:

  • spring-boot-starter-web - REST API support
  • spring-boot-starter-data-jpa - Database ORM
  • spring-boot-starter-oauth2-resource-server - OAuth2 security
  • spring-boot-starter-websocket - WebSocket support
  • org.postgresql:postgresql - PostgreSQL driver
  • org.flywaydb:flyway-* - Database migrations
  • org.projectlombok:lombok - Code generation
  • me.paulschwarz:spring-dotenv - .env support

Update Dependencies

mvn dependency:update-check
mvn dependency:tree

πŸ‘¨β€πŸ’» Contributors

  • Ahmet YAVUZ

About

A secure, real-time messaging application built with Spring Boot, Angular 19, Keycloak, and WebSockets.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages