greetapi

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 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 生成,编解码约定只有一份。装配形态:

reg.MountInternalService("/", greetapi.NewHTTPHandler(greetapi.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 NewSecureClient added in v0.12.0

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)

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