restapi

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2018 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContentTypeJSON     = "application/json"
	ContentTypePostForm = "application/x-www-form-urlencoded"
)

content types

Variables

View Source
var Router *gin.Engine

Router is a gin.Engine type. This exported because we need to run a httptest server in integration test (main_test.go).

Functions

func Configure

func Configure(config *Config)

Configure will start a listener to HTTP.

func Logger

func Logger() gin.HandlerFunc

Logger instances a Logger middleware that will write the logs to gin.DefaultWriter. By default gin.DefaultWriter = os.Stdout.

func LoggerWithWriter

func LoggerWithWriter(out io.Writer, notlogged ...string) gin.HandlerFunc

LoggerWithWriter instance a Logger middleware with the specified writter buffer. Example: os.Stdout, a file opened in write mode, a socket...

func Run

func Run() error

Run will start the server

func Shutdown

func Shutdown()

Shutdown gracefully when some signal from OS tell that system should be down.

func WrapGin

func WrapGin(parent context.Context, h Handler) gin.HandlerFunc

WrapGin wraps a Handler and turns it into gin compatible handler This method should be called with a fresh ctx

Types

type Config

type Config struct {
	Address string
	Test    bool
}

Config is an configuration needed by this restapi Server to run.

type DummyRequest

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

DummyRequest is for testing purpose. So instead using gin context, it will use http.Request.

func (*DummyRequest) AddHeader

func (r *DummyRequest) AddHeader(key, val string) *DummyRequest

AddHeader add header on this request.

func (*DummyRequest) AddPOSTParam

func (r *DummyRequest) AddPOSTParam(key, val string) *DummyRequest

AddPOSTParam will parse the encoded body in method post into current body.

func (*DummyRequest) Bind

func (r *DummyRequest) Bind(out interface{}) error

Bind will bind the request parameter (query, post form or raw body) into out variable, which can be struct.

func (*DummyRequest) ContentType

func (r *DummyRequest) ContentType() string

ContentType will return the content type of the request.

func (*DummyRequest) GetParam

func (r *DummyRequest) GetParam(key string) string

GetParam will get url parameter, for example on URL /api/:user_name, we can get user_name value by calling GetParam("user_name").

func (*DummyRequest) RawRequest

func (r *DummyRequest) RawRequest() *http.Request

RawRequest will returns the raw request payload, so we gan get the header or etc here.

func (*DummyRequest) SetContentType

func (r *DummyRequest) SetContentType(contentType string) *DummyRequest

SetContentType set content type of this request.

func (*DummyRequest) SetJSONBody

func (r *DummyRequest) SetJSONBody(p interface{}) *DummyRequest

SetJSONBody set the raw body json request.

func (*DummyRequest) SetMethod

func (r *DummyRequest) SetMethod(method string) *DummyRequest

SetMethod set the HTTP method on this request.

func (*DummyRequest) SetUser

func (r *DummyRequest) SetUser(user *model.User)

SetUser sets the current user based on authentication token. This usually set in middleware.

func (*DummyRequest) User

func (r *DummyRequest) User() *model.User

User get current user of this request.

type Handler

type Handler func(context.Context, Request) Response

Handler represents an api handler

type Middleware

type Middleware func(Handler) Handler

Middleware represents what an handler function should be write.

func ChainMiddleware

func ChainMiddleware(mw ...Middleware) Middleware

ChainMiddleware is a middleware wrapper which implementing this idea https://hackernoon.com/simple-http-middleware-with-go-79a4ad62889b

type Request

type Request interface {
	ContentType() string
	Bind(out interface{}) error
	RawRequest() *http.Request
	GetParam(string) string

	/* helper methods */
	User() *model.User
	SetUser(*model.User)
}

Request represents an api request

func NewDummyRequest

func NewDummyRequest() Request

NewDummyRequest creates a new dummy request. This implements the Request interface.

type Response

type Response interface {
	StatusCode() int
	Body() ([]byte, error)
	Header() http.Header
	ContentType() string
}

Response represents an api response

Jump to

Keyboard shortcuts

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