Documentation ¶
Index ¶
- Constants
- Variables
- func Configure(config *Config)
- func Logger() gin.HandlerFunc
- func LoggerWithWriter(out io.Writer, notlogged ...string) gin.HandlerFunc
- func Run() error
- func Shutdown()
- func WrapGin(parent context.Context, h Handler) gin.HandlerFunc
- type Config
- type DummyRequest
- func (r *DummyRequest) AddHeader(key, val string) *DummyRequest
- func (r *DummyRequest) AddPOSTParam(key, val string) *DummyRequest
- func (r *DummyRequest) Bind(out interface{}) error
- func (r *DummyRequest) ContentType() string
- func (r *DummyRequest) GetParam(key string) string
- func (r *DummyRequest) RawRequest() *http.Request
- func (r *DummyRequest) SetContentType(contentType string) *DummyRequest
- func (r *DummyRequest) SetJSONBody(p interface{}) *DummyRequest
- func (r *DummyRequest) SetMethod(method string) *DummyRequest
- func (r *DummyRequest) SetUser(user *model.User)
- func (r *DummyRequest) User() *model.User
- type Handler
- type Middleware
- type Request
- type Response
Constants ¶
const ( ContentTypeJSON = "application/json" ContentTypePostForm = "application/x-www-form-urlencoded" )
content types
Variables ¶
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 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...
Types ¶
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 Middleware ¶
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.