# ducanh271/backenddoan architecture

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

- Interactive diagram: https://gitdiagram.com/ducanh271/backenddoan
- Repository: https://github.com/ducanh271/backenddoan

## Overview

This Go backend provides employee administration, authentication, company configuration, and face-based attendance. The principal workflow accepts an attendance request, verifies identity via the AI service (and, for mobile check-in, checks company network/location constraints), then records attendance or access outcomes. The startup and route wiring and core attendance/authentication code are excerpted; README content is absent, and some handlers and services are unsampled, so those branches are shown only where route declarations or initialization support them.

Main entry points: [`routes/routes.go`](https://github.com/ducanh271/backenddoan/blob/main/routes/routes.go) (Route setup), [`service/auth_service.go`](https://github.com/ducanh271/backenddoan/blob/main/service/auth_service.go) (Authentication), [`service/attendance_service.go`](https://github.com/ducanh271/backenddoan/blob/main/service/attendance_service.go) (Attendance decisions), [`repository/employee_repository.go`](https://github.com/ducanh271/backenddoan/blob/main/repository/employee_repository.go) (Employee repository)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API and security"]
  node_server["Gin server<br/>[main.go]"]
  node_routes["Route setup<br/>[routes.go]"]
  node_authmw["JWT authorization<br/>[jwt_auth.go]"]
  node_authhandler["Auth endpoints<br/>[auth_handler.go]"]
  node_authservice["Authentication<br/>[auth_service.go]"]
  node_security["Token and OTP<br/>[token.go]"]
  node_otp["OTP generation<br/>[otp.go]"]
  node_email["OTP email<br/>[email.go]"]
end

subgraph group_workforce["Workforce management"]
  node_employeehandler["Employee endpoints"]
  node_employeeservice["Employee services"]
  node_registration["Employee registration"]
  node_confighandler["Config endpoints<br/>[config_handler.go]"]
  node_configservice["Company configuration<br/>[config_service.go]"]
end

subgraph group_attendance["Attendance"]
  node_attendancehandler["Attendance endpoints"]
  node_attservice["Attendance decisions"]
end

subgraph group_persistence["Persistence and integrations"]
  node_ai["Face AI client<br/>[ai_client.go]"]
  node_employee_repo["Employee repository"]
  node_attendance_repo["Attendance repository"]
  node_config_repo["Config repository"]
  node_token_repo["Refresh token repository"]
  node_mysql[("MySQL database<br/>[db.go]")]
end

node_client(("API clients"))
node_ai_service(("AI service"))
node_smtp(("SMTP service"))

node_client -->|"sends requests"| node_server
node_server -->|"configures routes"| node_routes
node_routes -->|"routes auth"| node_authhandler
node_routes -->|"routes attendance"| node_attendancehandler
node_routes -->|"routes employees"| node_employeehandler
node_routes -->|"routes config"| node_confighandler
node_routes -->|"guards routes"| node_authmw
node_authhandler -->|"calls"| node_authservice
node_authservice -->|"reads and updates"| node_employee_repo
node_authservice -->|"rotates tokens"| node_token_repo
node_authservice -->|"generates tokens"| node_security
node_authservice -->|"generates OTP"| node_otp
node_authservice -->|"sends OTP"| node_email
node_email -.->|"sends mail"| node_smtp
node_employeehandler -.->|"registers employees"| node_registration
node_employeehandler -.->|"manages employees"| node_employeeservice
node_employeeservice -.->|"uses repository"| node_employee_repo
node_registration -.->|"stores employees"| node_employee_repo
node_registration -.->|"registers faces"| node_ai
node_confighandler -.->|"calls"| node_configservice
node_configservice -.->|"reads and updates"| node_config_repo
node_attendancehandler -->|"calls"| node_attservice
node_attservice -->|"loads employees"| node_employee_repo
node_attservice -->|"checks company settings"| node_config_repo
node_attservice -->|"verifies faces"| node_ai
node_ai -->|"posts images"| node_ai_service
node_attservice -->|"reads and saves records"| node_attendance_repo
node_attservice -->|"logs access"| node_employee_repo
node_employee_repo -.->|"queries employees"| node_mysql
node_attendance_repo -.->|"queries attendance"| node_mysql
node_config_repo -.->|"stores settings"| node_mysql
node_token_repo -.->|"stores tokens"| node_mysql
node_server -->|"opens connection"| node_mysql

click node_server "https://github.com/ducanh271/backenddoan/blob/main/cmd/main.go"
click node_routes "https://github.com/ducanh271/backenddoan/blob/main/routes/routes.go"
click node_authmw "https://github.com/ducanh271/backenddoan/blob/main/middleware/jwt_auth.go"
click node_authhandler "https://github.com/ducanh271/backenddoan/blob/main/handlers/auth_handler.go"
click node_authservice "https://github.com/ducanh271/backenddoan/blob/main/service/auth_service.go"
click node_security "https://github.com/ducanh271/backenddoan/blob/main/security/token.go"
click node_otp "https://github.com/ducanh271/backenddoan/blob/main/security/otp.go"
click node_email "https://github.com/ducanh271/backenddoan/blob/main/utils/email.go"
click node_employeehandler "https://github.com/ducanh271/backenddoan/blob/main/handlers/employee_handler.go"
click node_employeeservice "https://github.com/ducanh271/backenddoan/blob/main/service/employee_service.go"
click node_registration "https://github.com/ducanh271/backenddoan/blob/main/service/register_service.go"
click node_confighandler "https://github.com/ducanh271/backenddoan/blob/main/handlers/config_handler.go"
click node_configservice "https://github.com/ducanh271/backenddoan/blob/main/service/config_service.go"
click node_attendancehandler "https://github.com/ducanh271/backenddoan/blob/main/handlers/attendance_handler.go"
click node_attservice "https://github.com/ducanh271/backenddoan/blob/main/service/attendance_service.go"
click node_ai "https://github.com/ducanh271/backenddoan/blob/main/infrastructure/ai/ai_client.go"
click node_employee_repo "https://github.com/ducanh271/backenddoan/blob/main/repository/employee_repository.go"
click node_attendance_repo "https://github.com/ducanh271/backenddoan/blob/main/repository/attendance_repository.go"
click node_config_repo "https://github.com/ducanh271/backenddoan/blob/main/repository/company_config_repository.go"
click node_token_repo "https://github.com/ducanh271/backenddoan/blob/main/repository/refresh_token_repository.go"
click node_mysql "https://github.com/ducanh271/backenddoan/blob/main/database/db.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_server,node_routes,node_authmw,node_authhandler,node_authservice,node_security,node_otp,node_email,node_client toneBlue
class node_employeehandler,node_employeeservice,node_registration,node_confighandler,node_configservice toneAmber
class node_attendancehandler,node_attservice toneMint
class node_ai,node_employee_repo,node_attendance_repo,node_config_repo,node_token_repo,node_mysql toneRose
class node_ai_service,node_smtp toneIndigo
```

## Components

### API and security

- [Gin server](https://github.com/ducanh271/backenddoan/blob/main/cmd/main.go): `cmd/main.go`
- [Route setup](https://github.com/ducanh271/backenddoan/blob/main/routes/routes.go): `routes/routes.go`
- [JWT authorization](https://github.com/ducanh271/backenddoan/blob/main/middleware/jwt_auth.go): `middleware/jwt_auth.go`
- [Auth endpoints](https://github.com/ducanh271/backenddoan/blob/main/handlers/auth_handler.go): `handlers/auth_handler.go`
- [Authentication](https://github.com/ducanh271/backenddoan/blob/main/service/auth_service.go): `service/auth_service.go`
- [Token and OTP](https://github.com/ducanh271/backenddoan/blob/main/security/token.go): `security/token.go`
- [OTP generation](https://github.com/ducanh271/backenddoan/blob/main/security/otp.go): `security/otp.go`
- [OTP email](https://github.com/ducanh271/backenddoan/blob/main/utils/email.go): `utils/email.go`

### Workforce management

- [Employee endpoints](https://github.com/ducanh271/backenddoan/blob/main/handlers/employee_handler.go): `handlers/employee_handler.go`
- [Employee services](https://github.com/ducanh271/backenddoan/blob/main/service/employee_service.go): `service/employee_service.go`
- [Employee registration](https://github.com/ducanh271/backenddoan/blob/main/service/register_service.go): `service/register_service.go`
- [Config endpoints](https://github.com/ducanh271/backenddoan/blob/main/handlers/config_handler.go): `handlers/config_handler.go`
- [Company configuration](https://github.com/ducanh271/backenddoan/blob/main/service/config_service.go): `service/config_service.go`

### Attendance

- [Attendance endpoints](https://github.com/ducanh271/backenddoan/blob/main/handlers/attendance_handler.go): `handlers/attendance_handler.go`
- [Attendance decisions](https://github.com/ducanh271/backenddoan/blob/main/service/attendance_service.go): `service/attendance_service.go`

### Persistence and integrations

- [Face AI client](https://github.com/ducanh271/backenddoan/blob/main/infrastructure/ai/ai_client.go): `infrastructure/ai/ai_client.go`
- [Employee repository](https://github.com/ducanh271/backenddoan/blob/main/repository/employee_repository.go): `repository/employee_repository.go`
- [Attendance repository](https://github.com/ducanh271/backenddoan/blob/main/repository/attendance_repository.go): `repository/attendance_repository.go`
- [Config repository](https://github.com/ducanh271/backenddoan/blob/main/repository/company_config_repository.go): `repository/company_config_repository.go`
- [Refresh token repository](https://github.com/ducanh271/backenddoan/blob/main/repository/refresh_token_repository.go): `repository/refresh_token_repository.go`
- [MySQL database](https://github.com/ducanh271/backenddoan/blob/main/database/db.go): `database/db.go`

### Other components

- **API clients**
- **AI service**
- **SMTP service**

## Connections

- API clients → Gin server: sends requests
- Gin server → Route setup: configures routes (evidence: `cmd/main.go`)
- Route setup → Auth endpoints: routes auth (evidence: `routes/routes.go`)
- Route setup → Attendance endpoints: routes attendance (evidence: `routes/routes.go`)
- Route setup → Employee endpoints: routes employees (evidence: `routes/routes.go`)
- Route setup → Config endpoints: routes config (evidence: `routes/routes.go`)
- Route setup → JWT authorization: guards routes (evidence: `routes/routes.go`)
- Auth endpoints → Authentication: calls (evidence: `handlers/auth_handler.go`)
- Authentication → Employee repository: reads and updates (evidence: `service/auth_service.go`)
- Authentication → Refresh token repository: rotates tokens (evidence: `service/auth_service.go`)
- Authentication → Token and OTP: generates tokens (evidence: `service/auth_service.go`)
- Authentication → OTP generation: generates OTP (evidence: `service/auth_service.go`)
- Authentication → OTP email: sends OTP (evidence: `service/auth_service.go`)
- OTP email → SMTP service: sends mail
- Employee endpoints → Employee registration: registers employees
- Employee endpoints → Employee services: manages employees
- Employee services → Employee repository: uses repository
- Employee registration → Employee repository: stores employees
- Employee registration → Face AI client: registers faces
- Config endpoints → Company configuration: calls
- Company configuration → Config repository: reads and updates
- Attendance endpoints → Attendance decisions: calls (evidence: `handlers/attendance_handler.go`)
- Attendance decisions → Employee repository: loads employees (evidence: `service/attendance_service.go`)
- Attendance decisions → Config repository: checks company settings (evidence: `service/attendance_service.go`)
- Attendance decisions → Face AI client: verifies faces (evidence: `service/attendance_service.go`)
- Face AI client → AI service: posts images (evidence: `infrastructure/ai/ai_client.go`)
- Attendance decisions → Attendance repository: reads and saves records (evidence: `service/attendance_service.go`)
- Attendance decisions → Employee repository: logs access (evidence: `service/attendance_service.go`)
- Employee repository → MySQL database: queries employees
- Attendance repository → MySQL database: queries attendance
- Config repository → MySQL database: stores settings
- Refresh token repository → MySQL database: stores tokens
- Gin server → MySQL database: opens connection (evidence: `cmd/main.go`)
