How ashiqyousuf/sbank works
Purpose and process entry point
sbank is a Go banking-service backend: accounts, transfers, users, authentication sessions/tokens, and user lifecycle RPCs. main.go is the composition root, loading configuration and starting the service stack. The repository exposes both native gRPC and HTTP/JSON through a gRPC-Gateway-generated bridge; legacy/direct HTTP handlers also exist under api/.
API delivery surfaces
The primary contract is protobuf-defined user RPCs in proto/service_simple_bank.proto, with request/message definitions in proto/rpc_create_user.proto, proto/rpc_login_user.proto, and proto/rpc_update_user.proto. Generated server, gRPC, and gateway bindings reside in pb/service_simple_bank_grpc.pb.go and pb/service_simple_bank.pb.gw.go. OpenAPI/Swagger static assets are shipped in doc/swagger/, embedded through doc/statik/statik.go.
gRPC application layer
gapi/server.go implements the protobuf service and owns request handling. RPC implementations in gapi/rpc_create_user.go, gapi/rpc_login_user.go, and gapi/rpc_update_user.go convert protobuf inputs to persistence/auth operations and return API messages. gapi/authorization.go provides gRPC authorization interception, while gapi/error.go maps domain/storage failures to transport-level gRPC errors. This interceptor boundary does not automatically apply to gateway HTTP requests.
HTTP API layer
api/server.go provides an HTTP server/route layer alongside the gRPC-first API. Resource handlers are separated by banking concern: api/account.go, api/transfer.go, and api/user.go. api/middleware.go is the HTTP cross-cutting boundary for concerns such as authentication, distinct from gRPC interception. api/validator.go validates inbound HTTP payloads.
Persistence and transactional banking core
PostgreSQL schema evolution is defined in db/migration/000001_init_schema.up.sql, db/migration/000002_add_users.up.sql, and db/migration/000003_add_sessions.up.sql. SQL query specifications live in db/query/account.sql, db/query/transfer.sql, and db/query/session.sql; sqlc-generated typed access code is under db/sqlc/. db/sqlc/store.go is the principal persistence boundary, coordinating queries and likely transaction-scoped account/transfer/entry changes, preventing handlers from directly assembling banking transactions.
Identity, sessions, and credentials
User records and durable sessions are modeled via db/query/user.sql, db/query/session.sql, and generated types in db/sqlc/models.go. The token abstraction in token/maker.go allows authentication implementation substitution; token/jwt_maker.go and token/paseto_maker.go implement JWT and PASETO issuance/verification. Password hashing/verification belongs to util/password.go, keeping raw credentials out of transport and store layers.
Asynchronous verification capability
worker/distributor.go abstracts task publication, while worker/task_send_verify_email.go defines the email-verification task payload/work unit. This establishes an async boundary for external email delivery, although no concrete queue or mail-provider implementation appears in the tree.
Runtime and infrastructure
The runtime is Go (module metadata: go.mod) with PostgreSQL implied by SQL migrations and sqlc usage. Configuration is loaded through util/config.go with environment values represented by app.env. Protocol infrastructure includes gRPC, protobuf, gRPC-Gateway HTTP translation, and embedded Swagger UI/OpenAPI documentation.
Main entry points: rpc_create_user.go User RPC handlers · server.go gRPC application server · main.go Service composition · service_simple_bank.pb.gw.go gRPC-Gateway HTTP bridge
Components 18
API Contracts
- User RPC contract
proto/service_simple_bank.proto - gRPC & gateway bindings
pb/service_simple_bank_grpc.pb.go - Embedded API documentation
doc/statik/statik.go
Transport Layers
- gRPC-Gateway HTTP bridge · Gateway requests bypass gRPC authorization interception.
pb/service_simple_bank.pb.gw.go - gRPC application server
gapi/server.go - User RPC handlers
gapi/rpc_create_user.go - gRPC authorization
gapi/authorization.go - Direct HTTP API
api/server.go - Banking HTTP handlers
api/account.go - HTTP auth & validation
api/middleware.go
Banking Core
- Transactional store · Coordinates typed queries and banking transactions.
db/sqlc/store.go
Identity & Async
- Password credentials
util/password.go - Token abstraction · Supports JWT and PASETO issuance and verification.
token/maker.go - Verification task publisher · Publishes email-verification work without a concrete queue implementation.
worker/distributor.go
Data Layer
- SQL query specifications
db/query/account.sql - PostgreSQL
db/migration/000001_init_schema.up.sql
Other components
- Service composition · Loads configuration and starts the service stack.
main.go - Runtime configuration
util/config.go
Connections 21
- Service composition → Runtime configuration: loads
- Service composition → gRPC application server: starts
- Service composition → Direct HTTP API: starts
- Service composition → gRPC-Gateway HTTP bridge: starts
- User RPC contract → gRPC & gateway bindings: generates
- gRPC & gateway bindings → gRPC application server: service interface
- gRPC & gateway bindings → gRPC-Gateway HTTP bridge: route bindings
- gRPC-Gateway HTTP bridge → gRPC application server: translates HTTP/JSON to RPC
- gRPC authorization → gRPC application server: intercepts RPCs
- gRPC application server → User RPC handlers: dispatches
- User RPC handlers → Transactional store: persists users and sessions
- User RPC handlers → Password credentials: hashes credentials
- User RPC handlers → Token abstraction: issues and verifies
- User RPC handlers → Verification task publisher: publishes verification
- Direct HTTP API → HTTP auth & validation: applies
- HTTP auth & validation → Banking HTTP handlers: guards requests
- Banking HTTP handlers → Transactional store: executes banking operations
- HTTP auth & validation → Token abstraction: authenticates
- Transactional store → SQL query specifications: uses generated access from
- SQL query specifications → PostgreSQL: reads and writes
- Embedded API documentation → gRPC-Gateway HTTP bridge: documents HTTP API