freedom

package module
v1.9.11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 16, 2025 License: Apache-2.0 Imports: 13 Imported by: 8

README

Freedom DDD 框架

License Go Report CardGoDoc GitHub release

简介

Freedom 是一个基于六边形架构(Hexagonal Architecture)的 Go 语言框架,专注于支持领域驱动设计(DDD)开发范式。本框架提供了完整的基础设施和工具链,帮助开发者构建可维护、可扩展的企业级应用。

核心特性

架构支持
  • 完整实现六边形架构(端口和适配器模式)
  • 领域驱动设计(DDD)最佳实践支持
  • 依赖注入(DI)和依赖倒置原则(DIP)
  • 完全符合开闭原则的插件化设计
框架集成
  • 无缝集成 Iris Web 框架
  • 支持 HTTP/H2C 服务端和客户端
  • 内置 Prometheus 监控集成
  • 支持 AOP(面向切面编程)
  • 基于 Worker 的无侵入 Context 设计
领域模型支持
  • 聚合根(Aggregate Root)实现
  • 领域事件(Domain Events)支持
  • CQS(命令查询分离)模式
  • 实体(Entity)和值对象(Value Object)支持
数据处理
  • 自动化 CRUD 操作生成
  • PO(持久化对象)代码生成器
  • 多级缓存架构
    • 一级缓存(内存)
    • 二级缓存(分布式)
    • 缓存击穿防护
消息和事件
  • 集成消息队列组件
  • 领域事件发布订阅
  • 事件驱动架构支持

快速开始

安装框架
# 安装 Freedom 命令行工具
$ go install github.com/8treenet/freedom/freedom@latest

# 验证安装
$ freedom version
创建新项目
# 创建项目
$ freedom new-project [项目名称]

# 进入项目目录
$ cd [项目名称]

# 安装依赖
$ go mod tidy

# 运行服务
$ go run main.go
代码生成工具
# 生成数据库相关代码(支持两种方式)

# 1. 通过数据库连接生成
$ freedom new-po --dsn "root:密码@tcp(127.0.0.1:3306)/数据库名?charset=utf8"

# 2. 通过 JSON Schema 生成
$ freedom new-po --json ./domain/po/schema.json

# 查看更多生成选项
$ freedom new-po -h

指南

核心文档
  1. 路由指南
    • HTTP 路由配置
    • API 设计规范
  2. 服务指南
    • 服务层设计原则
    • 业务逻辑实现指南
  3. 持久化对象指南
    • PO 对象使用说明
    • 数据库操作最佳实践
  4. HTTP 客户端指南
    • HTTP 客户端配置
    • 请求处理最佳实践
  5. DDD 指南
    • 领域驱动设计实践指南
    • 架构设计原则
  6. Worker 指南
    • Worker 机制详解
    • Context 使用说明

学习资源

示例项目
  1. 基础教程

    • DDD 基础概念实践
    • 框架基本功能使用
  2. HTTP2 示例

    • HTTP2 服务配置
    • 依赖倒置实现
  3. 基础设施示例

    • 事务组件实现
    • 自定义组件开发
    • Kafka 集成
    • 领域事件使用
  4. 电商系统示例

    • 完整的电商领域实现
    • CQS 模式示例
    • 聚合根设计
    • 实体和值对象使用
    • 领域事件实践
    • 资源库模式
    • 基础设施层集成

贡献指南

欢迎提交 Issue 和 Pull Request 来帮助改进 Freedom 框架。在提交代码前,请确保:

  • 代码符合 Go 语言规范
  • 添加了必要的测试用例
  • 更新了相关文档

开源协议

本项目采用 Apache 2.0 开源协议。详见 LICENSE 文件。

Documentation

Index

Constants

View Source
const (
	//TransactionKey for transaction DB handles in the first-level cache
	TransactionKey = internal.TransactionKey
)

Variables

View Source
var (

	// EnvProfileDir is the name of the environment variable for the search
	// directory of the profile
	EnvProfileDir = "FREEDOM_PROJECT_CONFIG"

	// ProfileENV is the name of the profile directory in environment variable
	ProfileENV = EnvProfileDir
)

Functions

func Configure added in v0.0.3

func Configure(obj interface{}, file string, metaData ...interface{}) error

Configure .

func DefaultConfiguration added in v1.6.9

func DefaultConfiguration() iris.Configuration

DefaultConfiguration Proxy a call to iris.DefaultConfiguration

func IsDir added in v1.8.8

func IsDir(dir string) bool

IsDir accepts a string with a directory path and tests the path. It returns true if the path exists and it is a directory, and false otherwise.

func IsFile added in v1.8.8

func IsFile(path string) bool

IsFile accepts a string with a file path and tests the path. It returns true if the path exists and it is a file, and false otherwise.

func JoinPath added in v1.8.8

func JoinPath(elems ...string) string

JoinPath returns a string that joins any number of path elements into a single path, separating them with slashes.

func Logger

func Logger() *golog.Logger

Logger Return to global logger.

func Prepare added in v1.6.2

func Prepare(f func(Initiator))

Prepare A prepared function is passed in for initialization.

func ProfileDirFromEnv added in v1.8.8

func ProfileDirFromEnv() string

ProfileDirFromEnv reads from environment variable with EnvProfileDir

func Prometheus added in v1.5.5

func Prometheus() *internal.Prometheus

Prometheus Return prometheus.

func ReadProfile added in v1.8.8

func ReadProfile(file string, v interface{}) error

ReadProfile accepts a string with the name of a profile file, and search the file by detectProfilePath. It will fill v with the configuration by parsing the profile into toml format, and returns nil if the file has found. It returns error, if the file has not been found or any error encountered.

func ServiceLocator added in v1.8.8

func ServiceLocator() *internal.ServiceLocatorImpl

ServiceLocator Return serviceLocator. Use the service locator to get the service.

func SetConfigurator added in v1.8.8

func SetConfigurator(c Configurator)

SetConfigurator assigns a Configurator to global configurator

func SetConfigurer added in v1.6.9

func SetConfigurer(c Configurer)

SetConfigurer assigns a Configurator to global configurator

Types

type Application

type Application interface {
	// Install the database. A function that returns the Interface type is passed in as a handle to the database.
	InstallDB(f func() (db interface{}))
	// Install the redis. A function that returns the redis.Cmdable is passed in as a handle to the client.
	InstallRedis(f func() (client redis.Cmdable))
	// Install a custom data source. A function that returns the Interface type is passed in as a handle to the custom data sources.
	InstallCustom(f func() interface{})
	// Install the middleware for http routing globally.
	InstallMiddleware(handler iris.Handler)
	// Assigns specified prefix to the application-level router.
	// Because of ambiguous naming, I've been create SetPrefixPath as an alternative.
	// Considering remove this function in the future.
	InstallParty(relativePath string)
	// NewRunner Can be used as an argument for the `Run` method.
	NewRunner(addr string, configurators ...host.Configurator) iris.Runner
	// NewH2CRunner Create a Runner for H2C .
	NewH2CRunner(addr string, configurators ...host.Configurator) iris.Runner
	// NewAutoTLSRunner Can be used as an argument for the `Run` method.
	NewAutoTLSRunner(addr string, domain string, email string, configurators ...host.Configurator) iris.Runner
	// NewTLSRunner Can be used as an argument for the `Run` method.
	NewTLSRunner(addr string, certFile, keyFile string, configurators ...host.Configurator) iris.Runner
	// Go back to the iris app.
	Iris() *iris.Application
	// Return to global logger.
	Logger() *golog.Logger
	// Run serving an iris application as the HTTP server to handle the incoming requests
	Run(serve iris.Runner, c iris.Configuration)
	// Adds a builder function which builds a BootManager.
	BindBooting(f func(bootManager BootManager))
	// Install the middleware for the message bus.
	InstallBusMiddleware(handle ...BusHandler)
	InstallSerializer(marshal func(v interface{}) ([]byte, error), unmarshal func(data []byte, v interface{}) error)
}

Application Represents an application of freedom

func NewApplication added in v1.0.5

func NewApplication() Application

NewApplication Create an application.

type BeforeActivation added in v1.7.1

type BeforeActivation = IrisBeforeActivation

BeforeActivation is Is the start-up pre-processing of the action.

type BootManager added in v1.8.9

type BootManager = internal.BootManager

BootManager Can be used to launch in the application.

type Bus added in v1.6.2

type Bus = internal.Bus

Bus Message bus, using http header to pass through data.

type BusHandler added in v1.6.2

type BusHandler = internal.BusHandler

BusHandler is the bus message middleware type.

type Configuration added in v1.6.9

type Configuration = IrisConfiguration

Configuration is the configuration type of the app.

type Configurator added in v1.8.8

type Configurator interface {
	Configure(obj interface{}, file string, metaData ...interface{}) error
}

Configurator .

type Configurer added in v1.6.9

type Configurer = Configurator

Configurer .

type Context added in v1.3.0

type Context = IrisContext

Context is the context type.

type DomainEvent added in v1.8.6

type DomainEvent = internal.DomainEvent

DomainEvent Interface definition of domain events for subscription and publishing of entities.

type Entity added in v1.3.7

type Entity = internal.Entity

Entity is the entity's father interface.

type Handler added in v1.9.7

type Handler = iris.Handler

type Infra added in v1.3.0

type Infra = internal.Infra

Infra The parent class of the infrastructure, which can be inherited using the parent class's methods.

type Initiator

type Initiator = internal.Initiator

Initiator Initialize the binding relationship between the user's code and the associated code.

type IrisBeforeActivation added in v1.8.8

type IrisBeforeActivation = mvc.BeforeActivation

IrisBeforeActivation represents an type alias to mvc.BeforeActivation

type IrisConfiguration added in v1.8.8

type IrisConfiguration = iris.Configuration

IrisConfiguration represents an type alias to iris.Configuration

type IrisContext added in v1.8.8

type IrisContext = iris.Context

IrisContext represents an type alias to iris.Context

type IrisResult added in v1.8.8

type IrisResult = hero.Result

IrisResult represents an type alias to hero.Result

type LogFields added in v1.7.16

type LogFields = golog.Fields

LogFields is the column type of the log.

type LogRow added in v1.7.16

type LogRow = golog.Log

LogRow is the log per line callback.

type Repository

type Repository = internal.Repository

Repository The parent class of the repository, which can be inherited using the parent class's methods.

type Result added in v1.3.0

type Result = IrisResult

Result is the controller return type.

type UnitTest added in v1.4.0

type UnitTest = internal.UnitTest

UnitTest is a unit test tool.

func NewUnitTest added in v1.4.0

func NewUnitTest() UnitTest

NewUnitTest Unit testing tools.

type Worker added in v1.7.1

type Worker = internal.Worker

Worker Request a runtime object.

func ToWorker added in v1.7.1

func ToWorker(ctx Context) Worker

ToWorker proxy a call to WorkerFromCtx.

func WorkerFromCtx added in v1.8.8

func WorkerFromCtx(ctx Context) Worker

WorkerFromCtx extracts Worker from a Context.

Directories

Path Synopsis
example
base command
Package main code generated by 'freedom new-project base'
Package main code generated by 'freedom new-project base'
base/adapter/repository
Package repository code generated by 'freedom new-project base'
Package repository code generated by 'freedom new-project base'
base/domain
Package domain generated by 'freedom new-project base'
Package domain generated by 'freedom new-project base'
base/domain/po
Package po generated by 'freedom new-project base'
Package po generated by 'freedom new-project base'
base/domain/vo
Package vo generated by 'freedom new-project base'
Package vo generated by 'freedom new-project base'
base/infra
Package infra generated by 'freedom new-project base'
Package infra generated by 'freedom new-project base'
fshop command
Code generated by 'freedom new-project fshop'
Code generated by 'freedom new-project fshop'
fshop/domain/po
Package po generated by 'freedom new-po'
Package po generated by 'freedom new-po'
fshop/infra
Package infra code generated by 'freedom new-project base'
Package infra code generated by 'freedom new-project base'
http2 command
infra-example command
Code generated by 'freedom new-project infra-example'
Code generated by 'freedom new-project infra-example'
infra-example/domain/po
Package po generated by 'freedom new-po'
Package po generated by 'freedom new-po'
infra-example/domain/vo
Package vo code generated by 'freedom new-project infra-example'
Package vo code generated by 'freedom new-project infra-example'
infra-example/infra
Package infra code generated by 'freedom new-project base'
Package infra code generated by 'freedom new-project base'
cmd
infra

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL