44 "log"
55 "net/http"
66
7- "github.com/infraboard/mcube/http/auth"
87 "github.com/infraboard/mcube/http/context"
98 "github.com/infraboard/mcube/http/response"
109 "github.com/infraboard/mcube/http/router"
@@ -26,7 +25,7 @@ type httpRouter struct {
2625
2726 middlewareChain []router.Middleware
2827 entries []* entry
29- authMiddleware router.Middleware
28+ auther router.Auther
3029 mergedHandler http.Handler
3130 labels []* router.Label
3231}
@@ -80,9 +79,8 @@ func (r *httpRouter) AddPublict(method, path string, h http.HandlerFunc) {
8079 r .add (e )
8180}
8281
83- func (r * httpRouter ) SetAuther (at auth.Auther ) {
84- am := router .NewAutherMiddleware (at )
85- r .authMiddleware = am
82+ func (r * httpRouter ) SetAuther (at router.Auther ) {
83+ r .auther = at
8684 return
8785}
8886
@@ -105,13 +103,14 @@ func (r *httpRouter) ServeHTTP(w http.ResponseWriter, req *http.Request) {
105103 r .r .ServeHTTP (response .NewResponse (w ), req )
106104}
107105
108- func (r * httpRouter ) GetEndpoints () []router.Entry {
109- entries := make ([]router.Entry , 0 , len (r .entries ))
106+ func (r * httpRouter ) GetEndpoints () * router.EntrySet {
107+ es := router .NewEntrySet ()
108+
110109 for i := range r .entries {
111- entries = append ( entries , r .entries [i ].Entry )
110+ es . AddEntry ( r .entries [i ].Entry )
112111 }
113112
114- return entries
113+ return es
115114}
116115
117116func (r * httpRouter ) EnableAPIRoot () {
@@ -139,29 +138,42 @@ func (r *httpRouter) debug(format string, args ...interface{}) {
139138func (r * httpRouter ) add (e * entry ) {
140139 var handler http.Handler
141140
142- if e .Protected && r .authMiddleware == nil {
141+ if e .Protected && r .auther == nil {
143142 r .debug ("add projected endpoint, but no auther set" )
144143 }
145144
146- if e .Protected && r .authMiddleware != nil {
147- handler = r .authMiddleware .Handler (e .h )
148- } else {
149- handler = http .HandlerFunc (e .h )
150- }
145+ handler = http .HandlerFunc (e .h )
151146
152- r .addHandler (e .Method , e .Path , handler )
147+ r .addHandler (e .Protected , e . Method , e .Path , handler )
153148 r .addEntry (e )
154149}
155150
156- func (r * httpRouter ) addHandler (method , path string , h http.Handler ) {
151+ func (r * httpRouter ) addHandler (protected bool , method , path string , h http.Handler ) {
157152 r .r .Handle (method , path ,
158153 func (w http.ResponseWriter , req * http.Request , ps httprouter.Params ) {
159- rc := & context.ReqContext {
160- PS : ps ,
154+ // 使用auther进行认证
155+ if protected && r .auther != nil {
156+ authInfo , err := r .auther .Auth (req .Header )
157+ if err != nil {
158+ response .Failed (w , err )
159+ return
160+ }
161+
162+ rc := context .GetContext (req )
163+ rc .AuthInfo = authInfo
164+ req = context .WithContext (req , rc )
161165 }
162- req = context .WithContext (req , rc )
163- h .ServeHTTP (w , req )
164166
167+ // 未包含和auth为nil
168+ if ! protected || r .auther == nil {
169+ rc := & context.ReqContext {
170+ PS : ps ,
171+ }
172+
173+ req = context .WithContext (req , rc )
174+ }
175+
176+ h .ServeHTTP (w , req )
165177 },
166178 )
167179}
0 commit comments