# wishall05/patient-management architecture

> Architecture diagram of the GitHub repository wishall05/patient-management, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/wishall05/patient-management
- Repository: https://github.com/wishall05/patient-management

## Overview

#### Purpose and topology
A Java 21/Spring Boot microservices system for patient registration. Patient creation synchronously provisions billing through gRPC, then asynchronously publishes a protobuf patient event for analytics. LocalStack emulates AWS deployment infrastructure; PostgreSQL is the patient data store.

#### Entrypoints and service boundaries
Each deployable service has a Spring Boot entrypoint and Docker image:
- Patient domain/API: `patient-service/src/main/java/com/pm/PatientServiceApplication.java`, `patient-service/Dockerfile`
- Authentication: `auth-service/src/main/java/com/pm/AuthServiceApplication.java`, `auth-service/Dockerfile`
- Edge routing: `api-gateway/src/main/java/com/pm/ApiGatewayServiceApplication.java`, `api-gateway/Dockerfile`
- Billing gRPC server: `billing-service/src/main/java/com/pm/BillingServiceApplication.java`, `billing-service/Dockerfile`
- Event analytics consumer: `analytics-service/src/main/java/com/pm/AnalyticsServiceApplication.java`, `analytics-service/Dockerfile`

#### Gateway and authentication
The API gateway is the external HTTP boundary and applies JWT validation before forwarding routed traffic. Its filter and production/application routing configuration are in `api-gateway/src/main/java/com/pm/filter/JwtValidationGatewayFilterFactory.java`, `api-gateway/src/main/resources/application.yml`, and `api-gateway/src/main/resources/application-prod.yml`.

Auth owns users, login flow, credential security, and JWT issuance. Its layered boundary is controller → service → repository/entity: `auth-service/src/main/java/com/pm/controller/AuthController.java`, `auth-service/src/main/java/com/pm/service/AuthService.java`, `auth-service/src/main/java/com/pm/util/JwtUtil.java`.

#### Patient workflow and persistence
Patient Service owns registration, validation, CRUD-facing behavior, and patient persistence. HTTP handling enters `patient-service/src/main/java/com/pm/controller/PatientController.java`, passes through `patient-service/src/main/java/com/pm/service/PatientService.java`, and persists `patient-service/src/main/java/com/pm/model/Patient.java` through `patient-service/src/main/java/com/pm/repository/PatientRepository.java`.

The request/response DTO and mapper layer separates transport contracts from the persistence model: `patient-service/src/main/java/com/pm/dto/PatientRequestDTO.java`, `patient-service/src/main/java/com/pm/mapper/PatientMapper.java`. PostgreSQL is the stated backing store; seed data is present in `patient-service/src/main/resources/data.sql`.

#### Synchronous billing integration
During registration, Patient Service invokes Billing Service over gRPC through `patient-service/src/main/java/com/pm/grpc/BillingServiceGrpcClient.java`. The shared wire contract is duplicated locally in `patient-service/src/main/proto/billing_service.proto` and `billing-service/src/main/proto/billing_service.proto`; Billing implements the server side in `billing-service/src/main/java/com/pm/grpc/BillingGrpcService.java`. This is a synchronous cross-service dependency in the patient-create path.

#### Asynchronous event and analytics flow
After successful patient creation, Patient Service publishes protobuf events via `patient-service/src/main/java/com/pm/kafka/KafkaProducer.java`, using `patient-service/src/main/proto/patient_event.proto`. Kafka is the decoupling boundary; Analytics independently consumes the patient topic through `analytics-service/src/main/java/com/pm/kafka/KafkaConsumer.java` and uses its corresponding event schema at `analytics-service/src/main/proto/patient_event.proto`.

#### Infrastructure and runtime
Maven builds each independently deployable module; Protocol Buffers/gRPC Java sources are generated at build time. Docker packages services. AWS CDK defines the LocalStack-targeted VPC, MSK, IAM, logs, ECS tasks, and gateway deployment in `infrastructure/src/main/java/com/pm/stack/LocalStack.java`, invoked by `infrastructure/localstack-deploy.sh`. Kafka UI is separately runnable through `kafka-ui/docker-compose.yml`.

Main entry points: [`infrastructure/src/main/java/com/pm/stack/LocalStack.java`](https://github.com/wishall05/patient-management/blob/main/infrastructure/src/main/java/com/pm/stack/LocalStack.java) (LocalStack CDK stack), [`patient-service/src/main/java/com/pm/service/PatientService.java`](https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/java/com/pm/service/PatientService.java) (Patient service), [`api-gateway/src/main/java/com/pm/filter/JwtValidationGatewayFilterFactory.java`](https://github.com/wishall05/patient-management/blob/main/api-gateway/src/main/java/com/pm/filter/JwtValidationGatewayFilterFactory.java) (JWT gateway filter), [`patient-service/src/main/java/com/pm/kafka/KafkaProducer.java`](https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/java/com/pm/kafka/KafkaProducer.java) (Patient event producer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_edge["Edge &amp; Identity"]
  node_gateway["API Gateway<br/>Spring Boot edge service"]
  node_jwt_filter["JWT gateway filter"]
  node_auth["Auth Service<br/>Spring Boot service"]
  node_auth_controller["Auth controller<br/>HTTP controller"]
  node_auth_service["Auth service &amp; JWT<br/>domain service<br/>[AuthService.java]"]
end

subgraph group_patient["Patient Domain"]
  node_patient_app["Patient Service<br/>Spring Boot service"]
  node_patient_controller["Patient controller<br/>HTTP controller"]
  node_patient_service["Patient service<br/>domain service"]
  node_patient_contract["Patient DTO &amp; mapper<br/>transport mapping"]
  node_patient_repository["Patient repository<br/>persistence repository"]
  node_postgres[("PostgreSQL<br/>patient datastore<br/>[data.sql]")]
end

subgraph group_integrations["Billing &amp; Events"]
  node_billing_client["Billing gRPC client"]
  node_billing_contract["Billing protobuf contract<br/>gRPC schema"]
  node_billing["Billing Service<br/>Spring Boot gRPC service"]
  node_billing_server["Billing gRPC server<br/>gRPC handler"]
  node_event_producer["Patient event producer<br/>Kafka producer<br/>[KafkaProducer.java]"]
  node_patient_event_schema["Patient event schema<br/>protobuf schema"]
  node_kafka["Kafka / MSK<br/>event broker"]
  node_analytics["Analytics Service<br/>Spring Boot consumer service"]
  node_analytics_consumer["Analytics consumer<br/>Kafka consumer<br/>[KafkaConsumer.java]"]
end

subgraph group_runtime["Deployment Runtime"]
  node_localstack_stack{{"LocalStack CDK stack<br/>AWS infrastructure definition<br/>[LocalStack.java]"}}
  node_deployment_script["LocalStack deploy script<br/>deployment script"]
end

node_gateway -->|"applies"| node_jwt_filter
node_jwt_filter -->|"auth routes"| node_auth
node_jwt_filter -->|"forwards validated requests"| node_patient_app
node_auth -->|"serves"| node_auth_controller
node_auth_controller -->|"delegates"| node_auth_service
node_patient_app -->|"serves"| node_patient_controller
node_patient_controller -->|"maps request/response"| node_patient_contract
node_patient_contract -->|"invokes"| node_patient_service
node_patient_service -->|"persists through"| node_patient_repository
node_patient_repository -->|"stores patients"| node_postgres
node_patient_service -->|"provisions billing"| node_billing_client
node_billing_client -->|"uses"| node_billing_contract
node_billing_contract -->|"defines RPC"| node_billing_server
node_billing -->|"hosts"| node_billing_server
node_patient_service -->|"publishes after creation"| node_event_producer
node_event_producer -->|"serializes"| node_patient_event_schema
node_event_producer -->|"produces patient event"| node_kafka
node_kafka -->|"delivers patient topic"| node_analytics_consumer
node_analytics -->|"hosts"| node_analytics_consumer
node_deployment_script -->|"invokes"| node_localstack_stack
node_localstack_stack -.->|"deploys"| node_gateway
node_localstack_stack -.->|"provisions MSK"| node_kafka
node_localstack_stack -.->|"deploys ECS task"| node_patient_app
node_localstack_stack -.->|"deploys ECS task"| node_auth
node_localstack_stack -.->|"deploys ECS task"| node_billing
node_localstack_stack -.->|"deploys ECS task"| node_analytics

click node_gateway "https://github.com/wishall05/patient-management/blob/main/api-gateway/src/main/java/com/pm/ApiGatewayServiceApplication.java"
click node_jwt_filter "https://github.com/wishall05/patient-management/blob/main/api-gateway/src/main/java/com/pm/filter/JwtValidationGatewayFilterFactory.java"
click node_auth "https://github.com/wishall05/patient-management/blob/main/auth-service/src/main/java/com/pm/AuthServiceApplication.java"
click node_auth_controller "https://github.com/wishall05/patient-management/blob/main/auth-service/src/main/java/com/pm/controller/AuthController.java"
click node_auth_service "https://github.com/wishall05/patient-management/blob/main/auth-service/src/main/java/com/pm/service/AuthService.java"
click node_patient_app "https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/java/com/pm/PatientServiceApplication.java"
click node_patient_controller "https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/java/com/pm/controller/PatientController.java"
click node_patient_service "https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/java/com/pm/service/PatientService.java"
click node_patient_contract "https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/java/com/pm/dto/PatientRequestDTO.java"
click node_patient_repository "https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/java/com/pm/repository/PatientRepository.java"
click node_postgres "https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/resources/data.sql"
click node_billing_client "https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/java/com/pm/grpc/BillingServiceGrpcClient.java"
click node_billing_contract "https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/proto/billing_service.proto"
click node_billing "https://github.com/wishall05/patient-management/blob/main/billing-service/src/main/java/com/pm/BillingServiceApplication.java"
click node_billing_server "https://github.com/wishall05/patient-management/blob/main/billing-service/src/main/java/com/pm/grpc/BillingGrpcService.java"
click node_event_producer "https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/java/com/pm/kafka/KafkaProducer.java"
click node_patient_event_schema "https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/proto/patient_event.proto"
click node_analytics "https://github.com/wishall05/patient-management/blob/main/analytics-service/src/main/java/com/pm/AnalyticsServiceApplication.java"
click node_analytics_consumer "https://github.com/wishall05/patient-management/blob/main/analytics-service/src/main/java/com/pm/kafka/KafkaConsumer.java"
click node_localstack_stack "https://github.com/wishall05/patient-management/blob/main/infrastructure/src/main/java/com/pm/stack/LocalStack.java"
click node_deployment_script "https://github.com/wishall05/patient-management/blob/main/infrastructure/localstack-deploy.sh"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_gateway,node_jwt_filter,node_auth,node_auth_controller,node_auth_service toneBlue
class node_patient_app,node_patient_controller,node_patient_service,node_patient_contract,node_patient_repository,node_postgres toneAmber
class node_billing_client,node_billing_contract,node_billing,node_billing_server,node_event_producer,node_patient_event_schema,node_kafka,node_analytics,node_analytics_consumer toneMint
class node_localstack_stack,node_deployment_script toneRose
```

## Components

### Edge & Identity

- [API Gateway](https://github.com/wishall05/patient-management/blob/main/api-gateway/src/main/java/com/pm/ApiGatewayServiceApplication.java): Spring Boot edge service · External HTTP boundary with routed service traffic. · `api-gateway/src/main/java/com/pm/ApiGatewayServiceApplication.java`
- [JWT gateway filter](https://github.com/wishall05/patient-management/blob/main/api-gateway/src/main/java/com/pm/filter/JwtValidationGatewayFilterFactory.java): gateway filter · Validates JWTs before route forwarding. · `api-gateway/src/main/java/com/pm/filter/JwtValidationGatewayFilterFactory.java`
- [Auth Service](https://github.com/wishall05/patient-management/blob/main/auth-service/src/main/java/com/pm/AuthServiceApplication.java): Spring Boot service · Owns login, users, credentials, and token issuance. · `auth-service/src/main/java/com/pm/AuthServiceApplication.java`
- [Auth controller](https://github.com/wishall05/patient-management/blob/main/auth-service/src/main/java/com/pm/controller/AuthController.java): HTTP controller · `auth-service/src/main/java/com/pm/controller/AuthController.java`
- [Auth service & JWT](https://github.com/wishall05/patient-management/blob/main/auth-service/src/main/java/com/pm/service/AuthService.java): domain service · Handles authentication and issues JWTs. · `auth-service/src/main/java/com/pm/service/AuthService.java`

### Patient Domain

- [Patient Service](https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/java/com/pm/PatientServiceApplication.java): Spring Boot service · Owns patient registration and CRUD behavior. · `patient-service/src/main/java/com/pm/PatientServiceApplication.java`
- [Patient controller](https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/java/com/pm/controller/PatientController.java): HTTP controller · `patient-service/src/main/java/com/pm/controller/PatientController.java`
- [Patient service](https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/java/com/pm/service/PatientService.java): domain service · Coordinates validation, creation, billing, and event publication. · `patient-service/src/main/java/com/pm/service/PatientService.java`
- [Patient DTO & mapper](https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/java/com/pm/dto/PatientRequestDTO.java): transport mapping · Separates HTTP contracts from the persistence model. · `patient-service/src/main/java/com/pm/dto/PatientRequestDTO.java`
- [Patient repository](https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/java/com/pm/repository/PatientRepository.java): persistence repository · `patient-service/src/main/java/com/pm/repository/PatientRepository.java`
- [PostgreSQL](https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/resources/data.sql): patient datastore · Stores patient records, with seed data loaded locally. · `patient-service/src/main/resources/data.sql`

### Billing & Events

- [Billing gRPC client](https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/java/com/pm/grpc/BillingServiceGrpcClient.java): gRPC client · Synchronously provisions billing during registration. · `patient-service/src/main/java/com/pm/grpc/BillingServiceGrpcClient.java`
- [Billing protobuf contract](https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/proto/billing_service.proto): gRPC schema · Shared locally duplicated billing wire contract. · `patient-service/src/main/proto/billing_service.proto`
- [Billing Service](https://github.com/wishall05/patient-management/blob/main/billing-service/src/main/java/com/pm/BillingServiceApplication.java): Spring Boot gRPC service · `billing-service/src/main/java/com/pm/BillingServiceApplication.java`
- [Billing gRPC server](https://github.com/wishall05/patient-management/blob/main/billing-service/src/main/java/com/pm/grpc/BillingGrpcService.java): gRPC handler · `billing-service/src/main/java/com/pm/grpc/BillingGrpcService.java`
- [Patient event producer](https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/java/com/pm/kafka/KafkaProducer.java): Kafka producer · Publishes a protobuf event after patient creation. · `patient-service/src/main/java/com/pm/kafka/KafkaProducer.java`
- [Patient event schema](https://github.com/wishall05/patient-management/blob/main/patient-service/src/main/proto/patient_event.proto): protobuf schema · `patient-service/src/main/proto/patient_event.proto`
- **Kafka / MSK**: event broker · Decoupling boundary for patient events.
- [Analytics Service](https://github.com/wishall05/patient-management/blob/main/analytics-service/src/main/java/com/pm/AnalyticsServiceApplication.java): Spring Boot consumer service · Independently consumes patient events. · `analytics-service/src/main/java/com/pm/AnalyticsServiceApplication.java`
- [Analytics consumer](https://github.com/wishall05/patient-management/blob/main/analytics-service/src/main/java/com/pm/kafka/KafkaConsumer.java): Kafka consumer · `analytics-service/src/main/java/com/pm/kafka/KafkaConsumer.java`

### Deployment Runtime

- [LocalStack CDK stack](https://github.com/wishall05/patient-management/blob/main/infrastructure/src/main/java/com/pm/stack/LocalStack.java): AWS infrastructure definition · Defines VPC, MSK, IAM, logs, ECS tasks, and gateway deployment. · `infrastructure/src/main/java/com/pm/stack/LocalStack.java`
- [LocalStack deploy script](https://github.com/wishall05/patient-management/blob/main/infrastructure/localstack-deploy.sh): deployment script · `infrastructure/localstack-deploy.sh`

## Connections

- API Gateway → JWT gateway filter: applies
- JWT gateway filter → Auth Service: auth routes
- JWT gateway filter → Patient Service: forwards validated requests
- Auth Service → Auth controller: serves
- Auth controller → Auth service & JWT: delegates
- Patient Service → Patient controller: serves
- Patient controller → Patient DTO & mapper: maps request/response
- Patient DTO & mapper → Patient service: invokes
- Patient service → Patient repository: persists through
- Patient repository → PostgreSQL: stores patients
- Patient service → Billing gRPC client: provisions billing
- Billing gRPC client → Billing protobuf contract: uses
- Billing protobuf contract → Billing gRPC server: defines RPC
- Billing Service → Billing gRPC server: hosts
- Patient service → Patient event producer: publishes after creation
- Patient event producer → Patient event schema: serializes
- Patient event producer → Kafka / MSK: produces patient event
- Kafka / MSK → Analytics consumer: delivers patient topic
- Analytics Service → Analytics consumer: hosts
- LocalStack deploy script → LocalStack CDK stack: invokes
- LocalStack CDK stack → API Gateway: deploys
- LocalStack CDK stack → Kafka / MSK: provisions MSK
- LocalStack CDK stack → Patient Service: deploys ECS task
- LocalStack CDK stack → Auth Service: deploys ECS task
- LocalStack CDK stack → Billing Service: deploys ECS task
- LocalStack CDK stack → Analytics Service: deploys ECS task
