mirror of
https://github.com/kataras/iris.git
synced 2025-02-03 07:50:34 +01:00
Merge pull request #13 from kataras/master
Up Former-commit-id: 95b3b37813f27cb14c63cf82e61c5d85f45af820
This commit is contained in:
commit
bdd9948111
|
@ -22,7 +22,10 @@ func main() {
|
||||||
|
|
||||||
// if !empty then its contents derives from `ctx.Values().Get("logger_message")
|
// if !empty then its contents derives from `ctx.Values().Get("logger_message")
|
||||||
// will be added to the logs.
|
// will be added to the logs.
|
||||||
MessageContextKey: "logger_message",
|
MessageContextKeys: []string{"logger_message"},
|
||||||
|
|
||||||
|
// if !empty then its contents derives from `ctx.GetHeader("User-Agent")
|
||||||
|
MessageHeaderKeys: []string{"User-Agent"},
|
||||||
})
|
})
|
||||||
|
|
||||||
app.Use(customLogger)
|
app.Use(customLogger)
|
||||||
|
|
|
@ -116,7 +116,7 @@ func main() {
|
||||||
app.Get("/echo", ws.Handler())
|
app.Get("/echo", ws.Handler())
|
||||||
|
|
||||||
// serve the javascript built'n client-side library,
|
// serve the javascript built'n client-side library,
|
||||||
// see weboskcets.html script tags, this path is used.
|
// see websockets.html script tags, this path is used.
|
||||||
app.Any("/iris-ws.js", func(ctx iris.Context) {
|
app.Any("/iris-ws.js", func(ctx iris.Context) {
|
||||||
ctx.Write(websocket.ClientSource)
|
ctx.Write(websocket.ClientSource)
|
||||||
})
|
})
|
||||||
|
|
|
@ -36,7 +36,7 @@ func setupWebsocket(app *iris.Application) {
|
||||||
app.Get("/echo", ws.Handler())
|
app.Get("/echo", ws.Handler())
|
||||||
|
|
||||||
// serve the javascript built'n client-side library,
|
// serve the javascript built'n client-side library,
|
||||||
// see weboskcets.html script tags, this path is used.
|
// see websockets.html script tags, this path is used.
|
||||||
app.Any("/iris-ws.js", func(ctx iris.Context) {
|
app.Any("/iris-ws.js", func(ctx iris.Context) {
|
||||||
ctx.Write(websocket.ClientSource)
|
ctx.Write(websocket.ClientSource)
|
||||||
})
|
})
|
||||||
|
|
|
@ -26,7 +26,7 @@ func main() {
|
||||||
app.Get("/my_endpoint", ws.Handler())
|
app.Get("/my_endpoint", ws.Handler())
|
||||||
|
|
||||||
// serve the javascript built'n client-side library,
|
// serve the javascript built'n client-side library,
|
||||||
// see weboskcets.html script tags, this path is used.
|
// see websockets.html script tags, this path is used.
|
||||||
app.Any("/iris-ws.js", func(ctx iris.Context) {
|
app.Any("/iris-ws.js", func(ctx iris.Context) {
|
||||||
ctx.Write(websocket.ClientSource)
|
ctx.Write(websocket.ClientSource)
|
||||||
})
|
})
|
||||||
|
|
|
@ -27,7 +27,7 @@ func main() {
|
||||||
app.Get("/my_endpoint", ws.Handler())
|
app.Get("/my_endpoint", ws.Handler())
|
||||||
|
|
||||||
// serve the javascript built'n client-side library,
|
// serve the javascript built'n client-side library,
|
||||||
// see weboskcets.html script tags, this path is used.
|
// see websockets.html script tags, this path is used.
|
||||||
app.Any("/iris-ws.js", func(ctx iris.Context) {
|
app.Any("/iris-ws.js", func(ctx iris.Context) {
|
||||||
ctx.Write(websocket.ClientSource)
|
ctx.Write(websocket.ClientSource)
|
||||||
})
|
})
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
"github.com/kataras/golog"
|
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
"github.com/kataras/iris/context"
|
"github.com/kataras/iris/context"
|
||||||
|
@ -17,8 +16,6 @@ import (
|
||||||
|
|
||||||
const globalConfigurationKeyword = "~"
|
const globalConfigurationKeyword = "~"
|
||||||
|
|
||||||
var globalConfigurationExisted = false
|
|
||||||
|
|
||||||
// homeConfigurationFilename returns the physical location of the global configuration(yaml or toml) file.
|
// homeConfigurationFilename returns the physical location of the global configuration(yaml or toml) file.
|
||||||
// This is useful when we run multiple iris servers that share the same
|
// This is useful when we run multiple iris servers that share the same
|
||||||
// configuration, even with custom values at its "Other" field.
|
// configuration, even with custom values at its "Other" field.
|
||||||
|
@ -28,28 +25,6 @@ func homeConfigurationFilename(ext string) string {
|
||||||
return filepath.Join(homeDir(), "iris"+ext)
|
return filepath.Join(homeDir(), "iris"+ext)
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
|
||||||
filename := homeConfigurationFilename(".yml")
|
|
||||||
c, err := parseYAML(filename)
|
|
||||||
if err != nil {
|
|
||||||
// this error will be occurred the first time that the configuration
|
|
||||||
// file doesn't exist.
|
|
||||||
// Create the YAML-ONLY global configuration file now using the default configuration 'c'.
|
|
||||||
// This is useful when we run multiple iris servers that share the same
|
|
||||||
// configuration, even with custom values at its "Other" field.
|
|
||||||
out, err := yaml.Marshal(&c)
|
|
||||||
|
|
||||||
if err == nil {
|
|
||||||
err = ioutil.WriteFile(filename, out, os.FileMode(0666))
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
golog.Debugf("error while writing the global configuration field at: %s. Trace: %v\n", filename, err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
globalConfigurationExisted = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func homeDir() (home string) {
|
func homeDir() (home string) {
|
||||||
u, err := user.Current()
|
u, err := user.Current()
|
||||||
if u != nil && err == nil {
|
if u != nil && err == nil {
|
||||||
|
|
|
@ -6,6 +6,8 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// $ go test -v -run TestConfiguration*
|
// $ go test -v -run TestConfiguration*
|
||||||
|
@ -89,7 +91,30 @@ func TestConfigurationOptionsDeep(t *testing.T) {
|
||||||
t.Fatalf("DEEP configuration is not the same after New expected:\n %#v \ngot:\n %#v", expected, has)
|
t.Fatalf("DEEP configuration is not the same after New expected:\n %#v \ngot:\n %#v", expected, has)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func createGlobalConfiguration(t *testing.T) {
|
||||||
|
filename := homeConfigurationFilename(".yml")
|
||||||
|
c, err := parseYAML(filename)
|
||||||
|
if err != nil {
|
||||||
|
// this error will be occurred the first time that the configuration
|
||||||
|
// file doesn't exist.
|
||||||
|
// Create the YAML-ONLY global configuration file now using the default configuration 'c'.
|
||||||
|
// This is useful when we run multiple iris servers that share the same
|
||||||
|
// configuration, even with custom values at its "Other" field.
|
||||||
|
out, err := yaml.Marshal(&c)
|
||||||
|
|
||||||
|
if err == nil {
|
||||||
|
err = ioutil.WriteFile(filename, out, os.FileMode(0666))
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("error while writing the global configuration field at: %s. Trace: %v\n", filename, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestConfigurationGlobal(t *testing.T) {
|
func TestConfigurationGlobal(t *testing.T) {
|
||||||
|
createGlobalConfiguration(t)
|
||||||
|
|
||||||
testConfigurationGlobal(t, WithGlobalConfiguration)
|
testConfigurationGlobal(t, WithGlobalConfiguration)
|
||||||
// globalConfigurationKeyword = "~""
|
// globalConfigurationKeyword = "~""
|
||||||
testConfigurationGlobal(t, WithConfiguration(YAML(globalConfigurationKeyword)))
|
testConfigurationGlobal(t, WithConfiguration(YAML(globalConfigurationKeyword)))
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package maintenance
|
package maintenance
|
||||||
|
|
||||||
// Start starts the maintenance process.
|
// Start starts the maintenance process.
|
||||||
func Start(globalConfigurationExisted bool) {
|
func Start() {
|
||||||
CheckForUpdates(!globalConfigurationExisted)
|
CheckForUpdates()
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ const (
|
||||||
|
|
||||||
// CheckForUpdates checks for any available updates
|
// CheckForUpdates checks for any available updates
|
||||||
// and asks for the user if want to update now or not.
|
// and asks for the user if want to update now or not.
|
||||||
func CheckForUpdates(ft bool) {
|
func CheckForUpdates() {
|
||||||
v := version.Acquire()
|
v := version.Acquire()
|
||||||
updateAvailale := v.Compare(Version) == version.Smaller
|
updateAvailale := v.Compare(Version) == version.Smaller
|
||||||
|
|
||||||
|
|
2
doc.go
2
doc.go
|
@ -1416,7 +1416,7 @@ Example Server Code:
|
||||||
app.Get("/echo", ws.Handler())
|
app.Get("/echo", ws.Handler())
|
||||||
|
|
||||||
// serve the javascript built'n client-side library,
|
// serve the javascript built'n client-side library,
|
||||||
// see weboskcets.html script tags, this path is used.
|
// see websockets.html script tags, this path is used.
|
||||||
app.Any("/iris-ws.js", func(ctx iris.Context) {
|
app.Any("/iris-ws.js", func(ctx iris.Context) {
|
||||||
ctx.Write(websocket.ClientSource)
|
ctx.Write(websocket.ClientSource)
|
||||||
})
|
})
|
||||||
|
|
2
iris.go
2
iris.go
|
@ -771,7 +771,7 @@ func (app *Application) Run(serve Runner, withOrWithout ...Configurator) error {
|
||||||
app.logger.Debugf("Application: running using %d host(s)", len(app.Hosts)+1)
|
app.logger.Debugf("Application: running using %d host(s)", len(app.Hosts)+1)
|
||||||
|
|
||||||
if !app.config.DisableVersionChecker {
|
if !app.config.DisableVersionChecker {
|
||||||
go maintenance.Start(globalConfigurationExisted)
|
go maintenance.Start()
|
||||||
}
|
}
|
||||||
|
|
||||||
// this will block until an error(unless supervisor's DeferFlow called from a Task).
|
// this will block until an error(unless supervisor's DeferFlow called from a Task).
|
||||||
|
|
|
@ -37,7 +37,7 @@ type Config struct {
|
||||||
// Defaults to false.
|
// Defaults to false.
|
||||||
Columns bool
|
Columns bool
|
||||||
|
|
||||||
// MessageContextKey if not empty,
|
// MessageContextKeys if not empty,
|
||||||
// the middleware will try to fetch
|
// the middleware will try to fetch
|
||||||
// the contents with `ctx.Values().Get(MessageContextKey)`
|
// the contents with `ctx.Values().Get(MessageContextKey)`
|
||||||
// and if available then these contents will be
|
// and if available then these contents will be
|
||||||
|
@ -46,12 +46,23 @@ type Config struct {
|
||||||
// a new column will be added named 'Message'.
|
// a new column will be added named 'Message'.
|
||||||
//
|
//
|
||||||
// Defaults to empty.
|
// Defaults to empty.
|
||||||
MessageContextKey string
|
MessageContextKeys []string
|
||||||
|
|
||||||
|
// MessageHeaderKeys if not empty,
|
||||||
|
// the middleware will try to fetch
|
||||||
|
// the contents with `ctx.Values().Get(MessageHeaderKey)`
|
||||||
|
// and if available then these contents will be
|
||||||
|
// appended as part of the logs (with `%v`, in order to be able to set a struct too),
|
||||||
|
// if Columns field was setted to true then
|
||||||
|
// a new column will be added named 'HeaderMessage'.
|
||||||
|
//
|
||||||
|
// Defaults to empty.
|
||||||
|
MessageHeaderKeys []string
|
||||||
|
|
||||||
// LogFunc is the writer which logs are written to,
|
// LogFunc is the writer which logs are written to,
|
||||||
// if missing the logger middleware uses the app.Logger().Infof instead.
|
// if missing the logger middleware uses the app.Logger().Infof instead.
|
||||||
// Note that message argument can be empty.
|
// Note that message argument can be empty.
|
||||||
LogFunc func(now time.Time, latency time.Duration, status, ip, method, path string, message interface{})
|
LogFunc func(now time.Time, latency time.Duration, status, ip, method, path string, message interface{}, headerMessage interface{})
|
||||||
// Skippers used to skip the logging i.e by `ctx.Path()` and serve
|
// Skippers used to skip the logging i.e by `ctx.Path()` and serve
|
||||||
// the next/main handler immediately.
|
// the next/main handler immediately.
|
||||||
Skippers []SkipperFunc
|
Skippers []SkipperFunc
|
||||||
|
@ -71,7 +82,6 @@ func DefaultConfig() Config {
|
||||||
Method: true,
|
Method: true,
|
||||||
Path: true,
|
Path: true,
|
||||||
Columns: false,
|
Columns: false,
|
||||||
MessageContextKey: "",
|
|
||||||
LogFunc: nil,
|
LogFunc: nil,
|
||||||
Skippers: nil,
|
Skippers: nil,
|
||||||
skip: nil,
|
skip: nil,
|
||||||
|
|
|
@ -70,19 +70,37 @@ func (l *requestLoggerMiddleware) ServeHTTP(ctx context.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var message interface{}
|
var message interface{}
|
||||||
if ctxKey := l.config.MessageContextKey; ctxKey != "" {
|
if ctxKeys := l.config.MessageContextKeys; len(ctxKeys) > 0 {
|
||||||
message = ctx.Values().Get(ctxKey)
|
for _, key := range ctxKeys {
|
||||||
|
msg := ctx.Values().Get(key)
|
||||||
|
if message == nil {
|
||||||
|
message = msg
|
||||||
|
} else {
|
||||||
|
message = fmt.Sprintf(" %v %v", message, msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var headerMessage interface{}
|
||||||
|
if headerKeys := l.config.MessageHeaderKeys; len(headerKeys) > 0 {
|
||||||
|
for _, key := range headerKeys {
|
||||||
|
msg := ctx.GetHeader(key)
|
||||||
|
if headerMessage == nil {
|
||||||
|
headerMessage = msg
|
||||||
|
} else {
|
||||||
|
headerMessage = fmt.Sprintf(" %v %v", headerMessage, msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// print the logs
|
// print the logs
|
||||||
if logFunc := l.config.LogFunc; logFunc != nil {
|
if logFunc := l.config.LogFunc; logFunc != nil {
|
||||||
logFunc(endTime, latency, status, ip, method, path, message)
|
logFunc(endTime, latency, status, ip, method, path, message, headerMessage)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if l.config.Columns {
|
if l.config.Columns {
|
||||||
endTimeFormatted := endTime.Format("2006/01/02 - 15:04:05")
|
endTimeFormatted := endTime.Format("2006/01/02 - 15:04:05")
|
||||||
output := Columnize(endTimeFormatted, latency, status, ip, method, path, message)
|
output := Columnize(endTimeFormatted, latency, status, ip, method, path, message, headerMessage)
|
||||||
ctx.Application().Logger().Printer.Output.Write([]byte(output))
|
ctx.Application().Logger().Printer.Output.Write([]byte(output))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -91,12 +109,16 @@ func (l *requestLoggerMiddleware) ServeHTTP(ctx context.Context) {
|
||||||
if message != nil {
|
if message != nil {
|
||||||
line += fmt.Sprintf(" %v", message)
|
line += fmt.Sprintf(" %v", message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if headerMessage != nil {
|
||||||
|
line += fmt.Sprintf(" %v", headerMessage)
|
||||||
|
}
|
||||||
ctx.Application().Logger().Info(line)
|
ctx.Application().Logger().Info(line)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Columnize formats the given arguments as columns and returns the formatted output,
|
// Columnize formats the given arguments as columns and returns the formatted output,
|
||||||
// note that it appends a new line to the end.
|
// note that it appends a new line to the end.
|
||||||
func Columnize(nowFormatted string, latency time.Duration, status, ip, method, path string, message interface{}) string {
|
func Columnize(nowFormatted string, latency time.Duration, status, ip, method, path string, message interface{}, headerMessage interface{}) string {
|
||||||
|
|
||||||
titles := "Time | Status | Latency | IP | Method | Path"
|
titles := "Time | Status | Latency | IP | Method | Path"
|
||||||
line := fmt.Sprintf("%s | %v | %4v | %s | %s | %s", nowFormatted, status, latency, ip, method, path)
|
line := fmt.Sprintf("%s | %v | %4v | %s | %s | %s", nowFormatted, status, latency, ip, method, path)
|
||||||
|
@ -105,6 +127,11 @@ func Columnize(nowFormatted string, latency time.Duration, status, ip, method, p
|
||||||
line += fmt.Sprintf(" | %v", message)
|
line += fmt.Sprintf(" | %v", message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if headerMessage != nil {
|
||||||
|
titles += " | HeaderMessage"
|
||||||
|
line += fmt.Sprintf(" | %v", headerMessage)
|
||||||
|
}
|
||||||
|
|
||||||
outputC := []string{
|
outputC := []string{
|
||||||
titles,
|
titles,
|
||||||
line,
|
line,
|
||||||
|
|
|
@ -53,7 +53,7 @@ Example code:
|
||||||
app.Get("/echo", ws.Handler())
|
app.Get("/echo", ws.Handler())
|
||||||
|
|
||||||
// serve the javascript built'n client-side library,
|
// serve the javascript built'n client-side library,
|
||||||
// see weboskcets.html script tags, this path is used.
|
// see websockets.html script tags, this path is used.
|
||||||
app.Any("/iris-ws.js", func(ctx context.Context) {
|
app.Any("/iris-ws.js", func(ctx context.Context) {
|
||||||
ctx.Write(websocket.ClientSource)
|
ctx.Write(websocket.ClientSource)
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue
Block a user