# xarest/gobs architecture

> Architecture diagram of the GitHub repository xarest/gobs, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/xarest/gobs
- Repository: https://github.com/xarest/gobs

## Overview

##### Project Type and Purpose
- This is a Go-based dependency-injection & lifecycle management framework (library), not a full-stack app.
- Its purpose is to let users register “services” (instances) that implement lifecycle interfaces (Init, Setup, Start, Stop), resolve dependencies between them, then orchestrate those lifecycle phases in a correct order.

##### Main Components
1. Application Layer (samples / user code)
– sample/*.go (main.go)
– Calls gobs.NewBootstrap(), bs.AddDefault(&MyService{}), bs.StartBootstrap(ctx)
2. GoBs Core Library
a. Bootstrap Manager (bootstrap.go, service.go, config.go, dependencies.go)
– Registers services, holds service descriptors, kicks off lifecycle
b. Scheduler (scheduler/scheduler.go)
– Builds dependency graph, computes execution order (topological sort), invokes hooks
c. Common & Types (common/*.go, types/types.go)
– Defines ServiceStatus, ServiceLifeCycle, Dependencies, and error types
d. Logger (logger/log.go)
– Centralized logging of each phase and service outcome
e. Utils (utils/*.go)
– Helper functions (e.g., loop over services concurrently or sequentially)
3. Tests (test/*.go)
– Verify behaviour of Bootstrap and Scheduler
– Not part of runtime, but indicate coverage of each lifecycle phase

##### Relationships & Interactions
- Application Layer → Bootstrap Manager
(registers services, passes context)
- Bootstrap Manager ↔ Common / Types
(records each IService, its dependencies, status flags)
- Bootstrap Manager → Scheduler
(invoke scheduler.Init(), .Setup(), .Start(), .Stop())
- Scheduler → Services
(calls each service’s Init/Setup/Start/Stop in order dictated by dependency graph)
- Scheduler & Bootstrap → Logger
(emit “Service X Init successfully”, etc.)
- Dependencies flow: service D depends on B,C; B depends on A; C depends on A,B; etc.

##### Architectural Patterns & Design Principles
- Dependency Injection via “AddDefault(new(Service))”
- Lifecycle hook interfaces (IServiceInit, IServiceSetup, etc.)
- Graph-based scheduling (topological sort + reverse order for teardown)
- Separation of concerns: registration vs. scheduling vs. logging vs. common types

##### Essential Elements
– A small inset showing a sample dependency graph (nodes A, B, C, D with directed edges)

[APPLICATION (samples/main.go)]
│
└─► Register Services
│
▼
[BOOTSTRAP MANAGER] ◀── uses ── [common & types]
│─► build graph ──► [SCHEDULER]
│                      │
│                      ├─► calls IService.Init()
│                      ├─► calls IService.Setup()
│                      ├─► calls IService.Start()
│                      └─► calls IService.Stop() (reverse order)
│
└─► logs ──► [LOGGER]

Inset:
Dependency Graph
A ↘      ↘
B ─────► C
↗
D

- Blue: Core packages
- Green: User application
- Orange: Scheduling engine
- Purple: Logging
- Gray: Shared types

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Application Layer
    subgraph "Application Layer" 
        direction TB
        App1["samples/1_instance/main.go"]:::app
        App3["samples/3_instances/main.go"]:::app
        App9["samples/9_instances/main.go"]:::app
    end

    %% Core Library
    subgraph "Core Library"
        direction TB
        BM["Bootstrap Manager"]:::core
        subgraph "Common & Types"
            direction TB
            CE["common/error.go"]:::types
            CS["common/service-status.go"]:::types
            TT["types/types.go"]:::types
        end
        SCH["Scheduler"]:::sched
        LOG["Logger"]:::logger
        subgraph "Utils"
            direction TB
            UL["utils/loop.go"]:::core
            UU["utils/utils.go"]:::core
        end
    end

    %% Tests
    subgraph "Tests"
        direction TB
        TB1["test/bootstrap_test.go"]:::test
        TB2["test/bootstrap_scheduler_test.go"]:::test
        TB3["test/scheduler_test.go"]:::test
        TB4["test/store_scheduler_test.go"]:::test
        TB5["test/store_service_test.go"]:::test
        TB6["test/interface_test.go"]:::test
        TB7["test/performance_test.go"]:::test
        TB8["test/performance_async_test.go"]:::test
    end

    %% Dependency Graph Inset
    subgraph "Dependency Graph"
        direction TB
        A["A"]:::graphnode
        B["B"]:::graphnode
        C["C"]:::graphnode
        D["D"]:::graphnode
        A --> B
        A --> C
        B --> C
        B --> D
    end

    %% Connections
    App1 -->|"Register()/StartBootstrap()"| BM
    App3 -->|"Register()/StartBootstrap()"| BM
    App9 -->|"Register()/StartBootstrap()"| BM

    BM -->|"uses types"| CE
    BM -->|"uses types"| CS
    BM -->|"uses types"| TT

    BM -->|"build graph/invoke hooks"| SCH
    BM -->|"logs status"| LOG

    SCH -->|"Invoke Init/Setup/Start/Stop"| BM
    SCH -->|"logs status"| LOG

    %% Styles
    classDef app fill:#c8e6c9,stroke:#2e7d32,color:#2e7d32
    classDef core fill:#bbdefb,stroke:#1565c0,color:#1565c0
    classDef types fill:#eeeeee,stroke:#616161,color:#424242
    classDef sched fill:#ffe0b2,stroke:#ef6c00,color:#e65100
    classDef logger fill:#e1bee7,stroke:#6a1b9a,color:#4a148c
    classDef test fill:#f5f5f5,stroke:#9e9e9e,color:#424242
    classDef graphnode fill:#ffffff,stroke:#000000,color:#000000

    %% Click Events
    click App1 "https://github.com/xarest/gobs/blob/main/samples/1_instance/main.go"
    click App3 "https://github.com/xarest/gobs/blob/main/samples/3_instances/main.go"
    click App9 "https://github.com/xarest/gobs/blob/main/samples/9_instances/main.go"
    click BM "https://github.com/xarest/gobs/blob/main/bootstrap.go"
    click CE "https://github.com/xarest/gobs/blob/main/common/error.go"
    click CS "https://github.com/xarest/gobs/blob/main/common/service-status.go"
    click TT "https://github.com/xarest/gobs/blob/main/types/types.go"
    click SCH "https://github.com/xarest/gobs/blob/main/scheduler/scheduler.go"
    click LOG "https://github.com/xarest/gobs/blob/main/logger/log.go"
    click UL "https://github.com/xarest/gobs/blob/main/utils/loop.go"
    click UU "https://github.com/xarest/gobs/blob/main/utils/utils.go"
    click TB1 "https://github.com/xarest/gobs/blob/main/test/bootstrap_test.go"
    click TB2 "https://github.com/xarest/gobs/blob/main/test/bootstrap_scheduler_test.go"
    click TB3 "https://github.com/xarest/gobs/blob/main/test/scheduler_test.go"
    click TB4 "https://github.com/xarest/gobs/blob/main/test/store_scheduler_test.go"
    click TB5 "https://github.com/xarest/gobs/blob/main/test/store_service_test.go"
    click TB6 "https://github.com/xarest/gobs/blob/main/test/interface_test.go"
    click TB7 "https://github.com/xarest/gobs/blob/main/test/performance_test.go"
    click TB8 "https://github.com/xarest/gobs/blob/main/test/performance_async_test.go"
```
