greetapi

package
v0.9.8 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	CodeGreetUnsupportedLang = "GREET_UNSUPPORTED_LANG"
)

错误码常量:错误身份 = 错误码,判定用 apperr.Is(err, Code...), 单体与微服务两种部署形态下行为一致。

Variables

View Source
var (
	ErrGreetUnsupportedLang = apperr.New(CodeGreetUnsupportedLang, 422, "不支持的问候语言")
)

错误 sentinel:不可变模板,可安全共享;附加上下文用 WithDetail/WithCause。

Functions

func NewHTTPHandler

func NewHTTPHandler(svc Service) http.Handler

NewHTTPHandler 把 Service 暴露为 HTTP:每方法一条 POST 路由,请求体 JSON decode,错误经 apperr.WriteProblem,成功 200 JSON。与 client.gen.go 由同一份 contract.yaml 生成,编解码约定只有一份。装配形态:

mux.Handle("/", greetapi.NewHTTPHandler(greetapi.WrapService(impl, 0)))

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

func NewClient(base, caller string, hc *http.Client) *Client

NewClient 返回契约 client。base 是服务根地址(如 "http://ledger:8080"), caller 填本服务名(callctx 白名单的归因值)。

白名单传播焊死在装配处:hc 为 nil 走默认 Transport;非 nil 时在其 Transport 外包一层 callctx.Transport(不改调用方的 client)。 「忘了装 Transport」这个静默失效形态在生成 client 里不存在。

func (*Client) Greet

func (c *Client) Greet(ctx context.Context, req GreetRequest) (GreetReply, error)

type GreetReply

type GreetReply struct {
	Message string `json:"message"`
}

GreetReply 是 Greet 的响应 DTO。

type GreetRequest

type GreetRequest struct {
	// Name 必填。
	Name string `json:"name"`
	Lang string `json:"lang"`
}

GreetRequest 是 Greet 的请求 DTO:契约边界传值、可序列化。

type Service

type Service interface {
	// Greet 打招呼。lang 空值等价 "en";不支持的语言返回 GREET_UNSUPPORTED_LANG。幂等:重复执行安全,client 会对可用性故障做有界重试。
	Greet(ctx context.Context, req GreetRequest) (GreetReply, error)
}

Service 是 greet 域的契约接口(事实源 contract.yaml)。进程内 wrapper 与远程 client 实现同一个接口,调用方无从(也不需要)分辨对端形态。

func WrapService

func WrapService(inner Service, timeout time.Duration) Service

WrapService 把实现包进契约边界,供 appkit.ProvideContract 使用; timeout <= 0 时使用 contract.DefaultTimeout。

Jump to

Keyboard shortcuts

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