simplebank

repository·master·Indexed 27 days ago

https://github.com/techschool/simplebank

A banking application featuring a Vue 3 frontend powered by Vite and a backend consisting of a gRPC server, a gRPC-Gateway HTTP server, and a standalone Gin server. The system includes a Redis-based task processor for background workers, a PostgreSQL database with migrations, and support for Paseto token authentication.

Tokens
2.6K
Snippets
17
Records
25
Agent score
92%

What's inside simplebank

  1. Run the SimpleBank application

    master

    The main function serves as the entrypoint for the SimpleBank application. When executed, it performs the following sequence:

    1. Loads configuration from the current directory using util.LoadConfig.
    2. Initializes a PostgreSQL connection pool using pgxpool.
    3. Runs database migrations using runDBMigration.
    4. Initializes the database store via db.NewStore.
    5. Sets up a Redis task distributor for background workers.
    6. Starts three concurrent services using an errgroup:
      • Task Processor: Handles background tasks (e.g., sending emails via Gmail).
      • gRPC Server: Provides the core gRPC API.
      • HTTP Gateway Server: Provides a RESTful JSON API via gRPC-Gateway, including Swagger documentation and CORS support.

    The application handles graceful shutdowns for all services upon receiving os.Interrupt, syscall.SIGTERM, or syscall.SIGINT.

  2. Configure the Simplebank API service via Docker Compose

    master

    The api service can be configured using environment variables to define database connectivity and Redis addressing. The service builds from the local Dockerfile and exposes ports 8080 and 9090.

    api:
      build:
        context: .
        dockerfile: Dockerfile
      ports:
        - "8080:8080"
        - "9090:9090"
      environment:
        - DB_SOURCE=postgresql://root:secret@postgres:5432/simple_bank?sslmode=disable
        - REDIS_ADDRESS=redis:6379
  3. Configure the Postgres database service via Docker Compose

    master

    The postgres service uses the postgres:14-alpine image. You can configure the database credentials and name using the following environment variables:

    • POSTGRES_USER: The database user (default: root)
    • POSTGRES_PASSWORD: The database password (default: secret)
    • POSTGRES_DB: The database name (default: simple_bank)

    Data is persisted using the data-volume volume.

    postgres:
      image: postgres:14-alpine
      environment:
        - POSTGRES_USER=root
        - POSTGRES_PASSWORD=secret
        - POSTGRES_DB=simple_bank
      ports:
        - "5432:5432"
      volumes:
        - data-volume:/var/lib/postgresql/data
  4. Start the Task Processor

    master

    The runTaskProcessor function initializes and starts the background worker system. It uses a Redis-based task distributor and a Gmail sender for email tasks.

    Parameters:

    • ctx: Context for lifecycle management.
    • waitGroup: An errgroup.Group to manage the processor's lifecycle.
    • config: The application configuration (util.Config).
    • redisOpt: Redis client options (asynq.RedisClientOpt).
    • store: The database store (db.Store).

    When the provided ctx is cancelled, the function triggers a graceful shutdown of the taskProcessor.

    func runTaskProcessor(
    	ctx context.Context,
    	waitGroup *errgroup.Group,
    	config util.Config,
    	redisOpt asynq.RedisClientOpt,
    	store db.Store,
    )