Documentation ¶
Index ¶
- Variables
- func Abbreviate(str string, maxWidth int) string
- func At(s []string, i int) string
- func ContainsFold(a, b string) bool
- func FirstWord(s string) string
- func GetRemoteAddress(r *http.Request) string
- func IPAddrFromRemoteAddr(s string) string
- func IfElse(ifCondition bool, ifValue, elseValue string) string
- func ImplSQLScanner(t reflect.Type) bool
- func ImplType(src, target reflect.Type) bool
- func IsQuerySQL(sql string) (string, bool)
- func IsWsRequest(url string) bool
- func LookupDriverName(driver driver.Driver) string
- func PeekBody(r *http.Request, maxSize int) []byte
- func PerformRequest(method, target string, fn http.Handler, fns ...RequestVarsFn) *httptest.ResponseRecorder
- func PutAttr(r *http.Request, key string, value interface{})
- func PutAttrMap(r *http.Request, attrs Attrs)
- func WriteJSON(w http.ResponseWriter, obj interface{}) error
- type Attrs
- type ContextKey
- type CtxVar
- type ExecResult
- type GinRouter
- func (r *GinRouter) Any(relativePath string, handler gin.HandlerFunc, options ...OptionFn) *GinRouter
- func (r *GinRouter) Group(groupPath string, handlers ...gin.HandlerFunc) *GinRouterGroup
- func (r *GinRouter) Handle(httpMethod, relativePath string, handler gin.HandlerFunc, options ...OptionFn) *GinRouter
- func (r *GinRouter) RegisterCtler(ctler interface{})
- func (r *GinRouter) Run(addr ...string) (err error)
- func (r *GinRouter) ServeHTTP(w http.ResponseWriter, req *http.Request)
- type GinRouterGroup
- type GinRouterGroupFn
- type HandlerFuncAware
- type JSON
- type Log
- type LogrusStore
- type MapMapping
- type MapPreparer
- type Mapping
- type Metrics
- type MiniDB
- type Mux
- type MuxOption
- type MuxOptionFn
- type NullAny
- type Option
- type OptionFn
- type OptionFns
- type OptionHolder
- type Preparer
- type RequestVars
- type RequestVarsFn
- type RequestVarsFns
- type RouterFn
- type SQLExec
- type SQLRun
- type SQLStore
- type Store
- type Stores
- type StructMapping
- type StructPreparer
- type TableCol
Constants ¶
This section is empty.
Variables ¶
var ( JSONUnmarshal = jsoniter.Unmarshal JSONMarshal = jsoniter.Marshal JSONMarshalIndent = jsoniter.MarshalIndent )
nolint
Functions ¶
func Abbreviate ¶
Abbreviate abbreviates a string using ellipses.
func ContainsFold ¶
ContainsFold tell if a contains b in case-insensitively.
func GetRemoteAddress ¶
GetRemoteAddress returns ip address of the client making the request, taking into account http proxies.
func IPAddrFromRemoteAddr ¶
IPAddrFromRemoteAddr parses the IP Address. Request.RemoteAddress contains port, which we want to remove i.e.: "[::1]:58292" => "[::1]".
func ImplSQLScanner ¶
ImplSQLScanner tells t whether it implements sql.Scanner interface.
func IsQuerySQL ¶
IsQuerySQL tests a sql is a query or not.
func IsWsRequest ¶
IsWsRequest return true if this request is a websocket request.
func LookupDriverName ¶
LookupDriverName get driverName from the driver instance. The database/sql API doesn't provide a way to get the registry name for a driver from the driver type. from https://github.com/golang/go/issues/12600
func PerformRequest ¶
func PerformRequest(method, target string, fn http.Handler, fns ...RequestVarsFn) *httptest.ResponseRecorder
PerformRequest performs a test request. from https://github.com/gin-gonic/gin/issues/1120.
func PutAttrMap ¶
PutAttrMap put an attribute map into the Attributes in the context.
func WriteJSON ¶
func WriteJSON(w http.ResponseWriter, obj interface{}) error
WriteJSON marshals the given interface object and writes it with custom ContentType.
Types ¶
type Attrs ¶
type Attrs map[string]interface{}
Attrs carries map. It implements value for that key and delegates all other calls to the embedded Context.
func ParseAttrs ¶
ParseAttrs returns the attributes map from the request context.
type ContextKey ¶
type ContextKey int
ContextKey defines the context key type.
const ( // CtxKey defines the context key for CtxVar. CtxKey ContextKey = iota )
type ExecResult ¶
type ExecResult struct { Error error CostTime time.Duration Headers []string Rows interface{} // [][]string or []YourStruct RowsCount int RowsAffected int64 LastInsertID int64 IsQuery bool }
ExecResult defines the result structure of sql execution.
func (ExecResult) StringRows ¶
func (r ExecResult) StringRows() [][]string
StringRows return the string rows when using MapPreparer.
type GinRouter ¶
type GinRouter struct { *gin.Engine // XXX is a shortcut for router.Handle("XXX", path, handle). POST, GET, DELETE, PATCH, PUT, OPTIONS, HEAD RouterFn // contains filtered or unexported fields }
GinRouter defines adaptor routes implementation for IRoutes.
func NewGin ¶
func NewGin(router *gin.Engine, store Store, muxOptions ...MuxOptionFn) *GinRouter
NewGin wraps a new GinRouter for the gin router.
func (*GinRouter) Any ¶
func (r *GinRouter) Any(relativePath string, handler gin.HandlerFunc, options ...OptionFn) *GinRouter
Any registers a route that matches all the HTTP methods. GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE.
func (*GinRouter) Group ¶
func (r *GinRouter) Group(groupPath string, handlers ...gin.HandlerFunc) *GinRouterGroup
Group creates a new router group. You should add all the routes that have common middlewares or the same path prefix. For example, all the routes that use a common middleware for authorization could be grouped.
func (*GinRouter) Handle ¶
func (r *GinRouter) Handle(httpMethod, relativePath string, handler gin.HandlerFunc, options ...OptionFn) *GinRouter
Handle registers a new request handle and middleware with the given path and method. The last handler should be the real handler, the other ones should be middleware that can and should be shared among different routes. See the example code in GitHub.
For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.
This function is intended for bulk loading and to allow the usage of less frequently used, non-standardized or custom methods (e.g. for internal communication with a proxy).
func (*GinRouter) RegisterCtler ¶
func (r *GinRouter) RegisterCtler(ctler interface{})
RegisterCtler registers a controller object which declares the router in the structure fields' tag.
type GinRouterGroup ¶
type GinRouterGroup struct { *gin.RouterGroup GinRouter *GinRouter // XXX is a shortcut for router.Handle("XXX", path, handle). POST, GET, DELETE, PATCH, PUT, OPTIONS, HEAD GinRouterGroupFn }
GinRouterGroup wraps the gin.RouterGroup.
type GinRouterGroupFn ¶
type GinRouterGroupFn func(relativePath string, handler gin.HandlerFunc, options ...OptionFn) *GinRouterGroup
GinRouterGroupFn defines the prototype for function gin Handle group.
type HandlerFuncAware ¶
type HandlerFuncAware interface {
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
}
HandlerFuncAware declares interface which holds the HandleFunc function.
type JSON ¶
type JSON struct {
Data interface{}
}
JSON contains the given interface object.
func (JSON) Render ¶
func (r JSON) Render(w http.ResponseWriter) error
Render (JSON) writes rowsData with custom ContentType.
func (JSON) WriteContentType ¶
func (r JSON) WriteContentType(w http.ResponseWriter)
WriteContentType (JSON) writes JSON ContentType.
type Log ¶
type Log struct { ID string Biz string // Method is GET etc. Method string URL string IPAddr string RspHeader http.Header ReqBody string // RspStatus, like 200, 404. RspStatus int // ReqHeader records the response header. ReqHeader http.Header // RespSize is number of bytes of the response sent. RespSize int64 // RspBody is the response body(limit to 1000). RspBody string Created time.Time // Start records the start time of the request. Start time.Time // End records the end time of the request. End time.Time // Duration means how long did it take to. Duration time.Duration Attrs Attrs Option *Option PathParams httprouter.Params Request *http.Request }
Log describes info about HTTP request.
type LogrusStore ¶
type LogrusStore struct{}
LogrusStore stores the log as logurs info.
func (*LogrusStore) Store ¶
func (s *LogrusStore) Store(log *Log)
Store stores the log in database like MySQL, InfluxDB, and etc.
type MapMapping ¶
type MapMapping struct {
// contains filtered or unexported fields
}
MapMapping maps the query rows to maps.
func (*MapMapping) RowsData ¶
func (m *MapMapping) RowsData() interface{}
RowsData returns the mapped rows data.
func (*MapMapping) Scan ¶
func (m *MapMapping) Scan(rowNum int) error
Scan scans the rows one by one.
type MapPreparer ¶
type MapPreparer struct { // NullReplace is the replacement of null values. NullReplace string }
MapPreparer prepares to scan query rows.
func NewMapPreparer ¶
func NewMapPreparer(nullReplace string) *MapPreparer
NewMapPreparer creates a new MapPreparer.
type Metrics ¶
type Metrics struct { // Code is the first http response code passed to the WriteHeader func of // the ResponseWriter. If no such call is made, a default code of 200 is // assumed instead. Code int Start time.Time End time.Time // Duration is the time it took to execute the handler. Duration time.Duration // Written is the number of bytes successfully written by the Write or // ReadFrom function of the ResponseWriter. ResponseWriters may also write // rowsData to their underlying connection directly (e.g. headers), but those // are not tracked. Therefore the number of Written bytes will usually match // the size of the response body. Written int64 RespBody string Header http.Header }
Metrics holds metrics captured from CaptureMetrics.
func CaptureMetrics ¶
CaptureMetrics wraps the given hnd, executes it with the given w and r, and returns the metrics it captured from it.
func CaptureMetricsFn ¶
func CaptureMetricsFn(w http.ResponseWriter, fn func(http.ResponseWriter)) Metrics
CaptureMetricsFn wraps w and calls fn with the wrapped w and returns the resulting metrics. This is very similar to CaptureMetrics (which is just sugar on top of this func), but is a more usable interface if your application doesn't use the Go http.Handler interface.
type MiniDB ¶
type MiniDB interface { // Exec executes update. Exec(query string, args ...interface{}) (sql.Result, error) // Query performs query. Query(query string, args ...interface{}) (*sql.Rows, error) }
MiniDB wraps Exec method.
type Mux ¶
type Mux struct {
// contains filtered or unexported fields
}
Mux defines the wrapper of http.ServeMux.
func NewMux ¶
func NewMux(handler http.Handler, store Store, muxOptions ...MuxOptionFn) *Mux
NewMux returns a new instance of Mux.
func (*Mux) HandleFunc ¶
func (mux *Mux) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request), options ...OptionFn)
HandleFunc registers the handler function for the given pattern.
func (*Mux) ParseOption ¶
func (mux *Mux) ParseOption(r *http.Request) *OptionHolder
type MuxOption ¶
type MuxOption struct {
IgnoreBizNoname bool
}
MuxOption defines the option of mux.
type MuxOptionFn ¶
type MuxOptionFn func(m *MuxOption)
MuxOptionFn defines the function prototype to seting MuxOption.
func IgnoreBizNoname ¶
func IgnoreBizNoname(ignore bool) MuxOptionFn
IgnoreBizNoname set the ignoreBizNoname option.
type NullAny ¶
NullAny represents any that may be null. it implements the Scanner interface so it can be used as a scan destination.
func (*NullAny) Scan ¶
Scan assigns a value from a database driver.
The src value will be of one of the following types:
int64 float64 bool []byte string time.Time nil - for NULL values
An error should be returned if the value cannot be stored without loss of information.
Reference types such as []byte are only valid until the next call to Scan and should not be retained. Their underlying memory is owned by the driver. If retention is necessary, copy their values before the next call to Scan.
type OptionFn ¶
type OptionFn func(option *Option)
OptionFn defines the option function prototype.
type OptionFns ¶
type OptionFns []OptionFn
OptionFns defines the slice of OptionFns.
func (OptionFns) CreateOption ¶
CreateOption returns the option after functions call.
type OptionHolder ¶
type OptionHolder struct {
// contains filtered or unexported fields
}
OptionHolder defines the holder for option and params.
func (OptionHolder) Header ¶
func (ho OptionHolder) Header() http.Header
Header returns the header map that will be sent by WriteHeader.
func (OptionHolder) Write ¶
func (ho OptionHolder) Write([]byte) (int, error)
Write writes the data to the connection as part of an HTTP reply.
func (OptionHolder) WriteHeader ¶
func (ho OptionHolder) WriteHeader(int)
WriteHeader sends an HTTP response header with the provided status code.
type Preparer ¶
type Preparer interface { // Prepare prepares to scan query rows. Prepare(rows *sql.Rows, columns []string) Mapping }
Preparer prepares to scan query rows.
type RequestVars ¶
RequestVars defines the structure of request vars tha can be set.
type RequestVarsFn ¶
type RequestVarsFn func(r *RequestVars)
RequestVarsFn defines the prototype of RequestVars option setting function.
func JSONVar ¶
func JSONVar(obj interface{}) RequestVarsFn
JSONVar creates a new JSON RequestVarsFn.
type RequestVarsFns ¶
type RequestVarsFns []RequestVarsFn
RequestVarsFns is the slice of RequestVarsFn.
func (RequestVarsFns) Create ¶
func (fns RequestVarsFns) Create() *RequestVars
Create creates new RequestVars.
type RouterFn ¶
type RouterFn func(relativePath string, handler gin.HandlerFunc, options ...OptionFn) *GinRouter
RouterFn defines the prototype for function gin Handle.
type SQLExec ¶
type SQLExec struct {
MiniDB
}
SQLExec is used to execute only updates.
func NewSQLExec ¶
NewSQLExec creates a new SQLExec for only updates.
func (*SQLExec) DoUpdate ¶
func (s *SQLExec) DoUpdate(query string, vars ...interface{}) (result ExecResult)
DoUpdate does the update.
type SQLRun ¶
SQLRun is used to execute queries and updates.
func (*SQLRun) DoExec ¶
func (s *SQLRun) DoExec(query string, args ...interface{}) ExecResult
DoExec executes a SQL.
func (*SQLRun) DoQuery ¶
func (s *SQLRun) DoQuery(query string, args ...interface{}) (result ExecResult)
DoQuery does the query.
type SQLStore ¶
type SQLStore struct { DB *sql.DB DriverName string LogTables []string TableCols map[string]*tableSchema }
SQLStore stores the log into database.
func NewSQLStore ¶
NewSQLStore creates a new SQLStore.
type Store ¶
type Store interface { // Store stores the log in database like MySQL, InfluxDB, and etc. Store(log *Log) }
Store defines the interface to Store a log.
type StructMapping ¶
type StructMapping struct { *StructPreparer // contains filtered or unexported fields }
StructMapping is the structure for mapping row to a structure.
func (*StructMapping) RowsData ¶
func (s *StructMapping) RowsData() interface{}
RowsData returns the mapped rows data.
func (*StructMapping) Scan ¶
func (s *StructMapping) Scan(rowNum int) error
Scan scans the query result to fetch the rows one by one.
type StructPreparer ¶
StructPreparer is the the structure to create struct mapping.
func NewStructPreparer ¶
func NewStructPreparer(v interface{}) *StructPreparer
NewStructPreparer creates a new StructPreparer.