How aom19/agri-api works
agri-api is a REST backend for agricultural operations and farm-resource management. Users authenticate, then call permission-protected APIs for fields, crops, machinery, assignments, field operations, inventory, dashboards, and reports; handlers delegate business rules to use cases and persistence to PostgreSQL repositories. Authentication also uses JWTs, Redis token blacklisting, refresh-token storage, and email. Background monitors track overdue operations, weather snapshots, and scheduled report digests. The tree and excerpts establish these subsystems, but many individual wiring details are unsampled; uncertain connections are shown as dashed.
Main entry points: handlers API handlers · postgres Postgres repositories · main.go Server startup · auth_service.go Authentication
Components 28
HTTP API
- Server startup
cmd/api/main.go - HTTP routes
internal/delivery/http/router.go - API handlers
internal/delivery/http/handlers - Auth middleware
internal/delivery/http/middleware/auth.go
Agricultural workflows
- Field operations
internal/usecase/field_operation_service.go - Operation completion
internal/usecase/field_operation_completion_service.go - Crop management
internal/usecase/crop_service.go - Farm equipment
internal/usecase/assigment_service.go - Reporting
internal/usecase/report_service.go - Report models
internal/domain/report.go - Weather service
internal/usecase/weather_service.go - Resources and stock
internal/usecase/resource_service.go - Notifications
internal/usecase/notification_service.go
Persistence
- Repository store
internal/store/store.go - Postgres repositories
internal/repository/postgres - PostgreSQL
internal/db/postgres.go
Identity and messaging
- Authentication
internal/usecase/auth_service.go - JWT service
internal/auth/jwt_service.go - Refresh tokens
internal/auth/refresh_service.go - Token blacklist
internal/auth/blacklist.go - Email delivery
internal/email/service.go - Redis
internal/redis/client.go
Background services
- Overdue monitor
internal/usecase/field_operation_overdue_monitor.go - Weather snapshots
internal/usecase/weather_snapshot_monitor.go - Report digests
internal/usecase/report_digest_service.go
Other components
- API client
- Open-Meteo
- SMTP server
Connections 39
- API client → HTTP routes: sends requests
internal/delivery/http/router.go - Server startup → HTTP routes: registers routes
- HTTP routes → API handlers: routes requests
internal/delivery/http/router.go - HTTP routes → Auth middleware: protects API
internal/delivery/http/router.go - API handlers → Authentication: invokes auth
- API handlers → Field operations: invokes operations
- API handlers → Operation completion: completes work
internal/delivery/http/handlers/field_operation_handler.go - API handlers → Crop management: invokes crop logic
- API handlers → Farm equipment: invokes assignments
- API handlers → Reporting: requests reports
- API handlers → Resources and stock: manages stock
- API handlers → Notifications: manages notifications
- Auth middleware → JWT service: validates tokens
- Auth middleware → Token blacklist: checks blacklist
- Authentication → JWT service: issues tokens
internal/usecase/auth_service.go - Authentication → Refresh tokens: stores refresh tokens
internal/usecase/auth_service.go - Authentication → Token blacklist: revokes tokens
internal/usecase/auth_service.go - Authentication → Email delivery: sends account mail
internal/usecase/auth_service.go - Field operations → Postgres repositories: persists operations
- Operation completion → Postgres repositories: records completion
- Crop management → Postgres repositories: persists crops
- Farm equipment → Postgres repositories: persists assignments
- Reporting → Postgres repositories: queries reports
- Reporting → Report models: builds report models
internal/usecase/report_service.go - Resources and stock → Postgres repositories: persists inventory
- Postgres repositories → PostgreSQL: reads and writes
- Repository store → Postgres repositories: aggregates repositories
- Server startup → Overdue monitor: starts monitor
cmd/api/main.go - Server startup → Weather snapshots: starts monitor
cmd/api/main.go - Server startup → Report digests: starts digest job
cmd/api/main.go - Overdue monitor → Postgres repositories: checks overdue work
- Weather snapshots → Weather service: fetches conditions
- Weather snapshots → Postgres repositories: stores snapshots
- Weather service → Open-Meteo: fetches forecasts
- Report digests → Reporting: generates digests
- Report digests → Email delivery: sends summaries
- Email delivery → SMTP server: sends mail
internal/email/service.go - Server startup → PostgreSQL: opens connection
- Server startup → Redis: initializes client