# bjhabibie59/golang-rest-api architecture

> Architecture diagram of the GitHub repository bjhabibie59/golang-rest-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-12-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bjhabibie59/golang-rest-api
- Repository: https://github.com/bjhabibie59/golang-rest-api

## Overview

This is a small Go HTTP API for user registration, login, and authenticated user listing, backed by MySQL. The main flow is route dispatch to handlers, service logic, and repository operations; authentication adds JWT issuance and request validation. The entry point wires these layers and configures the database. The README also advertises CRUD, though the shown routes implement only user listing, registration, and login. Relationships are limited to calls and wiring visible in the excerpts; the MySQL connection is documented by the database configuration code.

Main entry points: [`cmd/main.go`](https://github.com/bjhabibie59/golang-rest-api/blob/main/cmd/main.go) (Server Wiring), [`internal/handler/user_handler.go`](https://github.com/bjhabibie59/golang-rest-api/blob/main/internal/handler/user_handler.go) (User Handler), [`internal/service/auth_service.go`](https://github.com/bjhabibie59/golang-rest-api/blob/main/internal/service/auth_service.go) (Auth Service), [`internal/repository/user_repository.go`](https://github.com/bjhabibie59/golang-rest-api/blob/main/internal/repository/user_repository.go) (User Repository)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_http["HTTP API"]
  node_main["Server Wiring<br/>[main.go]"]
  node_auth_handler["Auth Handler<br/>[auth_handler.go]"]
  node_user_handler["User Handler<br/>[user_handler.go]"]
  node_auth_middleware["JWT Middleware<br/>[auth_middleware.go]"]
end

subgraph group_domain["User Domain"]
  node_auth_service["Auth Service<br/>[auth_service.go]"]
  node_user_service["User Service<br/>[user_service.go]"]
  node_user_model["User Model<br/>[user.go]"]
end

subgraph group_persistence["Persistence"]
  node_user_repo["User Repository<br/>[user_repository.go]"]
  node_db_config["DB Connection<br/>[database.go]"]
end

subgraph group_support["Runtime Support"]
  node_json_response["JSON Response<br/>[json.go]"]
end

node_client(("API Client"))
node_mysql[("MySQL")]

node_client -->|"sends requests"| node_main
node_main -->|"opens database"| node_db_config
node_db_config -->|"connects and migrates"| node_mysql
node_db_config -->|"migrates and seeds"| node_user_model
node_main -->|"creates with DB"| node_user_repo
node_main -->|"wires repository"| node_auth_service
node_main -->|"wires repository"| node_user_service
node_main -->|"registers routes"| node_auth_handler
node_main -->|"registers users route"| node_user_handler
node_main -->|"protects users route"| node_auth_middleware
node_auth_middleware -->|"dispatches authorized"| node_user_handler
node_auth_handler -->|"registers or logs in"| node_auth_service
node_auth_handler -->|"formats replies"| node_json_response
node_user_handler -->|"fetches users"| node_user_service
node_user_handler -->|"formats replies"| node_json_response
node_auth_middleware -->|"formats auth errors"| node_json_response
node_auth_service -->|"creates or finds user"| node_user_repo
node_auth_service -->|"builds user"| node_user_model
node_user_service -->|"lists users"| node_user_repo
node_user_repo -->|"queries user records"| node_user_model

click node_main "https://github.com/bjhabibie59/golang-rest-api/blob/main/cmd/main.go"
click node_auth_handler "https://github.com/bjhabibie59/golang-rest-api/blob/main/internal/handler/auth_handler.go"
click node_user_handler "https://github.com/bjhabibie59/golang-rest-api/blob/main/internal/handler/user_handler.go"
click node_auth_middleware "https://github.com/bjhabibie59/golang-rest-api/blob/main/internal/middleware/auth_middleware.go"
click node_auth_service "https://github.com/bjhabibie59/golang-rest-api/blob/main/internal/service/auth_service.go"
click node_user_service "https://github.com/bjhabibie59/golang-rest-api/blob/main/internal/service/user_service.go"
click node_user_model "https://github.com/bjhabibie59/golang-rest-api/blob/main/internal/model/user.go"
click node_user_repo "https://github.com/bjhabibie59/golang-rest-api/blob/main/internal/repository/user_repository.go"
click node_db_config "https://github.com/bjhabibie59/golang-rest-api/blob/main/internal/config/database.go"
click node_json_response "https://github.com/bjhabibie59/golang-rest-api/blob/main/pkg/response/json.go"

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_main,node_auth_handler,node_user_handler,node_auth_middleware,node_client toneBlue
class node_auth_service,node_user_service,node_user_model,node_mysql toneAmber
class node_user_repo,node_db_config toneMint
class node_json_response toneRose
```

## Components

### HTTP API

- [Server Wiring](https://github.com/bjhabibie59/golang-rest-api/blob/main/cmd/main.go): `cmd/main.go`
- [Auth Handler](https://github.com/bjhabibie59/golang-rest-api/blob/main/internal/handler/auth_handler.go): `internal/handler/auth_handler.go`
- [User Handler](https://github.com/bjhabibie59/golang-rest-api/blob/main/internal/handler/user_handler.go): `internal/handler/user_handler.go`
- [JWT Middleware](https://github.com/bjhabibie59/golang-rest-api/blob/main/internal/middleware/auth_middleware.go): `internal/middleware/auth_middleware.go`

### User Domain

- [Auth Service](https://github.com/bjhabibie59/golang-rest-api/blob/main/internal/service/auth_service.go): `internal/service/auth_service.go`
- [User Service](https://github.com/bjhabibie59/golang-rest-api/blob/main/internal/service/user_service.go): `internal/service/user_service.go`
- [User Model](https://github.com/bjhabibie59/golang-rest-api/blob/main/internal/model/user.go): `internal/model/user.go`

### Persistence

- [User Repository](https://github.com/bjhabibie59/golang-rest-api/blob/main/internal/repository/user_repository.go): `internal/repository/user_repository.go`
- [DB Connection](https://github.com/bjhabibie59/golang-rest-api/blob/main/internal/config/database.go): `internal/config/database.go`

### Runtime Support

- [JSON Response](https://github.com/bjhabibie59/golang-rest-api/blob/main/pkg/response/json.go): `pkg/response/json.go`

### Other components

- **API Client**
- **MySQL**

## Connections

- API Client → Server Wiring: sends requests (evidence: `cmd/main.go`)
- Server Wiring → DB Connection: opens database (evidence: `cmd/main.go`)
- DB Connection → MySQL: connects and migrates (evidence: `internal/config/database.go`)
- DB Connection → User Model: migrates and seeds (evidence: `internal/config/database.go`)
- Server Wiring → User Repository: creates with DB (evidence: `cmd/main.go`)
- Server Wiring → Auth Service: wires repository (evidence: `cmd/main.go`)
- Server Wiring → User Service: wires repository (evidence: `cmd/main.go`)
- Server Wiring → Auth Handler: registers routes (evidence: `cmd/main.go`)
- Server Wiring → User Handler: registers users route (evidence: `cmd/main.go`)
- Server Wiring → JWT Middleware: protects users route (evidence: `cmd/main.go`)
- JWT Middleware → User Handler: dispatches authorized
- Auth Handler → Auth Service: registers or logs in (evidence: `internal/handler/auth_handler.go`)
- Auth Handler → JSON Response: formats replies (evidence: `internal/handler/auth_handler.go`)
- User Handler → User Service: fetches users (evidence: `internal/handler/user_handler.go`)
- User Handler → JSON Response: formats replies (evidence: `internal/handler/user_handler.go`)
- JWT Middleware → JSON Response: formats auth errors (evidence: `internal/middleware/auth_middleware.go`)
- Auth Service → User Repository: creates or finds user (evidence: `internal/service/auth_service.go`)
- Auth Service → User Model: builds user (evidence: `internal/service/auth_service.go`)
- User Service → User Repository: lists users (evidence: `internal/service/user_service.go`)
- User Repository → User Model: queries user records (evidence: `internal/repository/user_repository.go`)
