Files
go-omni/cmd/go-omni/apigen/testdata/business/client.go

33 lines
929 B
Go

package svc
import (
"context"
"example.com/business/internal/middleware"
apierrors "git.yuntuo.hk/k2safe/go-omni/errors"
"git.yuntuo.hk/k2safe/go-omni/httpx"
"sync/atomic"
)
// A local fixture checks wiring; production authentication is project-owned.
type accessClient struct{ closed atomic.Bool }
func (c *accessClient) Close() error { c.closed.Store(true); return nil }
func (c *accessClient) CheckAccess(ctx context.Context, token string) error {
if c.closed.Load() {
return apierrors.New(503, "CLOSED", "closed")
}
if httpx.Metadata(ctx).Route == "" {
return apierrors.New(500, "NO_METADATA", "missing route")
}
if token == "reject" {
return apierrors.New(403, "DENIED", "denied")
}
return nil
}
func (c *ServiceContext) initAccess(ctx context.Context) error {
c.Client = &accessClient{}
c.OnClose(c.Client.Close)
c.Middlewares["RequireAccess"] = middleware.NewRequireAccess(c.Client)
return nil
}