example

package
v8.1.3+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2022 License: Apache-2.0 Imports: 19 Imported by: 91

Documentation

Index

Constants

View Source
const HaberdasherPathPrefix = "/twirp/twitch.twirp.example.Haberdasher/"

HaberdasherPathPrefix is a convenience constant that may identify URL paths. Should be used with caution, it only matches routes generated by Twirp Go clients, with the default "/twirp" prefix and default CamelCase service and method names. More info: https://twitchtv.github.io/twirp/docs/routing.html

Variables

View Source
var File_service_proto protoreflect.FileDescriptor

Functions

func WriteError

func WriteError(resp http.ResponseWriter, err error)

WriteError writes an HTTP response with a valid Twirp error format (code, msg, meta). Useful outside of the Twirp server (e.g. http middleware), but does not trigger hooks. If err is not a twirp.Error, it will get wrapped with twirp.InternalErrorWith(err)

Types

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient is the interface used by generated clients to send HTTP requests. It is fulfilled by *(net/http).Client, which is sufficient for most users. Users can provide their own implementation for special retry policies.

HTTPClient implementations should not follow redirects. Redirects are automatically disabled if *(net/http).Client is passed to client constructors. See the withoutRedirects function in this file for more details.

type Haberdasher

type Haberdasher interface {
	// MakeHat produces a hat of mysterious, randomly-selected color!
	MakeHat(context.Context, *Size) (*Hat, error)
}

A Haberdasher makes hats for clients.

func NewHaberdasherJSONClient

func NewHaberdasherJSONClient(baseURL string, client HTTPClient, opts ...twirp.ClientOption) Haberdasher

NewHaberdasherJSONClient creates a JSON client that implements the Haberdasher interface. It communicates using JSON and can be configured with a custom HTTPClient.

func NewHaberdasherProtobufClient

func NewHaberdasherProtobufClient(baseURL string, client HTTPClient, opts ...twirp.ClientOption) Haberdasher

NewHaberdasherProtobufClient creates a Protobuf client that implements the Haberdasher interface. It communicates using Protobuf and can be configured with a custom HTTPClient.

type Hat

type Hat struct {

	// The size of a hat should always be in inches.
	Size int32 `protobuf:"varint,1,opt,name=size,proto3" json:"size,omitempty"`
	// The color of a hat will never be 'invisible', but other than
	// that, anything is fair game.
	Color string `protobuf:"bytes,2,opt,name=color,proto3" json:"color,omitempty"`
	// The name of a hat is it's type. Like, 'bowler', or something.
	Name string `protobuf:"bytes,3,opt,name=name,proto3" json:"name,omitempty"`
	// contains filtered or unexported fields
}

A Hat is a piece of headwear made by a Haberdasher.

func (*Hat) Descriptor deprecated

func (*Hat) Descriptor() ([]byte, []int)

Deprecated: Use Hat.ProtoReflect.Descriptor instead.

func (*Hat) GetColor

func (x *Hat) GetColor() string

func (*Hat) GetName

func (x *Hat) GetName() string

func (*Hat) GetSize

func (x *Hat) GetSize() int32

func (*Hat) ProtoMessage

func (*Hat) ProtoMessage()

func (*Hat) ProtoReflect

func (x *Hat) ProtoReflect() protoreflect.Message

func (*Hat) Reset

func (x *Hat) Reset()

func (*Hat) String

func (x *Hat) String() string

type Size

type Size struct {
	Inches int32 `protobuf:"varint,1,opt,name=inches,proto3" json:"inches,omitempty"`
	// contains filtered or unexported fields
}

Size is passed when requesting a new hat to be made. It's always measured in inches.

func (*Size) Descriptor deprecated

func (*Size) Descriptor() ([]byte, []int)

Deprecated: Use Size.ProtoReflect.Descriptor instead.

func (*Size) GetInches

func (x *Size) GetInches() int32

func (*Size) ProtoMessage

func (*Size) ProtoMessage()

func (*Size) ProtoReflect

func (x *Size) ProtoReflect() protoreflect.Message

func (*Size) Reset

func (x *Size) Reset()

func (*Size) String

func (x *Size) String() string

type TwirpServer

type TwirpServer interface {
	http.Handler

	// ServiceDescriptor returns gzipped bytes describing the .proto file that
	// this service was generated from. Once unzipped, the bytes can be
	// unmarshalled as a
	// google.golang.org/protobuf/types/descriptorpb.FileDescriptorProto.
	//
	// The returned integer is the index of this particular service within that
	// FileDescriptorProto's 'Service' slice of ServiceDescriptorProtos. This is a
	// low-level field, expected to be used for reflection.
	ServiceDescriptor() ([]byte, int)

	// ProtocGenTwirpVersion is the semantic version string of the version of
	// twirp used to generate this file.
	ProtocGenTwirpVersion() string

	// PathPrefix returns the HTTP URL path prefix for all methods handled by this
	// service. This can be used with an HTTP mux to route Twirp requests.
	// The path prefix is in the form: "/<prefix>/<package>.<Service>/"
	// that is, everything in a Twirp route except for the <Method> at the end.
	PathPrefix() string
}

TwirpServer is the interface generated server structs will support: they're HTTP handlers with additional methods for accessing metadata about the service. Those accessors are a low-level API for building reflection tools. Most people can think of TwirpServers as just http.Handlers.

func NewHaberdasherServer

func NewHaberdasherServer(svc Haberdasher, opts ...interface{}) TwirpServer

NewHaberdasherServer builds a TwirpServer that can be used as an http.Handler to handle HTTP requests that are routed to the right method in the provided svc implementation. The opts are twirp.ServerOption modifiers, for example twirp.WithServerHooks(hooks).

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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