From 1a7c79db666b2a69b16d8918c0524516a3782cf5 Mon Sep 17 00:00:00 2001 From: Gerasimos Maropoulos Date: Wed, 7 Sep 2016 07:36:23 +0300 Subject: [PATCH] Update to 4.1.7. Logger changes, read HISTORY.md --- HISTORY.md | 8 +++++ README.md | 83 ++++++++++++++++++++++++++++++++++++++------------ config/iris.go | 19 ++++++++++++ context.go | 9 +++++- http.go | 7 +++-- iris.go | 83 ++++++++++++++++++++------------------------------ plugin.go | 8 ++--- ssh.go | 7 +++-- websocket.go | 5 +-- 9 files changed, 147 insertions(+), 82 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index f63f59c2..c1d5d32a 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -2,6 +2,14 @@ **How to upgrade**: remove your `$GOPATH/src/github.com/kataras/iris` folder, open your command-line and execute this command: `go get -u github.com/kataras/iris/iris`. +## 4.1.6 -> 4.1.7 + +- **CHANGED**: Use of the standard `log.Logger` instead of the `iris-contrib/logger`(colorful logger), these changes are reflects some middleware, examples and plugins, I updated all of them, so don't worry. + +So, [iris-contrib/middleware/logger](https://github.com/iris-contrib/middleware/tree/master/logger) will now NO need to pass other Logger instead, instead of: `iris.Use(logger.New(iris.Logger))` use -> `iris.Use(logger.New())` which will use the iris/instance's Logger. + +- **ADDED**: `context.Framework()` which returns your Iris instance (typeof `*iris.Framework`), useful for the future(Iris will give you, soon, the ability to pass custom options inside an iris instance). + ## 4.1.5 -> 4.1.6 - Align with [go-sessions](https://github.com/kataras/go-sessions), no front-end changes, however I think that the best time to make an upgrade to your local Iris is right now. diff --git a/README.md b/README.md index cf65945c..9257263d 100644 --- a/README.md +++ b/README.md @@ -3,31 +3,68 @@ -
+

+ Build Status -License +https://cdn.rawgit.com/sindresorhus/awesome/d7305f38d29fed78fa85652e3a63e154dd8e8829/media/badge.svg -Releases +Platforms -Practical Guide/Docs
- -Examples - -Build Status +License Built with GoLang -Platforms -

-Benchmark Wizzard July 21, 2016- Processing Time Horizontal Grap -

+
+ + +Releases + +Examples + +Practical Guide/Docs + +Chat

+ + +The fastest backend web framework for Go. Easy to learn, while it's highly customizable.
+Ideally suited for both experienced and novice Developers. +
-The fastest backend web framework, written entirely in Go.
Easy to learn, while it's highly customizable.
-Ideally suited for both experienced and novice Developers.

+ +Quick look +------------ + +```go +package main + +import "github.com/kataras/iris" + +func main() { + iris.Favicon("./favicon.ico") + + iris.Get("/", func(ctx *iris.Context) { + ctx.Render("index.html") + }) + + iris.Get("/login", func(ctx *iris.Context) { + ctx.Render("login.html", iris.Map{"Title": "Login Page"}) + }) + + iris.Post("/login", func(ctx *iris.Context) { + secret := ctx.PostValue("secret") + ctx.Session().Set("secret", secret) + + ctx.Redirect("/user") + }) + + iris.Listen(":8080") +} +``` + Installation ------------ The only requirement is the [Go Programming Language](https://golang.org/dl), at least v1.7. @@ -141,18 +178,26 @@ I recommend writing your API tests using this new library, [httpexpect](https:// Versioning ------------ -Current: **v4.1.6** +Current: **v4.1.7** > Iris is an active project +Read more about Semantic Versioning 2.0.0 + + - http://semver.org/ + - https://en.wikipedia.org/wiki/Software_versioning + - https://wiki.debian.org/UpstreamGuide#Releases_and_Versions Todo ------------ +- [x] Use of the standard `log.Logger` instead of the `iris-contrib/logger`(colorful logger), make these changes to all middleware, examples and plugins. +- [ ] Implement, even, a better way to manage configuration/options, devs will be able to set their own custom options inside there. ` I'm thinking of something the last days, but it will have breaking changes. ` +- [ ] Implement an internal updater, as requested [here](https://github.com/kataras/iris/issues/401). -Iris is a community-driven project, waiting for your suggestions and feature requests to add some items here! +Iris is a **Community-Driven** Project, waiting for your suggestions and [feature requests](https://github.com/kataras/iris/issues?utf8=%E2%9C%93&q=label%3A%22feature%20request%22)! - -If you're **willing to donate** click [here](DONATIONS.md)! +I, as the author of this package, am working full time on this package, no time to any other job, so +if you're **willing to donate** and you can **afford it** please click [here](DONATIONS.md), thank you! People ------------ @@ -176,7 +221,7 @@ License can be found [here](LICENSE). [Travis]: http://travis-ci.org/kataras/iris [License Widget]: https://img.shields.io/badge/license-MIT%20%20License%20-E91E63.svg?style=flat-square [License]: https://github.com/kataras/iris/blob/master/LICENSE -[Release Widget]: https://img.shields.io/badge/release-v4.1.6-blue.svg?style=flat-square +[Release Widget]: https://img.shields.io/badge/release-v4.1.7-blue.svg?style=flat-square [Release]: https://github.com/kataras/iris/releases [Chat Widget]: https://img.shields.io/badge/community-chat-00BCD4.svg?style=flat-square [Chat]: https://kataras.rocket.chat/channel/iris diff --git a/config/iris.go b/config/iris.go index 45977b44..0de92c74 100644 --- a/config/iris.go +++ b/config/iris.go @@ -1,6 +1,9 @@ package config import ( + "io" + "os" + "github.com/imdario/mergo" ) @@ -9,6 +12,11 @@ const ( DefaultDisablePathCorrection = false DefaultDisablePathEscape = false DefaultCharset = "UTF-8" + DefaultLoggerPreffix = "[IRIS] " +) + +var ( + DefaultLoggerOut = os.Stdout ) type ( @@ -42,6 +50,15 @@ type ( // Default is false DisableBanner bool + // LoggerOut is the destination for output + // + // defaults to os.Stdout + LoggerOut io.Writer + // LoggerOut is the logger's prefix to write at beginning of each line + // + // Defaults to [IRIS] + LoggerPreffix string + // ProfilePath a the route path, set it to enable http pprof tool // Default is empty, if you set it to a $path, these routes will handled: // $path/cmdline @@ -106,6 +123,8 @@ func Default() Iris { DisablePathCorrection: DefaultDisablePathCorrection, DisablePathEscape: DefaultDisablePathEscape, DisableBanner: false, + LoggerOut: DefaultLoggerOut, + LoggerPreffix: DefaultLoggerPreffix, DisableTemplateEngines: false, IsDevelopment: false, Charset: DefaultCharset, diff --git a/context.go b/context.go index c3cd2735..d92d2c49 100644 --- a/context.go +++ b/context.go @@ -568,7 +568,9 @@ func (ctx *Context) Render(name string, binding interface{}, options ...map[stri func (ctx *Context) MustRender(name string, binding interface{}, options ...map[string]interface{}) { if err := ctx.Render(name, binding, options...); err != nil { ctx.Panic() - ctx.framework.Logger.Dangerf("MustRender panics for client with IP: %s On template: %s.Trace: %s\n", ctx.RemoteAddr(), name, err) + if ctx.framework.Config.IsDevelopment { + ctx.framework.Logger.Printf("MustRender panics for client with IP: %s On template: %s.Trace: %s\n", ctx.RemoteAddr(), name, err) + } } } @@ -978,3 +980,8 @@ func (ctx *Context) SessionDestroy() { func (ctx *Context) Log(format string, a ...interface{}) { ctx.framework.Logger.Printf(format, a...) } + +// Framework returns the Iris instance, containing the configuration and all other fields +func (ctx *Context) Framework() *Framework { + return ctx.framework +} diff --git a/http.go b/http.go index 689c3f66..4e8f826e 100644 --- a/http.go +++ b/http.go @@ -13,8 +13,9 @@ import ( "sync" "time" + "log" + "github.com/iris-contrib/letsencrypt" - "github.com/iris-contrib/logger" "github.com/kataras/go-errors" "github.com/kataras/iris/config" "github.com/kataras/iris/utils" @@ -1336,7 +1337,7 @@ type ( api *muxAPI errorHandlers map[int]Handler - logger *logger.Logger + logger *log.Logger // the main server host's name, ex: localhost, 127.0.0.1, 0.0.0.0, iris-go.com hostname string // if any of the trees contains not empty subdomain @@ -1351,7 +1352,7 @@ type ( } ) -func newServeMux(logger *logger.Logger) *serveMux { +func newServeMux(logger *log.Logger) *serveMux { mux := &serveMux{ lookups: make([]*route, 0), errorHandlers: make(map[int]Handler, 0), diff --git a/iris.go b/iris.go index b3a4938e..93dbd98f 100644 --- a/iris.go +++ b/iris.go @@ -52,8 +52,19 @@ package iris // import "github.com/kataras/iris" import ( "fmt" + "log" + "net/http" + "net/url" + "os" + "path" + "reflect" + "strconv" + "strings" + "sync" + "testing" + "time" + "github.com/gavv/httpexpect" - "github.com/iris-contrib/logger" "github.com/iris-contrib/response/data" "github.com/iris-contrib/response/json" "github.com/iris-contrib/response/jsonp" @@ -69,21 +80,11 @@ import ( "github.com/kataras/iris/context" "github.com/kataras/iris/utils" "github.com/valyala/fasthttp" - "net/http" - "net/url" - "os" - "path" - "reflect" - "strconv" - "strings" - "sync" - "testing" - "time" ) const ( // Version of the iris - Version = "4.1.6" + Version = "4.1.7" banner = ` _____ _ |_ _| (_) @@ -97,7 +98,7 @@ const ( var ( Default *Framework Config *config.Iris - Logger *logger.Logger + Logger *log.Logger // if you want colors in your console then you should use this https://github.com/iris-contrib/logger instead. Plugins PluginContainer Websocket *WebsocketServer // Look ssh.go for this field's configuration @@ -169,7 +170,7 @@ type ( // Implements the FrameworkAPI Framework struct { *muxAPI - contextPool *sync.Pool + contextPool sync.Pool Config *config.Iris sessions sessions.Sessions responses *responseEngines @@ -178,7 +179,7 @@ type ( // the last added server is the main server Servers *ServerList // configuration by instance.Logger.Config - Logger *logger.Logger + Logger *log.Logger Plugins PluginContainer Websocket *WebsocketServer SSH *SSHServer @@ -200,7 +201,9 @@ func New(cfg ...config.Iris) *Framework { // we always use 's' no 'f' because 's' is easier for me to remember because of 'station' // some things never change :) s := &Framework{ - Config: &c, + Config: &c, + // set the Logger + Logger: log.New(c.LoggerOut, c.LoggerPreffix, log.LstdFlags), responses: &responseEngines{}, Available: make(chan bool), SSH: &SSHServer{}, @@ -208,10 +211,11 @@ func New(cfg ...config.Iris) *Framework { sessions: sessions.New(sessions.Config(c.Sessions)), } { - + s.contextPool.New = func() interface{} { + return &Context{framework: s} + } ///NOTE: set all with s.Config pointer - // set the Logger - s.Logger = logger.New(logger.DefaultConfig()) + // set the plugin container s.Plugins = newPluginContainer(s.Logger) // set the templates @@ -219,9 +223,6 @@ func New(cfg ...config.Iris) *Framework { "url": s.URL, "urlpath": s.Path, }) - s.contextPool = &sync.Pool{New: func() interface{} { - return &Context{framework: s} - }} // set the websocket server s.Websocket = NewWebsocketServer(s.Config.Websocket) // set the servemux, which will provide us the public API also, with its context pool @@ -292,29 +293,6 @@ func (s *Framework) initialize() { } } -/* not used anymore, we had 2% performance reduce -func (s *Framework) acquireCtx(reqCtx *fasthttp.RequestCtx) *Context { - v := s.contextPool.Get() - if v == nil { - return &Context{ - RequestCtx: reqCtx, - framework: s, - } - } - ctx := v.(*Context) - ctx.Params = ctx.Params[0:0] - ctx.RequestCtx = reqCtx - ctx.middleware = nil - ctx.session = nil - return ctx -} - -func (s *Framework) releaseCtx(ctx *Context) { - s.contextPool.Put(ctx) -} -// so .New() is better because of internal .Get() pins -*/ - // Go starts the iris station, listens to all registered servers, and prepare only if Virtual func Go() error { return Default.Go() @@ -327,12 +305,14 @@ func (s *Framework) Go() error { // build the fasthttp handler to bind it to the servers h := s.mux.Handler() reqHandler := func(reqCtx *fasthttp.RequestCtx) { - ctx := s.contextPool.Get().(*Context) - ctx.Params = ctx.Params[0:0] + ctx := s.contextPool.Get().(*Context) // Changed to use the pool's New 09/07/2016, ~ -4k nanoseconds(9 bench tests) per requests (better performance) ctx.RequestCtx = reqCtx + + h(ctx) + + ctx.Params = ctx.Params[0:0] ctx.middleware = nil ctx.session = nil - h(ctx) s.contextPool.Put(ctx) } if firstErr := s.Servers.OpenAll(reqHandler); firstErr != nil { @@ -348,9 +328,12 @@ func (s *Framework) Go() error { hosts[i] = srv.Host() } - bannerMessage := time.Now().Format(config.TimeFormat) + ": Running at " + strings.Join(hosts, ", ") - s.Logger.PrintBanner(banner, "\n"+bannerMessage) + bannerMessage := fmt.Sprintf("%s: Running at %s", time.Now().Format(config.TimeFormat), strings.Join(hosts, ", ")) + // we don't print it via Logger because: + // 1. The banner is only 'useful' when the developer logs to terminal and no file + // 2. Prefix & LstdFlags options of the default s.Logger + fmt.Printf("%s\n\n%s\n", banner, bannerMessage) } s.Plugins.DoPostListen(s) diff --git a/plugin.go b/plugin.go index 5ad55879..a9ccc623 100644 --- a/plugin.go +++ b/plugin.go @@ -3,9 +3,9 @@ package iris import ( "sync" - "github.com/kataras/go-errors" + "log" - "github.com/iris-contrib/logger" + "github.com/kataras/go-errors" "github.com/kataras/go-fs" ) @@ -224,12 +224,12 @@ type pluginContainer struct { activatedPlugins []Plugin customEvents map[string][]func() downloader *pluginDownloadManager - logger *logger.Logger + logger *log.Logger mu sync.Mutex } // newPluginContainer receives a logger and returns a new PluginContainer -func newPluginContainer(l *logger.Logger) PluginContainer { +func newPluginContainer(l *log.Logger) PluginContainer { return &pluginContainer{logger: l} } diff --git a/ssh.go b/ssh.go index d2431693..9e0081da 100644 --- a/ssh.go +++ b/ssh.go @@ -43,7 +43,8 @@ import ( "text/template" "time" - "github.com/iris-contrib/logger" + "log" + "github.com/kardianos/osext" "github.com/kardianos/service" "github.com/kataras/go-errors" @@ -423,8 +424,8 @@ type SSHServer struct { Commands Commands // Commands{Command{Name: "restart", Description:"restarts & rebuild the server", Action: func(ssh.Channel){}}} // note for Commands field: // the default Iris's commands are defined at the end of this file, I tried to make this file as standalone as I can, because it will be used for Iris web framework also. - Shell bool // Set it to true to enable execute terminal's commands(system commands) via ssh if no other command is found from the Commands field. Defaults to false for security reasons - Logger *logger.Logger // log.New(...)/ $qinstance.Logger, fill it when you want to receive debug and info/warnings messages + Shell bool // Set it to true to enable execute terminal's commands(system commands) via ssh if no other command is found from the Commands field. Defaults to false for security reasons + Logger *log.Logger // log.New(...)/ $qinstance.Logger, fill it when you want to receive debug and info/warnings messages } // Enabled returns true if SSH can be started, if Host != "" diff --git a/websocket.go b/websocket.go index e66c588b..57d01c9c 100644 --- a/websocket.go +++ b/websocket.go @@ -1,7 +1,8 @@ package iris import ( - "github.com/iris-contrib/logger" + "log" + irisWebsocket "github.com/iris-contrib/websocket" "github.com/kataras/go-websocket" "github.com/kataras/iris/config" @@ -38,7 +39,7 @@ func NewWebsocketServer(c *config.Websocket) *WebsocketServer { // RegisterWebsocketServer registers the handlers for the websocket server // it's a bridge between station and websocket server -func RegisterWebsocketServer(station FrameworkAPI, server *WebsocketServer, logger *logger.Logger) { +func RegisterWebsocketServer(station FrameworkAPI, server *WebsocketServer, logger *log.Logger) { c := server.Config if c.Endpoint == "" { return