Skip to content

refactor: comprehensive project restructuring and layer abstraction#2

Merged
kyson-dev merged 23 commits into
mainfrom
dev
Mar 23, 2026
Merged

refactor: comprehensive project restructuring and layer abstraction#2
kyson-dev merged 23 commits into
mainfrom
dev

Conversation

@kyson-dev
Copy link
Copy Markdown
Owner

Summary

This PR implements a major architectural overhaul to improve maintainability, decouple components, and follow Go best practices (DDD-inspired layering).

Key Changes

  • Core Layering: Restructured internal/ into core, sys, proxy, and app layers.
  • Proxy Modules: Abstracted proxy configuration into dedicated modules (DNS, Inbounds, Outbounds, etc.).
  • Dependency Injection: Improved initialization and isolated sing-box dependencies.
  • Compatibility: Enhanced test coverage and ensured backward compatibility for sing-box v1.11.x config export.
  • Cleanup: Updated .gitignore to include refactor notes and temporary logs; removed unnecessary files.

Test Plan

  • Ran make test locally; all 35 tests passed across focused packages.
  • Manually verified CLI entry points and daemon IPC communication.

kyson-dev added 23 commits March 1, 2026 17:04
Phase 1 - Directory Restructuring:
  - cmd/main.go → cmd/sing-helm/main.go (Go standard practice)
  - internal/env/ → internal/platform/ (clearer naming)
  - internal/runtime/ → internal/model/ (avoid stdlib conflict)
  - internal/client/ → internal/clashapi/ (explicit purpose)

Phase 2 - Dependency Injection Foundation:
  - Add internal/app/app.go with Application struct (DI container)
  - Add platform.Resolve() pure function (no global state)
  - Add model.SaveStateTo/LoadStateFrom (path as parameter)
  - Wire Application into CLI via context (AppFromContext)
  - Add logger.GetInstance() for DI consumers

Phase 3 - Anti-Corruption Layer:
  - Merge internal/config/ + internal/service/ → internal/engine/
  - Move tools/exporter → internal/engine/exporter.go
  - All sing-box imports now isolated to engine/ package
  - Future sing-box upgrades only require changes in engine/

Phase 4 - Daemon Slimming:
  - Split daemon.go (578 lines) into focused files:
    - daemon.go (~200 lines): core struct, Serve, Handle router
    - handler_run.go: run/reload logic
    - handler_node.go: node management via Clash API
    - handler_mode.go: proxy/route mode switching
    - handler_status.go: status/health/log/stop/reload
Address secondary review findings:
- Delete redundant `controller` package, route TUI IPC calls directly
- Consolidate duplicated `asInt` implementations to `ipc.AsInt`
- Eliminate `internal/pkg` anti-pattern (move `netutil` to `platform`)
- Merge `cli/utils.go` into `dispatcher.go`
- Extract 366-line `engine/exporter.go` to independent `internal/export` package
- Split 350-line `cli/config.go` by extracting operations to `config_ops.go`
- Rename files for better clarity (`lifecycle`->`status`, `type`->`options`, `singbox`->`instance`, etc.)
Create `internal/engine/config` for core building logic (builder, loader, types), and `internal/engine/module` for all `ConfigModule` implementations (tun, route, experimental, outbound, etc.).
Move high level exported API to `internal/engine/engine.go` to act as the primary facade, eliminating Go circular imports.
Resolve chaotic top-level "internal/" coupling by introducing strict vertical layering:
- `internal/core`: Isolated business definitions (model, version)
- `internal/sys`: System abstractions (sys/env, logger, ipc)
- `internal/proxy`: Proxy domain boundaries (engine, export, subscription, clashapi)
- `internal/app`: Application entrypoints & orchestration (cli, daemon, tui, container)
- Move `internal/sys/env/lock.go` to `internal/sys/lock/lock.go`
- Rename `internal/sys/env` to `internal/sys/paths` since it mainly models and resolves the application paths instead of generic environments
- moving daemon background state and file reading into app/daemon/meta.go
- migrating cli bootstrap into app/cli/setup.go
- keeping sys/paths purely a utility package for static path computations without JSON serialization logic
…ckage and update CLI and daemon for new structure.
…sing logic to a dedicated subscription package and consolidating module utilities under `node` and `utils` subdirectories.
…ig/model`, and refactor subscription storage to use JSON files.
… module, enhance node processing with improved deduplication and detour resolution, and add comprehensive test coverage across various modules.
…tibility and ensure DNS hijack rules precede private direct rules.
…port, removing generic version compatibility.
- Added refactor.md, serve_log.txt, .gemini/ and gha-creds-*.json to .gitignore
- Removed serve_log.txt from the repository
@kyson-dev kyson-dev merged commit 0b1e05d into main Mar 23, 2026
2 checks passed
@kyson-dev kyson-dev deleted the dev branch March 23, 2026 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant