usersvc

package
v0.4.5 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2023 License: MIT Imports: 13 Imported by: 0

README

usersvc

This example illustrates how to apply Argument aggregation to struct fields.

Generate the code

$ go generate

Test the server

Run the server:

$ go run cmd/main.go
2021/11/14 18:13:15 transport=HTTP addr=:8080

Create a user:

$ curl -XPOST 'http://localhost:8080/users?name=Tracey&age=1'
{"Name":"Tracey","Age":1,"IP":"127.0.0.1"}

Create a user with the X-Forwarded-For header:

$ curl -H 'X-Forwarded-For: 192.168.0.1' -XPOST 'http://localhost:8080/users?name=Tracey&age=1'
{"Name":"Tracey","Age":1,"IP":"192.168.0.1"}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MakeEndpointOfCreateUser

func MakeEndpointOfCreateUser(s Service) endpoint.Endpoint

MakeEndpointOfCreateUser creates the endpoint for s.CreateUser.

func NewCodecs

func NewCodecs() *httpcodec.DefaultCodecs

func NewHTTPRouter

func NewHTTPRouter(svc Service, codecs httpcodec.Codecs, opts ...httpoption.Option) chi.Router

func OASv2APIDoc

func OASv2APIDoc(schema oas2.Schema) string

func ValidateCreateUserRequest

func ValidateCreateUserRequest(newSchema func(*CreateUserRequest) validating.Schema) httpoption.Validator

ValidateCreateUserRequest creates a validator for CreateUserRequest.

Types

type CreateUserRequest

type CreateUserRequest struct {
	User User `json:"-"`
}

type CreateUserResponse

type CreateUserResponse struct {
	Result User  `json:"result"`
	Err    error `json:"-"`
}

func (*CreateUserResponse) Body

func (r *CreateUserResponse) Body() interface{}

func (*CreateUserResponse) Failed

func (r *CreateUserResponse) Failed() error

Failed implements endpoint.Failer.

type HTTPClient

type HTTPClient struct {
	// contains filtered or unexported fields
}

func NewHTTPClient

func NewHTTPClient(codecs httpcodec.Codecs, httpClient *http.Client, baseURL string) (*HTTPClient, error)

func (*HTTPClient) CreateUser

func (c *HTTPClient) CreateUser(ctx context.Context, user User) (result User, err error)

type IPCodec

type IPCodec struct{}

IPCodec is used to encode and decode an IP. It can be reused wherever needed.

func (IPCodec) Decode

func (c IPCodec) Decode(in map[string][]string, out interface{}) error

func (IPCodec) Encode

func (c IPCodec) Encode(in interface{}) (out map[string][]string)

type Service

type Service interface {
	//kun:op POST /users
	//kun:param user
	//kun:success body=result
	CreateUser(ctx context.Context, user User) (result User, err error)
}

type User

type User struct {
	Name string
	Age  int
	IP   net.IP `kun:"in=header name=X-Forwarded-For, in=request name=RemoteAddr"`
}

type UserService

type UserService struct{}

func (*UserService) CreateUser

func (u *UserService) CreateUser(ctx context.Context, user User) (User, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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