add a ridiculous simple 'context#String' func which will return a very simple string representatin of the current request

Former-commit-id: e8a17f006516d77aa466b64c8065ed8f07b332e4
This commit is contained in:
Gerasimos (Makis) Maropoulos 2017-12-10 07:26:01 +02:00
parent ed79f0c3cd
commit aa18b62f64

View File

@ -19,6 +19,7 @@ import (
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"sync/atomic"
"time" "time"
"github.com/fatih/structs" "github.com/fatih/structs"
@ -813,8 +814,12 @@ type Context interface {
Application() Application Application() Application
// String returns the string representation of this request. // String returns the string representation of this request.
// Each context has a unique string representation, so this can be used // Each context has a unique string representation.
// as an "ID" as well, if needed. // It can be used for simple debugging scenarions, i.e print context as string.
//
// What it returns? A number which declares the length of the
// total `String` calls per executable application, followed
// by the remote IP (the client) and finally the method:url.
String() string String() string
} }
@ -867,10 +872,10 @@ type Map map[string]interface{}
// +------------------------------------------------------------+ // +------------------------------------------------------------+
type context struct { type context struct {
// the unique id, it's empty until `String` function is called, // the unique id, it's zero until `String` function is called,
// it's here to cache the random, unique context's id, although `String` // it's here to cache the random, unique context's id, although `String`
// returns more than this. // returns more than this.
id string id uint64
// the http.ResponseWriter wrapped by custom writer. // the http.ResponseWriter wrapped by custom writer.
writer ResponseWriter writer ResponseWriter
// the original http.Request // the original http.Request
@ -2735,19 +2740,6 @@ func (ctx *context) Exec(method string, path string) {
} }
} }
// String returns the string representation of this request.
// Each context has a unique string representation, so this can be used
// as an "ID" as well, if needed.
func (ctx *context) String() (s string) {
if ctx.id == "" {
// set the id here.
s = "..."
}
return
}
// Application returns the iris app instance which belongs to this context. // Application returns the iris app instance which belongs to this context.
// Worth to notice that this function returns an interface // Worth to notice that this function returns an interface
// of the Application, which contains methods that are safe // of the Application, which contains methods that are safe
@ -2756,3 +2748,28 @@ func (ctx *context) String() (s string) {
func (ctx *context) Application() Application { func (ctx *context) Application() Application {
return ctx.app return ctx.app
} }
var lastCapturedContextID uint64
// LastCapturedContextID returns the total number of `context#String` calls.
func LastCapturedContextID() uint64 {
return atomic.LoadUint64(&lastCapturedContextID)
}
// String returns the string representation of this request.
// Each context has a unique string representation.
// It can be used for simple debugging scenarions, i.e print context as string.
//
// What it returns? A number which declares the length of the
// total `String` calls per executable application, followed
// by the remote IP (the client) and finally the method:url.
func (ctx *context) String() string {
if ctx.id == 0 {
// set the id here.
forward := atomic.AddUint64(&lastCapturedContextID, 1)
ctx.id = forward
}
return fmt.Sprintf("[%d] %s ▶ %s:%s",
ctx.id, ctx.RemoteAddr(), ctx.Method(), ctx.Request().RequestURI)
}