135 lines
3.1 KiB
Go
135 lines
3.1 KiB
Go
// Code generated by go-omni api. DO NOT EDIT.
|
|
package demo
|
|
|
|
import (
|
|
"context"
|
|
"example.com/managed/internal/svc"
|
|
"example.com/managed/internal/types"
|
|
"fmt"
|
|
"git.yuntuo.hk/k2safe/go-omni/contract"
|
|
"git.yuntuo.hk/k2safe/go-omni/httpx"
|
|
)
|
|
|
|
type Logic interface {
|
|
Check(ctx context.Context, req *types.CheckReq) (*types.Receipt, error)
|
|
Other(ctx context.Context, req *types.CheckReq) (*types.Receipt, error)
|
|
RawJSON(ctx context.Context) (*types.Receipt, error)
|
|
Upload(ctx context.Context, body *httpx.Body) (*types.Receipt, error)
|
|
Callback(ctx context.Context, body *httpx.Body) (*httpx.Stream, error)
|
|
Download(ctx context.Context) (*httpx.Stream, error)
|
|
Slow(ctx context.Context) (*types.Receipt, error)
|
|
}
|
|
type Handler struct {
|
|
logic Logic
|
|
deps *svc.ServiceContext
|
|
}
|
|
|
|
func New(s Logic, deps ...*svc.ServiceContext) *Handler {
|
|
h := &Handler{logic: s}
|
|
if len(deps) > 0 {
|
|
h.deps = deps[0]
|
|
}
|
|
return h
|
|
}
|
|
|
|
var checkRequest = contract.MustNew[types.CheckReq]()
|
|
|
|
func (h *Handler) Check(c *httpx.Context) error {
|
|
var req types.CheckReq
|
|
if err := checkRequest.Bind(c.Request(), httpx.RouteParams(c.Context()), &req); err != nil {
|
|
return err
|
|
}
|
|
resp, err := h.logic.Check(c.Context(), &req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if resp == nil {
|
|
return fmt.Errorf("Check returned a nil response")
|
|
}
|
|
return c.OK(resp)
|
|
}
|
|
|
|
var otherRequest = contract.MustNew[types.CheckReq]()
|
|
|
|
func (h *Handler) Other(c *httpx.Context) error {
|
|
var req types.CheckReq
|
|
if err := otherRequest.Bind(c.Request(), httpx.RouteParams(c.Context()), &req); err != nil {
|
|
return err
|
|
}
|
|
resp, err := h.logic.Other(c.Context(), &req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if resp == nil {
|
|
return fmt.Errorf("Other returned a nil response")
|
|
}
|
|
return c.OK(resp)
|
|
}
|
|
func (h *Handler) RawJSON(c *httpx.Context) error {
|
|
resp, err := h.logic.RawJSON(c.Context())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if resp == nil {
|
|
return fmt.Errorf("RawJSON returned a nil response")
|
|
}
|
|
return c.OK(resp)
|
|
}
|
|
func (h *Handler) Upload(c *httpx.Context) error {
|
|
body, err := httpx.NewBody(c.Request(), "multipart")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
resp, err := h.logic.Upload(c.Context(), body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if resp == nil {
|
|
return fmt.Errorf("Upload returned a nil response")
|
|
}
|
|
if err := body.Complete(); err != nil {
|
|
return err
|
|
}
|
|
return c.OK(resp)
|
|
}
|
|
func (h *Handler) Callback(c *httpx.Context) error {
|
|
body, err := httpx.NewBody(c.Request(), "raw")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
resp, err := h.logic.Callback(c.Context(), body)
|
|
if err != nil {
|
|
if resp != nil {
|
|
_ = resp.Close()
|
|
}
|
|
return err
|
|
}
|
|
if err := body.Complete(); err != nil {
|
|
if resp != nil {
|
|
_ = resp.Close()
|
|
}
|
|
return err
|
|
}
|
|
return c.WriteStream(200, "text/plain", resp)
|
|
}
|
|
func (h *Handler) Download(c *httpx.Context) error {
|
|
resp, err := h.logic.Download(c.Context())
|
|
if err != nil {
|
|
if resp != nil {
|
|
_ = resp.Close()
|
|
}
|
|
return err
|
|
}
|
|
return c.WriteStream(200, "application/octet-stream", resp)
|
|
}
|
|
func (h *Handler) Slow(c *httpx.Context) error {
|
|
resp, err := h.logic.Slow(c.Context())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if resp == nil {
|
|
return fmt.Errorf("Slow returned a nil response")
|
|
}
|
|
return c.OK(resp)
|
|
}
|