Documentation
¶
Index ¶
- Constants
- Variables
- func NewHTTPHandler(svc Service) http.Handler
- type Client
- func (c *Client) Greet(ctx context.Context, req GreetRequest) (GreetReply, error)
- func (c *Client) Ping(ctx context.Context) error
- func (c *Client) Reset(ctx context.Context, req ResetRequest) error
- func (c *Client) Search(ctx context.Context, req SearchRequest) (SearchReply, error)
- func (c *Client) Stats(ctx context.Context) (StatsReply, error)
- type Entry
- type EntryPosted
- type EntryReversed
- type GreetReply
- type GreetRequest
- type ResetRequest
- type SearchReply
- type SearchRequest
- type Service
- type StatsReply
Constants ¶
const ( CodeLedgerInsufficientFunds = "LEDGER_INSUFFICIENT_FUNDS" CodeLedgerEntryNotFound = "LEDGER_ENTRY_NOT_FOUND" CodeLedgerDuplicateID = "LEDGER_DUPLICATE_ID" )
错误码常量:错误身份 = 错误码,判定用 apperr.Is(err, Code...), 单体与微服务两种部署形态下行为一致。
const TopicEntryPosted = "ledger.entry.posted"
TopicEntryPosted 是 EntryPosted 事件的 topic。
const TopicEntryReversed = "ledger.entry.reversed"
TopicEntryReversed 是 EntryReversed 事件的 topic。
Variables ¶
var ( ErrLedgerInsufficientFunds = apperr.New(CodeLedgerInsufficientFunds, 422, "insufficient funds") ErrLedgerEntryNotFound = apperr.New(CodeLedgerEntryNotFound, 404, "entry not found") ErrLedgerDuplicateID = apperr.New(CodeLedgerDuplicateID, 409, "duplicate entry id") )
错误 sentinel:不可变模板,可安全共享;附加上下文用 WithDetail/WithCause。
Functions ¶
func NewHTTPHandler ¶
NewHTTPHandler 把 Service 暴露为 HTTP:每方法一条 POST 路由,请求体 JSON decode,错误经 apperr.WriteProblem,成功 200 JSON。与 client.gen.go 由同一份 contract.yaml 生成,编解码约定只有一份。装配形态:
reg.MountInternalService("/", genfixture.NewHTTPHandler(genfixture.WrapService(impl, 0)))
生产必须配置严格 App 服务认证链并分类挂载;本 handler 只做编解码, 不验凭证。裸 handler 的 unsigned tenant/partition/caller 头不构成授权。
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client 是 greet 契约的远程绑定:与进程内 wrapper(wrap.gen.go)实现 同一个 Service 接口,方法体同样经 contract.Call——事务守卫、ctx 防火墙、 超时与错误规范化在两种部署形态下完全一致(DESIGN §5.3)。
func NewClient ¶
NewClient 返回契约 client。base 是服务根地址(如 "http://ledger:8080"), caller 填本服务名(callctx 白名单的归因值)。
这是兼容旧行为的 legacy/dev 入口,不提供服务认证或 HTTPS 强制; 生产服务调用使用 NewSecureClient。
白名单传播焊死在装配处:hc 为 nil 走默认 Transport;非 nil 时在其 Transport 外包一层 callctx.Transport(不改调用方的 client)。 「忘了装 Transport」这个静默失效形态在生成 client 里不存在。
func NewSecureClient ¶ added in v0.9.1
func NewSecureClient(base string, opts contract.SecureClientOptions) (*Client, error)
NewSecureClient 返回显式认证的 HTTPS 契约 client。Audience 与服务凭证 provider 必填;每次尝试取新凭证,拒绝过期凭证、不安全 transport 和重定向。 caller 由服务 JWT 的 subject 决定,不接受调用方伪造的 caller 或用户凭证。 Partition/TenantID 经 contract ctx 防火墙传给 provider,由其明确授权委托。
func (*Client) Greet ¶
func (c *Client) Greet(ctx context.Context, req GreetRequest) (GreetReply, error)
func (*Client) Search ¶
func (c *Client) Search(ctx context.Context, req SearchRequest) (SearchReply, error)
type Entry ¶
type Entry struct {
// EntryID 必填。
EntryID string `json:"entry_id"`
// Amount 是十进制数字符串:用 money.Parse 解析,全系统禁 float 金额。
Amount string `json:"amount"`
At time.Time `json:"at"`
}
Entry 是分录概要(List 类响应的元素类型;命名 DTO 嵌套封顶一层)。
type EntryPosted ¶
type EntryPosted struct {
EntryID string `json:"entry_id"`
// Amount 是十进制数字符串:用 money.Parse 解析,全系统禁 float 金额。
Amount string `json:"amount"`
At time.Time `json:"at"`
Attempts int64 `json:"attempts"`
Final bool `json:"final"`
}
EntryPosted 事件 payload(topic: ledger.entry.posted)。
func ParseEntryPosted ¶
func ParseEntryPosted(evt appkit.Event) (EntryPosted, error)
ParseEntryPosted 从 appkit.Event 还原 EntryPosted:校验 topic 与必填字段。
type EntryReversed ¶
EntryReversed 事件 payload(topic: ledger.entry.reversed)。
func ParseEntryReversed ¶
func ParseEntryReversed(evt appkit.Event) (EntryReversed, error)
ParseEntryReversed 从 appkit.Event 还原 EntryReversed:校验 topic 与必填字段。
type GreetReply ¶
type GreetReply struct {
Message string `json:"message"`
}
GreetReply 是 Greet 的响应 DTO。
type GreetRequest ¶
type GreetRequest struct {
// Name 必填。
Name string `json:"name"`
}
GreetRequest 是 Greet 的请求 DTO:契约边界传值、可序列化。
type ResetRequest ¶
type ResetRequest struct {
Scope string `json:"scope"`
}
ResetRequest 是 Reset 的请求 DTO:契约边界传值、可序列化。
type SearchReply ¶
SearchReply 是 Search 的响应 DTO。
type SearchRequest ¶
type SearchRequest struct {
Prefix string `json:"prefix"`
}
SearchRequest 是 Search 的请求 DTO:契约边界传值、可序列化。
type Service ¶
type Service interface {
// Greet 打招呼(req + resp 形态)。幂等:重复执行安全,client 会对可用性故障做有界重试。
Greet(ctx context.Context, req GreetRequest) (GreetReply, error)
// Stats 服务统计(无 req、有 resp 形态)。幂等:重复执行安全,client 会对可用性故障做有界重试。
Stats(ctx context.Context) (StatsReply, error)
// Reset 重置(有 req、仅 error 形态)。
Reset(ctx context.Context, req ResetRequest) error
// Ping 探活(仅 ctx、仅 error 形态)。幂等:重复执行安全,client 会对可用性故障做有界重试。
Ping(ctx context.Context) error
// Search 检索分录(命名 DTO 数组响应形态)。幂等:重复执行安全,client 会对可用性故障做有界重试。
Search(ctx context.Context, req SearchRequest) (SearchReply, error)
}
Service 是 greet 域的契约接口(事实源 contract.yaml)。进程内 wrapper 与远程 client 实现同一个接口,调用方无从(也不需要)分辨对端形态。
type StatsReply ¶
type StatsReply struct {
Served int64 `json:"served"`
}
StatsReply 是 Stats 的响应 DTO。