Skip to content

AlphaNick15/chat-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Chat Backend Service 🚀

A Spring Boot REST API service for managing chat rooms and messages. This service handles all chat operations and validates JWT tokens from the auth service.

Features ✨

  • Chat Room Management: Create, retrieve, and delete chat rooms
  • Message Management: Send and retrieve messages within chat rooms
  • JWT Authentication: Validates tokens from the Python auth service
  • H2 In-Memory Database: No external database setup required
  • REST API: Full REST endpoints for chat operations
  • CORS Support: Configured for Angular frontend communication

Technology Stack

  • Java 17
  • Spring Boot 3.1.5
  • Spring Data JPA
  • H2 Database
  • JWT (JJWT)
  • Maven

Prerequisites

  • Java 17 or higher
  • Maven 3.6+

Installation & Setup

  1. Navigate to the project directory:

    cd chat-backend
  2. Build the project:

    mvn clean install
  3. Run the application:

    mvn spring-boot:run

The service will start on http://localhost:8080/api

API Endpoints

Chat Rooms

  • Create Room (requires JWT)

    POST /api/rooms
    Authorization: Bearer <jwt_token>
    Content-Type: application/json
    
    {
      "name": "General",
      "description": "General discussion room"
    }
    
  • Get All Rooms

    GET /api/rooms
    
  • Get Room by ID (requires JWT)

    GET /api/rooms/{id}
    Authorization: Bearer <jwt_token>
    
  • Delete Room (requires JWT)

    DELETE /api/rooms/{id}
    Authorization: Bearer <jwt_token>
    

Messages

  • Send Message (requires JWT)

    POST /api/messages
    Authorization: Bearer <jwt_token>
    Content-Type: application/json
    
    {
      "roomId": 1,
      "content": "Hello everyone!"
    }
    
  • Get Messages by Room

    GET /api/messages?roomId=1
    
  • Get Message by ID (requires JWT)

    GET /api/messages/{id}
    Authorization: Bearer <jwt_token>
    
  • Delete Message (requires JWT)

    DELETE /api/messages/{id}
    Authorization: Bearer <jwt_token>
    

Authentication

  • Verify Token (called by Auth Service)
    GET /api/auth/verify
    Authorization: Bearer <jwt_token>
    

Configuration

Edit src/main/resources/application.yml to customize:

server:
  port: 8080                    # Change port if needed
  servlet:
    context-path: /api          # API base path

jwt:
  secret: your-secret-key       # Change this in production!
  expiration: 86400000          # 24 hours in milliseconds

auth-service:
  url: http://localhost:8000    # Auth service URL

Database

The application uses H2 in-memory database. Access the H2 console at:

http://localhost:8080/api/h2-console

Default credentials:

  • JDBC URL: jdbc:h2:mem:chatdb
  • Username: sa
  • Password: (leave blank)

Inter-Service Communication

This service validates JWT tokens and can verify users through the auth service:

  1. Frontend → Sends JWT in Authorization: Bearer <token> header
  2. Backend → Validates JWT signature using configured secret
  3. Auth Service → Can be called by backend for additional verification

Project Structure

chat-backend/
├── pom.xml
├── src/main/
│   ├── java/com/chatapp/
│   │   ├── ChatBackendApplication.java
│   │   ├── config/
│   │   │   ├── JwtConfig.java
│   │   │   ├── JwtUtil.java
│   │   │   └── CorsConfig.java
│   │   ├── controllers/
│   │   │   ├── ChatRoomController.java
│   │   │   ├── MessageController.java
│   │   │   └── AuthController.java
│   │   ├── services/
│   │   │   ├── ChatRoomService.java
│   │   │   ├── MessageService.java
│   │   │   └── AuthVerificationService.java
│   │   ├── repositories/
│   │   │   ├── ChatRoomRepository.java
│   │   │   └── MessageRepository.java
│   │   └── models/
│   │       ├── ChatRoom.java
│   │       └── Message.java
│   └── resources/
│       └── application.yml
└── README.md

Troubleshooting

Port 8080 already in use?

# Change port in application.yml or run with:
mvn spring-boot:run -Dspring-boot.run.arguments="--server.port=8081"

JWT validation failing?

  • Ensure the JWT secret matches the auth service
  • Check token hasn't expired
  • Verify token format: Bearer <token>

CORS errors?

  • Frontend must be running on http://localhost:4200
  • Adjust CORS settings in CorsConfig.java if needed

Notes

  • All timestamps are stored in UTC
  • Messages are ordered by creation time
  • Room names must be unique
  • User information is extracted from JWT claims

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages