From 9143ccec6e03c69cc404cfddf111c0c1bbdc9ffd Mon Sep 17 00:00:00 2001 From: hiveminded Date: Tue, 1 Aug 2017 22:25:08 +0300 Subject: [PATCH] add PartyFunc and gofmt -s -w . :large_blue_diamond: Former-commit-id: a3809498a45140d691f3f235ad9cb25239d495a2 --- .travis.yml | 3 ++- context/response_recorder.go | 6 ++++++ context/response_writer.go | 10 ++++++++++ core/router/api_builder.go | 22 ++++++++++++++++++++++ core/router/party.go | 21 ++++++++++++++++++++- iris.go | 7 +++++++ sessions/session.go | 10 +++++----- sessions/sessiondb/redis/database.go | 2 +- 8 files changed, 73 insertions(+), 8 deletions(-) diff --git a/.travis.yml b/.travis.yml index f0e0593c..0c4e63c0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,7 +15,8 @@ after_script: - cd ./_examples - go get ./... - go test -v -cover ./... + - cd ../ # typescript examples - - cd ../../typescript/_examples + - cd ./typescript/_examples - go get ./... - go test -v -cover ./... \ No newline at end of file diff --git a/context/response_recorder.go b/context/response_recorder.go index 8af7b17e..76e7b777 100644 --- a/context/response_recorder.go +++ b/context/response_recorder.go @@ -41,6 +41,12 @@ type ResponseRecorder struct { var _ ResponseWriter = &ResponseRecorder{} +// Naive returns the simple, underline and original http.ResponseWriter +// that backends this response writer. +func (w *ResponseRecorder) Naive() http.ResponseWriter { + return w.ResponseWriter.Naive() +} + // BeginRecord accepts its parent ResponseWriter and // prepares itself, the response recorder, to record and send response to the client. func (w *ResponseRecorder) BeginRecord(underline ResponseWriter) { diff --git a/context/response_writer.go b/context/response_writer.go index 86555f22..7f0bfe48 100644 --- a/context/response_writer.go +++ b/context/response_writer.go @@ -28,6 +28,10 @@ type ResponseWriter interface { http.CloseNotifier http.Pusher + // Naive returns the simple, underline and original http.ResponseWriter + // that backends this response writer. + Naive() http.ResponseWriter + // BeginResponse receives an http.ResponseWriter // and initialize or reset the response writer's field's values. BeginResponse(http.ResponseWriter) @@ -117,6 +121,12 @@ const ( StatusCodeWritten = 0 ) +// Naive returns the simple, underline and original http.ResponseWriter +// that backends this response writer. +func (w *responseWriter) Naive() http.ResponseWriter { + return w.ResponseWriter +} + // BeginResponse receives an http.ResponseWriter // and initialize or reset the response writer's field's values. func (w *responseWriter) BeginResponse(underline http.ResponseWriter) { diff --git a/core/router/api_builder.go b/core/router/api_builder.go index b7dcb518..d08e2025 100644 --- a/core/router/api_builder.go +++ b/core/router/api_builder.go @@ -215,6 +215,28 @@ func (rb *APIBuilder) Party(relativePath string, handlers ...context.Handler) Pa } } +// PartyFunc same as `Party`, groups routes that share a base path or/and same handlers. +// However this function accepts a function that receives this created Party instead. +// Returns the Party in order the caller to be able to use this created Party to continue the +// top-bottom routes "tree". +// +// Note: `iris#Party` and `core/router#Party` describes the exactly same interface. +// +// Usage: +// app.PartyFunc("/users", func(u iris.Party){ +// u.Use(authMiddleware, logMiddleware) +// u.Get("/", getAllUsers) +// u.Post("/", createOrUpdateUser) +// u.Delete("/", deleteUser) +// }) +// +// Look `Party` for more. +func (rb *APIBuilder) PartyFunc(relativePath string, partyBuilderFunc func(p Party)) Party { + p := rb.Party(relativePath) + partyBuilderFunc(p) + return p +} + // Subdomain returns a new party which is responsible to register routes to // this specific "subdomain". // diff --git a/core/router/party.go b/core/router/party.go index 44975a85..7a2ff2de 100644 --- a/core/router/party.go +++ b/core/router/party.go @@ -2,7 +2,9 @@ package router import ( "github.com/kataras/iris/context" -) // Party is here to separate the concept of +) + +// Party is here to separate the concept of // api builder and the sub api builder. // Party is just a group joiner of routes which have the same prefix and share same middleware(s) also. @@ -12,6 +14,23 @@ import ( type Party interface { // Party creates and returns a new child Party with the following features. Party(relativePath string, middleware ...context.Handler) Party + // PartyFunc same as `Party`, groups routes that share a base path or/and same handlers. + // However this function accepts a function that receives this created Party instead. + // Returns the Party in order the caller to be able to use this created Party to continue the + // top-bottom routes "tree". + // + // Note: `iris#Party` and `core/router#Party` describes the exactly same interface. + // + // Usage: + // app.PartyFunc("/users", func(u iris.Party){ + // u.Use(authMiddleware, logMiddleware) + // u.Get("/", getAllUsers) + // u.Post("/", createOrUpdateUser) + // u.Delete("/", deleteUser) + // }) + // + // Look `Party` for more. + PartyFunc(relativePath string, partyBuilderFunc func(p Party)) Party // Subdomain returns a new party which is responsible to register routes to // this specific "subdomain". // diff --git a/iris.go b/iris.go index a9efbb34..099ba32e 100644 --- a/iris.go +++ b/iris.go @@ -276,6 +276,13 @@ func (app *Application) View(writer io.Writer, filename string, layout string, b } var ( + // Party is just a group joiner of routes which have the same prefix and share same middleware(s) also. + // Party could also be named as 'Join' or 'Node' or 'Group' , Party chosen because it is fun. + // + // Look the `core/router#APIBuilder` for its implementation. + // + // A shortcut for the `core/router#Party`, useful when `PartyFunc` is being used. + Party router.Party // LimitRequestBodySize is a middleware which sets a request body size limit // for all next handlers in the chain. // diff --git a/sessions/session.go b/sessions/session.go index b998d9f8..942001dc 100644 --- a/sessions/session.go +++ b/sessions/session.go @@ -25,11 +25,11 @@ type ( // but without temp values (flash messages) which are removed after fetching. // so introduce a new field here. // NOTE: flashes are not managed by third-party, only inside session struct. - flashes map[string]*flashMessage - mu sync.RWMutex - expireAt *time.Time // nil pointer means no expire date - timer *time.Timer - provider *provider + flashes map[string]*flashMessage + mu sync.RWMutex + expireAt *time.Time // nil pointer means no expire date + timer *time.Timer + provider *provider } flashMessage struct { diff --git a/sessions/sessiondb/redis/database.go b/sessions/sessiondb/redis/database.go index 8a9885af..62dfc4af 100644 --- a/sessions/sessiondb/redis/database.go +++ b/sessions/sessiondb/redis/database.go @@ -1,9 +1,9 @@ package redis import ( - "time" "bytes" "encoding/gob" + "time" "github.com/kataras/iris/sessions/sessiondb/redis/service" )