# hugohu0224/sportshot architecture

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

- Interactive diagram: https://gitdiagram.com/hugohu0224/sportshot
- Repository: https://github.com/hugohu0224/sportshot

## Overview

##### Project Type and Purpose
- Sportshot is a full-stack sports odds scanning tool composed of three primary services:
1. A **crawler** that periodically fetches sports betting odds (currently basketball) from an external API.
2. A **gRPC event server** that provides filtered, historical odds data to clients, using etcd for service discovery.
3. A **web server** (with frontend UI) that handles user authentication, exposes RESTful APIs, and acts as a gRPC client to retrieve odds.
- Data persistence layers include MongoDB (odds storage) and MySQL (user auth), plus etcd for gRPC naming/discovery.

##### File-Structure Analysis
- Top-level commands under `cmd/`:
- `crawler/main.go` – starts the crawler worker
- `grpcserver/events/main.go` – starts the gRPC event server
- `webserver/main.go` – starts the web server + UI
- Internal packages in `internal/` map each service:
- `internal/crawler` – crawling logic & interfaces
- `internal/grpcserver/events` – gRPC handler, persistence layer
- `internal/webserver` – HTTP API endpoints, JWT auth, routing, templating, static assets
- Shared code in `pkg/`:
- `pkg/utils/db` – adapters for MongoDB, MySQL, etcd (viper config)
- `pkg/utils/proto` – protobuf definitions & generated gRPC stubs
- `pkg/utils/models` – domain models (Event, User, Login)
- Infrastructure config:
- `docker-compose.yaml` – orchestrates MongoDB, MySQL, etcd, and the three Go services
- `config.yaml` – application settings (crawler interval, DB URIs)
- `scripts/entrypoint.sh`, `Dockerfile` – container bootstrapping

##### Key Architectural Patterns
- **Micro-service style**: Three independent Go binaries, each with their own internal layers.
- **Dependency injection** via interfaces in `internal/crawler/interfaces.go`.
- **API gateway / BFF**: Web server stands between UI and gRPC service.
- **Service discovery & load balancing**: gRPC server registers with etcd; web server uses etcd client to locate instances.
- **Separation of concerns**:
- Crawling → data ingestion
- gRPC server → data filtering & access
- Web server → user management & UI

- Components:
A. **External API** (BetsAPI)
B. **Crawler Service** (cmd/crawler)
C. **MongoDB** (odds storage)
D. **gRPC Event Server** (cmd/grpcserver)
E. **etcd Cluster** (service registry)
F. **MySQL** (user credentials)
G. **Web Server + UI** (cmd/webserver)
H. **Browser/UI Client**
- Interactions:
1. Crawler polls External API → writes to MongoDB.
2. gRPC Server reads from MongoDB → serves filtered odds via gRPC.
3. gRPC Server registers its endpoint(s) in etcd upon startup.
4. Web Server uses etcd client to discover gRPC Server addresses.
5. Web Server authenticates users against MySQL (via pkg/utils/db/mysql).
6. Authenticated Browser/UI → Web Server REST API → Web server calls gRPC to fetch odds → returns JSON/UI templates.

##### Final Checklist
- Clear, distinct labels for each service and datastore
- Annotations for key frameworks/libs (Go, gRPC, etcd, Viper, Gin/Chi or net/http insofar as applicable)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Ingestion Layer
    subgraph "Ingestion" 
        direction TB
        CrawlerService["Crawler Service"]:::ingestion
        BetsAPI["External BetsAPI"]:::external
        MongoDB["MongoDB"]:::database
    end

    CrawlerService -->|"Poll (every 30s) HTTP"| BetsAPI
    CrawlerService -->|"Write (MongoDB Driver)"| MongoDB

    %% Processing Layer
    subgraph "Processing" 
        direction TB
        GRPCServer["gRPC Event Server"]:::processing
        Etcd["etcd Cluster"]:::registry
    end

    GRPCServer -->|"Read (MongoDB Driver)"| MongoDB
    GRPCServer -->|"Register"| Etcd
    Etcd -->|"Service Discovery"| GRPCServer

    %% Presentation Layer
    subgraph "Presentation"
        direction TB
        Browser["Browser/UI Client"]:::client
        WebServer["Web Server + UI"]:::presentation
        MySQL["MySQL"]:::database
    end

    Browser -->|"HTTP/REST (Gin)"| WebServer
    WebServer -->|"gRPC"| GRPCServer
    WebServer -->|"MySQL Driver"| MySQL
    WebServer -->|"Serve Static/Templates"| Browser

    %% Configuration
    Config["config.yaml"]:::config
    Config -.-> CrawlerService
    Config -.-> GRPCServer
    Config -.-> WebServer

    %% Orchestration
    Infra["Docker Compose & Dockerfile"]:::infra
    Infra --> CrawlerService
    Infra --> GRPCServer
    Infra --> WebServer
    Infra --> MongoDB
    Infra --> MySQL
    Infra --> Etcd

    %% Click Events
    click CrawlerService "https://github.com/hugohu0224/sportshot/blob/main/cmd/crawler/main.go"
    click GRPCServer "https://github.com/hugohu0224/sportshot/blob/main/cmd/grpcserver/events/main.go"
    click WebServer "https://github.com/hugohu0224/sportshot/blob/main/cmd/webserver/main.go"
    click MongoDB "https://github.com/hugohu0224/sportshot/blob/main/pkg/utils/db/mongodb.go"
    click MySQL "https://github.com/hugohu0224/sportshot/blob/main/pkg/utils/db/mysql.go"
    click Etcd "https://github.com/hugohu0224/sportshot/blob/main/pkg/utils/db/etcd.go"
    click Config "https://github.com/hugohu0224/sportshot/blob/main/config.yaml"
    click Infra "https://github.com/hugohu0224/sportshot/blob/main/docker-compose.yaml"

    %% Styles
    classDef ingestion fill:#e0f7fa,stroke:#006064,color:#004d40
    classDef processing fill:#e8f5e9,stroke:#1b5e20,color:#2e7d32
    classDef presentation fill:#fff3e0,stroke:#e65100,color:#bf360c
    classDef registry fill:#f3e5f5,stroke:#4a148c,color:#6a1b9a
    classDef database fill:#fafafa,stroke:#424242,color:#212121,stroke-dasharray: 5 5
    classDef external fill:#eceff1,stroke:#607d8b,color:#37474f
    classDef client fill:#ffebee,stroke:#b71c1c,color:#c62828
    classDef config fill:#eceff1,stroke:#ff6f00,color:#e65100,stroke-dasharray: 2 2
    classDef infra fill:#f1f8e9,stroke:#33691e,color:#558b2f,stroke-dasharray: 4 2
```
