Overview
POS Kasir follows a modern full-stack architecture with clear separation between frontend and backend. The system is built with scalability, maintainability, and type-safety as core principles.High-Level Architecture
Backend Architecture
The backend follows a layered architecture pattern with clear separation of concerns.Layer Structure
1. Handler Layer (HTTP)
Located ininternal/*/handler.go
- Handles HTTP requests and responses
- Performs input validation
- Converts DTOs to/from domain models
- Returns appropriate HTTP status codes
server/routes.go:15-21
2. Service Layer (Business Logic)
Located ininternal/*/service.go
- Contains core business logic
- Orchestrates operations between repositories
- Handles transactions using sqlc Store
- Integrates with external services
- Implements domain rules and validations
3. Repository Layer (Data Access)
Located ininternal/*/repository/
- Auto-generated by sqlc for type-safe database queries
- Executes SQL queries defined in
.sqlfiles - Returns strongly-typed Go structs
- Handles connection pooling via pgx
internal/products/repository/querier.go
4. Package Layer (pkg/)
Shared utilities and integrations:pkg/database- PostgreSQL connection and migrationspkg/logger- Structured logging with Logruspkg/payment- Midtrans payment gateway integrationpkg/cloudflare-r2- Image storage servicepkg/utils- JWT manager, helperspkg/validator- Request validationpkg/cache- In-memory cachingpkg/escpos- Thermal printer ESC/POS protocol
Dependency Injection
The application uses constructor-based dependency injection: Server Initialization (server/server.go:92-139):
server/server.go:141-225):
Frontend Architecture
TanStack Start Framework
The frontend uses TanStack Start, a full-stack React framework featuring:- File-based routing with type-safe navigation
- Server-side rendering (SSR) and streaming support
- TanStack Query for data fetching and caching
- TanStack Router for advanced routing capabilities
Key Components
API Client Generation
API client is auto-generated from backend Swagger specs:- Type-safe API calls
- Automatic DTO/model synchronization
- Reduced manual coding errors
State Management
- TanStack Query for server state (API data, caching)
- React Context for global UI state
- React Hook Form + Zod for form state and validation
UI Components
- Shadcn UI - Accessible, customizable components
- Tailwind CSS - Utility-first styling
- Radix UI primitives - Unstyled, accessible primitives
Communication Flow
1. Authentication Flow
2. Order Creation Flow
3. Payment Integration Flow
Middleware Architecture
Authentication Middleware
Location:internal/common/middleware/auth.go
- Validates JWT tokens
- Extracts user claims
- Injects user context into requests
server/routes.go:13
Role-Based Access Control (RBAC)
Three roles with hierarchical permissions:- Admin - Full system access
- Manager - Product, reports, settings management
- Cashier - Order processing, basic operations
server/routes.go:18
Shift Middleware
Ensures cashiers have an active shift before processing orders:Database Transaction Management
Using sqlc Store pattern for atomic operations:Error Handling
Custom Error Handler
server/server.go:289-305
Configuration Management
Centralized configuration via environment variables: Location:config/config.go
Loads from:
.envfile (development)- System environment variables (production)
- Server settings (port, CORS)
- Database credentials
- JWT secrets
- Midtrans API keys
- Cloudflare R2 credentials
Activity Logging
All significant operations are logged for audit trails: Tracked Actions:- CREATE, UPDATE, DELETE
- CANCEL (orders)
- APPLY_PROMOTION
- PROCESS_PAYMENT
- PRODUCT, CATEGORY, PROMOTION, ORDER, USER
activity_logs table with JSONB details
Caching Strategy
In-Memory Cache
Used for:- Active shift data (prevents repeated DB queries)
- User sessions
- Frequently accessed settings
pkg/cache/memory_cache.go
Performance Considerations
- Connection Pooling: pgx/v5 connection pool for efficient database access
- Query Optimization: Indexed columns for common queries (see migrations)
- Lazy Loading: Frontend loads data on-demand using TanStack Query
- Image Optimization: Cloudflare R2 CDN for fast image delivery
- Type-Safe Queries: sqlc eliminates runtime query errors
Security Architecture
- Authentication: JWT-based with refresh tokens
- Authorization: Role-based access control (RBAC)
- Password Hashing: bcrypt for secure password storage
- CORS: Configurable allowed origins
- SQL Injection Prevention: Parameterized queries via sqlc
- Input Validation: go-playground/validator on all inputs
Next Steps
- Tech Stack - Detailed technology breakdown
- Database Schema - Complete schema documentation
- Contributing Guide - Development setup and workflow