Skip to main content

Architecture Design

Overall Architecture​

┌─────────────────────────────────────────────────────┐
│ Browser / Git Client │
└──────────┬──────────────────────┬───────────────────┘
│ HTTP (3001) │ HTTP/SSH (8081/2022)
▼ ▼
┌──────────────────┐ ┌──────────────────────────────┐
│ web (Next.js) │ │ server (Go + Fiber) │
│ ┌────────────┐ │ │ ┌──────────┐ ┌───────────┐ │
│ │ SSR/CSR │──┼────┼─▶│ Router │─▶│ Handler │ │
│ │ Chakra UI │ │ │ │ Middleware│ │ Service │ │
│ └────────────┘ │ │ └──────────┘ └─────┬─────┘ │
└──────────────────┘ │ │ │
│ ┌───────────────────▼──────┐ │
│ │ Repository (GORM) │ │
│ │ ┌─────────────────────┐ │ │
│ │ │ SQLite/MySQL/PG │ │ │
│ │ └─────────────────────┘ │ │
│ └───────────────────────────┘ │
│ ┌───────────────────────────┐ │
│ │ Git Service (go-git) │ │
│ │ ┌─────────────────────┐ │ │
│ │ │ repositories/ │ │ │
│ │ │ (bare git repos) │ │ │
│ │ └─────────────────────┘ │ │
│ └───────────────────────────┘ │
└──────────────────────────────┘

Backend Layering​

LayerResponsibilityDirectory
RouterRoute registration, middleware chaininternal/router/
HandlerHTTP request handling, parameter validation, response serializationinternal/handler/
ServiceBusiness logic, transaction orchestrationinternal/service/
RepositoryData access, GORM operationsinternal/repository/
ModelData model definitionsinternal/model/
MiddlewareAuthentication, CORS, rate limiting, logginginternal/middleware/
GitGit operation encapsulation (with input validation, repository locks)internal/git/
ContainerDependency injection containerinternal/container/

Git Security Layer​

The internal/git/ package provides all Git operations and includes built-in security mechanisms:

FileResponsibility
validation.goInput validation tools (IsValidRefName, ValidateRepoPath, IsValidCommitHash)
lock.goRepository-level concurrency locks (GetRepoLock)
branches.goBranch operations (with input validation and locks)
tags.goTag operations (with input validation and locks)
files.goFile operations (with path validation and locks)
commits.goCommit queries
diff.goDiff comparison

Dependency Injection​

All services are managed uniformly through container.Container, initialized at startup, and handlers obtain service instances through the container.

Database Migration​

Startup sequence:
InitGORMDB → AutoMigrate (create tables) → RunMigrations (data backfill)
  • AutoMigrate: GORM automatic table/column creation (idempotent)
  • RunMigrations: Versioned SQL migration (internal/database/migrations/*.up.sql)

Frontend Architecture​

Next.js App Router​

web/src/
├── app/ # Page routes
│ ├── [owner]/[repo]/ # Repository-related pages
│ ├── projects/[slug]/ # Project management pages
│ ├── admin/ # Admin dashboard
│ └── settings/ # User settings
├── components/ # Shared components
├── contexts/ # React Context (authentication, WebSocket, i18n)
├── lib/ # Utility functions, API client
└── api/ # API request encapsulation

API Client​

ApiClient in lib/api.ts uniformly handles:

  • Authentication token (localStorage + cookie)
  • Request/response interception
  • 401 automatic redirect to login
  • API base URL dynamic adaptation (browser hostname)

Internationalization​

Uses I18nContext + JSON locale files (src/locales/en.json / zh.json).