profilesvc

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2026 License: MIT Imports: 16 Imported by: 0

README

profilesvc

This example demonstrates how to use Go kit to implement a REST-y HTTP service. It leverages the excellent gorilla mux package for routing.

Run the example with the optional port address for the service:

$ go run ./cmd/profilesvc/main.go -http.addr :8080
ts=2018-05-01T16:13:12.849086255Z caller=main.go:47 transport=HTTP addr=:8080

Create a Profile:

$ curl -d '{"id":"1234","Name":"Go Kit"}' -H "Content-Type: application/json" -X POST http://localhost:8080/profiles/
{}

Get the profile you just created

$ curl localhost:8080/profiles/1234
{"profile":{"id":"1234","name":"Go Kit"}}

目录结构

examples/profilesvc/
├── README.md          # 使用说明文档
├── client/client.go   # 服务客户端实现(含服务发现与负载均衡)
├── cmd/profilesvc/main.go  # 服务启动入口
├── service.go         # 业务逻辑定义(Profile CRUD接口)
├── endpoints.go       # 端点封装(服务与传输层桥接)
├── transport.go       # HTTP传输层实现
└── middlewares.go     # 中间件(日志)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInconsistentIDs = errors.New("inconsistent IDs")
	ErrAlreadyExists   = errors.New("already exists")
	ErrNotFound        = errors.New("not found")
)
View Source
var (
	// ErrBadRouting is returned when an expected path variable is missing.
	// It always indicates programmer error.
	ErrBadRouting = errors.New("inconsistent mapping between route and handler (programmer error)")
)

Functions

func MakeGetProfileEndpoint

func MakeGetProfileEndpoint(s Service) endpoint.Endpoint

MakeGetProfileEndpoint returns an endpoint via the passed service. Primarily useful in a server.

func MakeHTTPHandler

func MakeHTTPHandler(s Service, logger *log.Logger) http.Handler

MakeHTTPHandler mounts all of the service endpoints into an http.Handler. Useful in a profilesvc server.

func MakePostProfileEndpoint

func MakePostProfileEndpoint(s Service) endpoint.Endpoint

MakePostProfileEndpoint returns an endpoint via the passed service. Primarily useful in a server.

func MakePutProfileEndpoint

func MakePutProfileEndpoint(s Service) endpoint.Endpoint

MakePutProfileEndpoint returns an endpoint via the passed service. Primarily useful in a server.

Types

type Address

type Address struct {
	ID       string `json:"id"`
	Location string `json:"location,omitempty"`
}

Address is a field of a user profile. ID should be unique within the profile (at a minimum).

type Endpoints

type Endpoints struct {
	PostProfileEndpoint endpoint.Endpoint
	GetProfileEndpoint  endpoint.Endpoint
	PutProfileEndpoint  endpoint.Endpoint
}

Endpoints collects all of the endpoints that compose a profile service. It's meant to be used as a helper struct, to collect all of the endpoints into a single parameter.

In a server, it's useful for functions that need to operate on a per-endpoint basis. For example, you might pass an Endpoints to a function that produces an http.Handler, with each method (endpoint) wired up to a specific path. (It is probably a mistake in design to invoke the Service methods on the Endpoints struct in a server.)

In a client, it's useful to collect individually constructed endpoints into a single type that implements the Service interface. For example, you might construct individual endpoints using transport/http.NewClient, combine them into an Endpoints, and return it to the caller as a Service.

func MakeClientEndpoints

func MakeClientEndpoints(instance string) (Endpoints, error)

MakeClientEndpoints returns an Endpoints struct where each endpoint invokes the corresponding method on the remote instance, via a transport/http.Client. Useful in a profilesvc client.

func MakeServerEndpoints

func MakeServerEndpoints(s Service) Endpoints

MakeServerEndpoints returns an Endpoints struct where each endpoint invokes the corresponding method on the provided service. Useful in a profilesvc server.

func (Endpoints) GetProfile

func (e Endpoints) GetProfile(ctx context.Context, id string) (Profile, error)

GetProfile implements Service. Primarily useful in a client.

func (Endpoints) PostProfile

func (e Endpoints) PostProfile(ctx context.Context, p Profile) error

PostProfile implements Service. Primarily useful in a client.

func (Endpoints) PutProfile

func (e Endpoints) PutProfile(ctx context.Context, id string, p Profile) error

PutProfile implements Service. Primarily useful in a client.

type Middleware

type Middleware func(Service) Service

Middleware describes a service (as opposed to endpoint) middleware.

func LoggingMiddleware

func LoggingMiddleware(logger *log.Logger) Middleware

type Profile

type Profile struct {
	ID        string    `json:"id"`
	Name      string    `json:"name,omitempty"`
	Addresses []Address `json:"addresses,omitempty"`
}

Profile represents a single user profile. ID should be globally unique.

type Service

type Service interface {
	PostProfile(ctx context.Context, p Profile) error
	GetProfile(ctx context.Context, id string) (Profile, error)
	PutProfile(ctx context.Context, id string, p Profile) error
}

Service is a simple CRUD interface for user profiles.

func NewInmemService

func NewInmemService() Service

Directories

Path Synopsis
Package client provides a profilesvc client backed by Consul service discovery, round-robin load balancing, and automatic retry.
Package client provides a profilesvc client backed by Consul service discovery, round-robin load balancing, and automatic retry.
cmd
profilesvc command

Jump to

Keyboard shortcuts

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