# saketv8/basic-crud-api-golang architecture

> Architecture diagram of the GitHub repository saketv8/basic-crud-api-golang, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/saketv8/basic-crud-api-golang
- Repository: https://github.com/saketv8/basic-crud-api-golang

## Overview

##### Main Components
- API Server: Handled by main.go which sets up the Gin server.
- Routes: Located in the "routes" directory (routes.go) indicating the mapping of API endpoints to respective handlers.
- Handlers: Found under "handlers" (for instance, account_handler.go) where HTTP requests are processed.
- Models: Under the "models" directory (e.g., user_account.go) representing the data structures used in the application.
- Database Layer: The “sqlite” directory (e.g., query_user_accounts.go) contains the SQL handling code, and the "databases/migrations" folder holds the migration scripts managed by Goose.
- External Services: Although not explicitly called here, the CRUD operations interact with an embedded SQLite database making it an important external component from the business logic perspective.

##### Relationships and Data Flow
- Clients (e.g., Postman, Httpie) make HTTP requests (GET, POST, PUT, DELETE).
- The requests first hit the routing layer (component labeled “Routes”) which directs them to the appropriate handler in "handlers".
- The Handlers use the models (data definitions in “models”) to parse or construct data and then interact with the database.
- The Database Layer, represented by SQLite interactions in “sqlite”, carries out the actual data manipulation, with migrations referenced in "databases/migrations" to set up schema changes.

##### Architectural Patterns and Design Principles
- Separation of Concerns: Dividing the application into distinct layers (routes, handlers, models, database access) to maintain clarity and modularity.
- RESTful Design: Clear delineation of API endpoints with CRUD actions.
- Use of Migrations: The database schema is managed separately with Goose, emphasizing maintainability of the schema evolution.
- Framework Usage: Gin’s middleware and routing patterns typically encourage structured and efficient API design.

##### Specific Technologies
- Go (the core programming language)
- Gin: for HTTP routing and request handling.
- SQLite: for the persistent data store.
- Goose: for database migration management.
- SQLite Driver (mattn/go-sqlite3): to enable SQLite integration with Go.

## Diagram (Mermaid)

```mermaid
graph TD
    %% Client Layer
    client["Clients (Postman, Httpie)"]:::client

    %% API Layer Subgraph
    subgraph "API Layer"
        apiServer["API Server"]:::apiLayer
        routesModule["Routes Module"]:::apiLayer
        accountHandler["User Account Handler"]:::apiLayer
        userAccountModel["Data Model (User Account)"]:::apiLayer
    end

    %% Database Layer Subgraph
    subgraph "Database Layer"
        databaseAccessLayer["Database Access Layer"]:::dataLayer
        sqliteDB["SQLite Database"]:::dataLayer
        databaseMigrations["Database Migrations (Goose)"]:::migrations
    end

    %% Data Flow Arrows
    client -->|"HTTP_request"| apiServer
    apiServer -->|"configures_routes"| routesModule
    routesModule -->|"calls"| accountHandler
    accountHandler -->|"uses"| userAccountModel
    accountHandler -->|"performsCRUD"| databaseAccessLayer
    databaseAccessLayer -->|"executesQueriesOn"| sqliteDB
    databaseMigrations -->|"managesSchemaOf"| sqliteDB
    apiServer -->|"HTTP_response"| client

    %% Click Events
    click apiServer "https://github.com/saketv8/basic-crud-api-golang/blob/main/main.go"
    click routesModule "https://github.com/saketv8/basic-crud-api-golang/blob/main/routes/routes.go"
    click accountHandler "https://github.com/saketv8/basic-crud-api-golang/blob/main/handlers/account_handler.go"
    click userAccountModel "https://github.com/saketv8/basic-crud-api-golang/blob/main/models/user_account.go"
    click databaseAccessLayer "https://github.com/saketv8/basic-crud-api-golang/blob/main/sqlite/query_user_accounts.go"
    click databaseMigrations "https://github.com/saketv8/basic-crud-api-golang/blob/main/databases/migrations/20241108192959_user_accounts.sql"

    %% Styles
    classDef client fill:#D5F5E3,stroke:#1D8348,stroke-width:2px;
    classDef apiLayer fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef dataLayer fill:#F9E79F,stroke:#9A7D0A,stroke-width:2px;
    classDef migrations fill:#FADBD8,stroke:#922B21,stroke-width:2px;
```
