-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
## 行云 ## | ||
|
||
### 对象 ### | ||
|
||
- Server: pipe容器 | ||
- Pipe: 对请求做前、后置处理 | ||
- Context: 请求相关的数据 | ||
|
||
### 接口 ### | ||
|
||
- http.Handler | ||
- pipe.Handler (negroni.Handler) | ||
- Config | ||
- Logger | ||
- Router: 用于Pipe路由Handler路由 | ||
|
||
### 目标 ### | ||
|
||
- 提供机制、而不**只是**实现 | ||
- 方便扩展 | ||
- 默认配置功能强大、容易使用 | ||
- 使用代码生成作为元编程 | ||
- Everything is http.Handler | ||
|
||
### Core SubContext ### | ||
|
||
- Cookie | ||
- Session | ||
- XSRF | ||
- Flash | ||
- Render | ||
|
||
### Default PipeHandler ### | ||
|
||
- Logger | ||
- Recover | ||
- Static |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package xingyun |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package xingyun |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package xingyun |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package xingyun | ||
|
||
type logger interface { | ||
Infof(s string, o ...interface{}) | ||
Errorf(s string, o ...interface{}) | ||
Debugf(s string, o ...interface{}) | ||
Tracef(s string, o ...interface{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package xingyun | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/codegangsta/negroni" | ||
) | ||
|
||
var ( | ||
DefaultPipeHandler = []PipeHandler{} | ||
) | ||
|
||
type PipeHandler interface { | ||
ServeHTTP(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) | ||
} | ||
|
||
type Pipe struct { | ||
negroni *negroni.Negroni | ||
} | ||
|
||
func (p *Pipe) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
p.negroni.ServeHTTP(w, r) | ||
} | ||
|
||
func (p *Pipe) Use(handlers ...PipeHandler) { | ||
if len(DefaultPipeHandler) != 0 && len(p.negroni.Handlers()) == 0 { | ||
for _, h := range DefaultPipeHandler { | ||
p.negroni.Use(h) | ||
} | ||
} | ||
for _, h := range handlers { | ||
p.negroni.Use(h) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package xingyun | ||
|
||
import "net/http" | ||
|
||
type Router interface { | ||
Route(r *http.Request) http.Handler | ||
} | ||
|
||
func RouterHandler(router Router) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
h := router.Route(r) | ||
h.ServeHTTP(w, r) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package xingyun | ||
|
||
import "net/http" | ||
|
||
type Server struct { | ||
Config Config | ||
|
||
pipeRouter Router | ||
} | ||
|
||
func NewServer(router Router) *Server { | ||
return &Server{pipeRouter: router} | ||
} | ||
|
||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
h := RouterHandler(s.pipeRouter) | ||
h(w, r) | ||
} | ||
|
||
func (s *Server) ListenAndServe(addr string) { | ||
http.ListenAndServe(addr, s) | ||
} |