webhttp

package
v0.0.0-...-82364ab Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2020 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package webhttp is a generated GoMock package.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingRequired = errors.New("missing required")
	ErrBadFormat       = errors.New("bad format")
)
View Source
var AcceptsJSON = RequestContentType("application/json; charset=utf-8")

AcceptsJSON verifies request has a 'content-type' header with 'application/json' mime type.

View Source
var ProducesJSON = ResponseContentType("application/json; charset=utf-8")

ProducesJSON sets response header 'content-type' to with 'application/json' mime type.

Functions

func Decode

func Decode(reader io.Reader, dst interface{}) error

func Encode

func Encode(writer io.Writer, src interface{}) error

func InjectLogger

func InjectLogger(logger logging.Logger) func(http.Handler) http.Handler

InjectLogger returns a middleware function that injects a logger into request's context. It also propagates logger with a request unique sequence number, so all the logs for a particular request could be grouped together.

func LogRequest

func LogRequest() func(http.Handler) http.Handler

LogRequest returns a middleware function that logs each incoming request. TODO: make logging level configurable so we could control log severity for each baseHandler

func NewRouter

func NewRouter(logger logging.Logger) chi.Router

NewRouter returns initialized HTTP router. It sets up all required middlewares and bindings for endpoints.

func NewServer

func NewServer(hl http.Handler) *http.Server

func RequestContentType

func RequestContentType(contentType string) func(http.Handler) http.Handler

RequestContentType returns a middleware function that verifies request has `content-type` header and its media type is equal to passed in value.

func ResponseContentType

func ResponseContentType(contentType string) func(http.Handler) http.Handler

ResponseContentType returns a middleware function that sets passed in value as a `content-type` header to the HTTP response in case is not yet set.

func Serve

func Serve(ctx context.Context, logger logging.Logger, srv *http.Server, port int) error

func StartServer

func StartServer(l net.Listener, srv *http.Server) error

func StopServer

func StopServer(logger logging.Logger, srv *http.Server, gracePeriod time.Duration) error

func WriteError

func WriteError(w http.ResponseWriter, logger logging.Logger, err error)

WriteError sends an error response back to the client.

Types

type CreateShortenReq

type CreateShortenReq struct {
	URL string `json:"url"`
}

type ErrorResponse

type ErrorResponse struct {
	// StatusCode status code that should be returned.
	StatusCode int
	// Cause is an error that needs to be placed as a message describing what was wrong.
	Cause error
}

ErrorResponse aggregates error information into the struct and know how to send it back to the client.

func (ErrorResponse) Write

func (er ErrorResponse) Write(logger logging.Logger, w http.ResponseWriter)

type GetShortenResp

type GetShortenResp struct {
	ID   int64  `json:"id"`
	URL  string `json:"url"`
	Hash string `json:"hash"`
}

type InfoHandler

type InfoHandler struct{}

InfoHandler handles requests about service status.

func (InfoHandler) Liveness

func (InfoHandler) Liveness(w http.ResponseWriter, _ *http.Request)

Liveness returns HTTP status `200` for each request. It is used to determine if the service is healthy.

func (InfoHandler) Readiness

func (InfoHandler) Readiness(w http.ResponseWriter, _ *http.Request)

Readiness returns HTTP status `200` for each request. It is used to determine if the service is ready to start receiving requests.

func (InfoHandler) Register

func (ih InfoHandler) Register(router chi.Router)

Register creates a binding between method handlers and endpoints.

func (InfoHandler) Version

func (InfoHandler) Version(w http.ResponseWriter, _ *http.Request)

Version return information about binary: version, commit sha, timestamp of compilation

type ListShortenResp

type ListShortenResp []GetShortenResp

type Mapper

type Mapper struct{}

type MockShortenService

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

MockShortenService is a mock of ShortenService interface

func NewMockShortenService

func NewMockShortenService(ctrl *gomock.Controller) *MockShortenService

NewMockShortenService creates a new mock instance

func (*MockShortenService) Create

func (m *MockShortenService) Create(ctx context.Context, entity shorten.Entity) (int64, error)

Create mocks base method

func (*MockShortenService) Delete

func (m *MockShortenService) Delete(ctx context.Context, id int64) error

Delete mocks base method

func (*MockShortenService) EXPECT

EXPECT returns an object that allows the caller to indicate expected use

func (*MockShortenService) Get

Get mocks base method

func (*MockShortenService) List

List mocks base method

func (*MockShortenService) Resolve

func (m *MockShortenService) Resolve(ctx context.Context, hash string) (string, error)

Resolve mocks base method

type MockShortenServiceMockRecorder

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

MockShortenServiceMockRecorder is the mock recorder for MockShortenService

func (*MockShortenServiceMockRecorder) Create

func (mr *MockShortenServiceMockRecorder) Create(ctx, entity interface{}) *gomock.Call

Create indicates an expected call of Create

func (*MockShortenServiceMockRecorder) Delete

func (mr *MockShortenServiceMockRecorder) Delete(ctx, id interface{}) *gomock.Call

Delete indicates an expected call of Delete

func (*MockShortenServiceMockRecorder) Get

func (mr *MockShortenServiceMockRecorder) Get(ctx, id interface{}) *gomock.Call

Get indicates an expected call of Get

func (*MockShortenServiceMockRecorder) List

func (mr *MockShortenServiceMockRecorder) List(ctx, pager interface{}) *gomock.Call

List indicates an expected call of List

func (*MockShortenServiceMockRecorder) Resolve

func (mr *MockShortenServiceMockRecorder) Resolve(ctx, hash interface{}) *gomock.Call

Resolve indicates an expected call of Resolve

type ParamInt64Opts

type ParamInt64Opts struct {
	P       ParamOpts
	Default int64
}

type ParamOpts

type ParamOpts struct {
	Optional bool
	Name     string
}

type Resolver

type Resolver interface {
	// Resolve returns a full URL accessioned with the hash.
	Resolve(ctx context.Context, hash string) (string, error)
}

type ResolverHandler

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

ResolverHandler redirects incoming requests by using request uri as a hash of the longer path.

func NewResolverHandler

func NewResolverHandler(resolver Resolver) ResolverHandler

func (ResolverHandler) Register

func (rh ResolverHandler) Register(router chi.Router)

func (ResolverHandler) Resolve

func (rh ResolverHandler) Resolve(w http.ResponseWriter, r *http.Request)

type ShortenHandler

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

ShortenHandler handles request for the user entity(-ies).

func NewShortenHandler

func NewShortenHandler(shortenService ShortenService) ShortenHandler

NewShortenHandler returns HTTP baseHandler initialized with provided service abstraction.

func (ShortenHandler) Create

func (uh ShortenHandler) Create(w http.ResponseWriter, r *http.Request)

func (ShortenHandler) Delete

func (uh ShortenHandler) Delete(w http.ResponseWriter, r *http.Request)

func (ShortenHandler) Get

func (ShortenHandler) List

func (ShortenHandler) Register

func (uh ShortenHandler) Register(router chi.Router)

Register creates a binding between method handlers and endpoints.

type ShortenService

type ShortenService interface {
	// Create creates a new shorten and returns its unique identifier.
	Create(ctx context.Context, entity shorten.Entity) (int64, error)
	// Get returns a single shorten by its unique identifier.
	Get(ctx context.Context, id int64) (shorten.Entity, error)
	// List returns subset of all shortens.
	List(ctx context.Context, pager shorten.Pager) ([]shorten.Entity, error)
	// Delete removes shorten by its unique identifier.
	Delete(ctx context.Context, id int64) error
	// Resolve returns a full URL accessioned with the hash.
	Resolve(ctx context.Context, hash string) (string, error)
}

ShortenService provides set of operations available to operate on the user entity.

Jump to

Keyboard shortcuts

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