# newsand/janus architecture

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

- Interactive diagram: https://gitdiagram.com/newsand/janus
- Repository: https://github.com/newsand/janus

## Overview

Janus is a JWT identity service. Clients call its HTTP API to authenticate, manage users, accept invites, recover accounts, and configure 2FA; successful authentication returns access and refresh tokens. `main.go` initializes configuration and PostgreSQL, then registers feature routes. Feature handlers use shared middleware and GORM-backed models. The README documents password recovery and mail delivery behavior, but the password handler was not sampled, so its internal calls are not asserted.

Main entry points: [`main.go`](https://github.com/newsand/janus/blob/main/main.go) (HTTP server), [`features/auth/handler.go`](https://github.com/newsand/janus/blob/main/features/auth/handler.go) (Authentication), [`features/users/handler.go`](https://github.com/newsand/janus/blob/main/features/users/handler.go) (User management), [`internal/db/db.go`](https://github.com/newsand/janus/blob/main/internal/db/db.go) (Database access)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["HTTP features"]
  node_server["HTTP server<br/>[main.go]"]
  node_auth["Authentication<br/>[handler.go]"]
  node_users["User management<br/>[handler.go]"]
  node_password["Password recovery"]
  node_twofa["Two-factor auth<br/>[handler.go]"]
  node_health["Health checks<br/>[handler.go]"]
end

subgraph group_security["Identity security"]
  node_jwt["JWT and keys<br/>[auth.go]"]
  node_limits["Rate limits<br/>[ratelimit.go]"]
end

subgraph group_persistence["Persistence"]
  node_db["Database access<br/>[db.go]"]
  node_models["Identity records<br/>[models.go]"]
end

subgraph group_runtime["Runtime services"]
  node_config["Configuration<br/>[config.go]"]
  node_logger["Logging<br/>[logger.go]"]
end

node_clients(("API clients"))
node_postgres[("PostgreSQL")]
node_mailer["Email delivery"]

node_clients -->|"HTTP requests"| node_server
node_server -->|"registers routes"| node_auth
node_server -->|"registers routes"| node_users
node_server -->|"registers routes"| node_password
node_server -->|"registers routes"| node_twofa
node_server -->|"registers routes"| node_health
node_server -->|"loads settings"| node_config
node_server -->|"initializes"| node_db
node_server -->|"initializes"| node_logger
node_auth -->|"reads and writes"| node_db
node_users -->|"reads and writes"| node_db
node_twofa -->|"reads and writes"| node_db
node_health -->|"checks connection"| node_db
node_db -->|"connects"| node_postgres
node_auth -->|"uses token records"| node_models
node_users -->|"uses user records"| node_models
node_twofa -->|"uses challenge records"| node_models
node_auth -->|"applies JWT auth"| node_jwt
node_users -->|"applies service-key auth"| node_jwt
node_twofa -->|"applies JWT auth"| node_jwt
node_auth -->|"applies rate limits"| node_limits
node_users -->|"limits invite acceptance"| node_limits
node_auth -->|"reads token settings"| node_config
node_twofa -->|"reads token settings"| node_config
node_users -->|"reads invite settings"| node_config
node_health -->|"reads version"| node_config
node_auth -->|"logs events"| node_logger
node_users -->|"logs events"| node_logger
node_twofa -->|"logs events"| node_logger
node_password -.->|"stores recovery state"| node_db
node_password -.->|"sends recovery email"| node_mailer
node_auth -.->|"delivers magic links"| node_mailer
node_users -.->|"delivers invites"| node_mailer

click node_server "https://github.com/newsand/janus/blob/main/main.go"
click node_auth "https://github.com/newsand/janus/blob/main/features/auth/handler.go"
click node_users "https://github.com/newsand/janus/blob/main/features/users/handler.go"
click node_password "https://github.com/newsand/janus/tree/main/features/password"
click node_twofa "https://github.com/newsand/janus/blob/main/features/twofa/handler.go"
click node_health "https://github.com/newsand/janus/blob/main/features/health/handler.go"
click node_jwt "https://github.com/newsand/janus/blob/main/internal/middleware/auth.go"
click node_limits "https://github.com/newsand/janus/blob/main/internal/middleware/ratelimit.go"
click node_config "https://github.com/newsand/janus/blob/main/internal/config/config.go"
click node_logger "https://github.com/newsand/janus/blob/main/internal/logger/logger.go"
click node_db "https://github.com/newsand/janus/blob/main/internal/db/db.go"
click node_models "https://github.com/newsand/janus/blob/main/internal/models/models.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_auth,node_users,node_password,node_twofa,node_health,node_clients toneBlue
class node_jwt,node_limits,node_postgres toneAmber
class node_db,node_models toneMint
class node_config,node_logger toneRose
class node_mailer toneIndigo
```

## Components

### HTTP features

- [HTTP server](https://github.com/newsand/janus/blob/main/main.go): `main.go`
- [Authentication](https://github.com/newsand/janus/blob/main/features/auth/handler.go): `features/auth/handler.go`
- [User management](https://github.com/newsand/janus/blob/main/features/users/handler.go): `features/users/handler.go`
- [Password recovery](https://github.com/newsand/janus/tree/main/features/password): `features/password`
- [Two-factor auth](https://github.com/newsand/janus/blob/main/features/twofa/handler.go): `features/twofa/handler.go`
- [Health checks](https://github.com/newsand/janus/blob/main/features/health/handler.go): `features/health/handler.go`

### Identity security

- [JWT and keys](https://github.com/newsand/janus/blob/main/internal/middleware/auth.go): `internal/middleware/auth.go`
- [Rate limits](https://github.com/newsand/janus/blob/main/internal/middleware/ratelimit.go): `internal/middleware/ratelimit.go`

### Persistence

- [Database access](https://github.com/newsand/janus/blob/main/internal/db/db.go): `internal/db/db.go`
- [Identity records](https://github.com/newsand/janus/blob/main/internal/models/models.go): `internal/models/models.go`

### Runtime services

- [Configuration](https://github.com/newsand/janus/blob/main/internal/config/config.go): `internal/config/config.go`
- [Logging](https://github.com/newsand/janus/blob/main/internal/logger/logger.go): `internal/logger/logger.go`

### Other components

- **API clients**
- **PostgreSQL**
- **Email delivery**

## Connections

- API clients → HTTP server: HTTP requests (evidence: `README.md`)
- HTTP server → Authentication: registers routes (evidence: `main.go`)
- HTTP server → User management: registers routes (evidence: `main.go`)
- HTTP server → Password recovery: registers routes (evidence: `main.go`)
- HTTP server → Two-factor auth: registers routes (evidence: `main.go`)
- HTTP server → Health checks: registers routes (evidence: `main.go`)
- HTTP server → Configuration: loads settings (evidence: `main.go`)
- HTTP server → Database access: initializes (evidence: `main.go`)
- HTTP server → Logging: initializes (evidence: `main.go`)
- Authentication → Database access: reads and writes (evidence: `features/auth/handler.go`)
- User management → Database access: reads and writes (evidence: `features/users/handler.go`)
- Two-factor auth → Database access: reads and writes (evidence: `features/twofa/handler.go`)
- Health checks → Database access: checks connection (evidence: `features/health/handler.go`)
- Database access → PostgreSQL: connects (evidence: `internal/db/db.go`)
- Authentication → Identity records: uses token records (evidence: `features/auth/handler.go`)
- User management → Identity records: uses user records (evidence: `features/users/handler.go`)
- Two-factor auth → Identity records: uses challenge records (evidence: `features/twofa/handler.go`)
- Authentication → JWT and keys: applies JWT auth (evidence: `features/auth/handler.go`)
- User management → JWT and keys: applies service-key auth (evidence: `features/users/handler.go`)
- Two-factor auth → JWT and keys: applies JWT auth (evidence: `features/twofa/handler.go`)
- Authentication → Rate limits: applies rate limits (evidence: `features/auth/handler.go`)
- User management → Rate limits: limits invite acceptance (evidence: `features/users/handler.go`)
- Authentication → Configuration: reads token settings (evidence: `features/auth/handler.go`)
- Two-factor auth → Configuration: reads token settings (evidence: `features/twofa/handler.go`)
- User management → Configuration: reads invite settings (evidence: `features/users/handler.go`)
- Health checks → Configuration: reads version (evidence: `features/health/handler.go`)
- Authentication → Logging: logs events (evidence: `features/auth/handler.go`)
- User management → Logging: logs events (evidence: `features/users/handler.go`)
- Two-factor auth → Logging: logs events (evidence: `features/twofa/handler.go`)
- Password recovery → Database access: stores recovery state
- Password recovery → Email delivery: sends recovery email (evidence: `README.md`)
- Authentication → Email delivery: delivers magic links (evidence: `README.md`)
- User management → Email delivery: delivers invites (evidence: `README.md`)
