Просмотр исходного кода

[naming] module(s) => middleware

tags/v0.9.0
jimzhan 9 лет назад
Родитель
Сommit
ddd89214dd
11 измененных файлов: 51 добавлений и 107 удалений
  1. +5
    -5
      README.md
  2. +1
    -1
      middleware/cache.go
  3. +1
    -1
      middleware/compress.go
  4. +1
    -1
      middleware/header.go
  5. +1
    -1
      middleware/logger.go
  6. +1
    -1
      middleware/static.go
  7. +1
    -1
      middleware/xsrf.go
  8. +0
    -53
      module.go
  9. +0
    -23
      modules/compress_test.go
  10. +2
    -10
      rex.go
  11. +38
    -10
      router.go

+ 5
- 5
README.md Просмотреть файл

@@ -91,16 +91,16 @@ You will now have the HTTP server running on `0.0.0.0:9394`.
Hey, dude, why not just use those popular approaches, like file-based config? We know you'll be asking & we have the answer as well, [here](http://12factor.net/config).


## Modules
## Middleware

Modules (aka. middleware) work between http requests and the router, they are no different than the standard http.Handler. Existing modules from other frameworks like logging, authorization, session, gzipping are very easy to integrate into Rex. As long as it complies the standard `func(http.Handler) http.Handler` signature, you can simply add one like this:
Middlware modules work between http requests and the router, they are no different than the standard http.Handler. Existing middleware modules from other frameworks like logging, authorization, session, gzipping are very easy to integrate into Rex. As long as it complies the standard `func(http.Handler) http.Handler` signature, you can simply add one like this:

``` go
app.Use(modules.XSRF)
app.Use(middleware.XSRF)
```


Since a module is just the standard http.Handler, writing a custom module is also pretty straightforward:
Since a middleware module is just the standard http.Handler, writing custom middleware is also pretty straightforward:

``` go
app.Use(func(next http.Handler) http.Handler {
@@ -146,8 +146,8 @@ Positive! Rex is an internal/fundamental project at GoAnywhere. We developed it
- [X] Hot-Compile Runner
- [X] Live Reload Integration
- [X] Common Modules
- [X] Continuous Integration
- [ ] Full Test Converage
- [ ] Improved Template Rendering
- [ ] Project Wiki
- [ ] Continuous Integration
- [ ] Stable API

modules/cache.go → middleware/cache.go Просмотреть файл

@@ -20,7 +20,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
* ----------------------------------------------------------------------*/
package modules
package middleware

import "net/http"


modules/compress.go → middleware/compress.go Просмотреть файл

@@ -20,7 +20,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
* ----------------------------------------------------------------------*/
package modules
package middleware

import (
"bytes"

modules/header.go → middleware/header.go Просмотреть файл

@@ -20,7 +20,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
* ----------------------------------------------------------------------*/
package modules
package middleware

import "net/http"


modules/logger.go → middleware/logger.go Просмотреть файл

@@ -20,7 +20,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
* ----------------------------------------------------------------------*/
package modules
package middleware

import (
"net/http"

modules/static.go → middleware/static.go Просмотреть файл

@@ -20,7 +20,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
* ----------------------------------------------------------------------*/
package modules
package middleware

import (
"net/http"

modules/xsrf.go → middleware/xsrf.go Просмотреть файл

@@ -20,7 +20,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
* ----------------------------------------------------------------------*/
package modules
package middleware

import (
"bytes"

+ 0
- 53
module.go Просмотреть файл

@@ -1,53 +0,0 @@
/* ----------------------------------------------------------------------
* ______ ___ __
* / ____/___ / | ____ __ ___ __/ /_ ___ ________
* / / __/ __ \/ /| | / __ \/ / / / | /| / / __ \/ _ \/ ___/ _ \
* / /_/ / /_/ / ___ |/ / / / /_/ /| |/ |/ / / / / __/ / / __/
* \____/\____/_/ |_/_/ /_/\__. / |__/|__/_/ /_/\___/_/ \___/
* /____/
*
* (C) Copyright 2015 GoAnywhere (http://goanywhere.io).
* ----------------------------------------------------------------------
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ----------------------------------------------------------------------*/
package rex

import "net/http"

type module struct {
cache http.Handler
stack []func(http.Handler) http.Handler
}

// build sets up the whole middleware modules in a FIFO chain.
func (self *module) build() http.Handler {
if self.cache == nil {
var next http.Handler = http.DefaultServeMux
// Activate modules in FIFO order.
for index := len(self.stack) - 1; index >= 0; index-- {
next = self.stack[index](next)
}
self.cache = next
}
return self.cache
}

// Use add the middleware module into the stack chain.
func (self *module) Use(modules ...func(http.Handler) http.Handler) {
self.stack = append(self.stack, modules...)
}

// Implements the net/http Handler interface and calls the middleware stack.
func (self *module) ServeHTTP(w http.ResponseWriter, r *http.Request) {
self.build().ServeHTTP(w, r)
}

+ 0
- 23
modules/compress_test.go Просмотреть файл

@@ -1,23 +0,0 @@
/* ----------------------------------------------------------------------
* ______ ___ __
* / ____/___ / | ____ __ ___ __/ /_ ___ ________
* / / __/ __ \/ /| | / __ \/ / / / | /| / / __ \/ _ \/ ___/ _ \
* / /_/ / /_/ / ___ |/ / / / /_/ /| |/ |/ / / / / __/ / / __/
* \____/\____/_/ |_/_/ /_/\__. / |__/|__/_/ /_/\___/_/ \___/
* /____/
*
* (C) Copyright 2015 GoAnywhere (http://goanywhere.io).
* ----------------------------------------------------------------------
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ----------------------------------------------------------------------*/
package modules

+ 2
- 10
rex.go Просмотреть файл

@@ -29,7 +29,7 @@ import (
"runtime"

"github.com/goanywhere/rex/internal"
"github.com/goanywhere/rex/modules"
mw "github.com/goanywhere/rex/middleware"
"github.com/goanywhere/x/env"
"github.com/goanywhere/x/fs"
)
@@ -86,21 +86,13 @@ func Group(path string) *Router {
return DefaultMux.Group(path)
}

// FileServer registers a handler to serve HTTP requests
// with the contents of the file system rooted at root.
/*
func FileServer(prefix, dir string) {
DefaultMux.FileServer(prefix, dir)
}
*/

// Use appends middleware module into the serving list, modules will be served in FIFO order.
func Use(module func(http.Handler) http.Handler) {
DefaultMux.Use(module)
}

func Run() {
DefaultMux.Use(modules.Logger)
DefaultMux.Use(mw.Logger)
DefaultMux.Run()
}


+ 38
- 10
router.go Просмотреть файл

@@ -32,8 +32,36 @@ import (
"github.com/gorilla/mux"
)

type middleware struct {
cache http.Handler
stack []func(http.Handler) http.Handler
}

// build sets up the whole middleware modules in a FIFO chain.
func (self *middleware) build() http.Handler {
if self.cache == nil {
var next http.Handler = http.DefaultServeMux
// Activate modules in FIFO order.
for index := len(self.stack) - 1; index >= 0; index-- {
next = self.stack[index](next)
}
self.cache = next
}
return self.cache
}

// Use add the middleware module into the stack chain.
func (self *middleware) Use(modules ...func(http.Handler) http.Handler) {
self.stack = append(self.stack, modules...)
}

// Implements the net/http Handler interface and calls the middleware stack.
func (self *middleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
self.build().ServeHTTP(w, r)
}

type Router struct {
module *module
middleware *middleware
mux *mux.Router
ready bool
subrouters []*Router
@@ -41,8 +69,8 @@ type Router struct {

func New() *Router {
return &Router{
module: new(module),
mux: mux.NewRouter().StrictSlash(true),
middleware: new(middleware),
mux: mux.NewRouter().StrictSlash(true),
}
}

@@ -51,18 +79,18 @@ func (self *Router) build() http.Handler {
if !self.ready {
self.ready = true
// * activate router's middleware modules.
self.module.Use(func(http.Handler) http.Handler {
self.middleware.Use(func(http.Handler) http.Handler {
return self.mux
})
// * activate subrouters's middleware modules.
for index := 0; index < len(self.subrouters); index++ {
sr := self.subrouters[index]
sr.module.Use(func(http.Handler) http.Handler {
sr.middleware.Use(func(http.Handler) http.Handler {
return sr.mux
})
}
}
return self.module
return self.middleware
}

// register adds the http.Handler/http.HandleFunc into Gorilla mux.
@@ -121,11 +149,11 @@ func (self *Router) Delete(pattern string, handler interface{}) {

// Group creates a new application group under the given path prefix.
func (self *Router) Group(prefix string) *Router {
var module = new(module)
self.mux.PathPrefix(prefix).Handler(module)
var middleware = new(middleware)
self.mux.PathPrefix(prefix).Handler(middleware)
var mux = self.mux.PathPrefix(prefix).Subrouter()

router := &Router{module: module, mux: mux}
router := &Router{middleware: middleware, mux: mux}
self.subrouters = append(self.subrouters, router)
return router
}
@@ -141,7 +169,7 @@ func (self *Router) Name(r *http.Request) (name string) {

// Use add the middleware module into the stack chain.
func (self *Router) Use(module func(http.Handler) http.Handler) {
self.module.Use(module)
self.middleware.Use(module)
}

// ServeHTTP dispatches the request to the handler whose

Загрузка…
Отмена
Сохранить