In Go a simple implementation of HTTP service middleware

Usually a simple http services framework, is to register a bunch of routes, and then call a different logic to deal with the routing.

But it may actually have almost all of the routing number of a unified process for all involved, such as logging, such as permissions and so on.

Well, this time to engage in a middle preprocessing, it is a good idea.

A middleware unit is defined:

 1 package main
 2 
 3 import (
 4     "net/http"
 5 )
 6 
 7 // AdaptorHandle middleware func type
 8 type AdaptorHandle func(w http.ResponseWriter, r *http.Request) (next bool, err error)
 9 
10 // MiddleWareAdaptor router middlewares mapped by url
11 type MiddleWareAdaptor struct {
12     URLs map[string][]AdaptorHandle
13 }
14 
15 // MakeMiddleWareAdaptor make a middleware adaptor
16 func MakeMiddleWareAdaptor() *MiddleWareAdaptor {
17     mwa := &MiddleWareAdaptor{
18         URLs: make(map[string][]AdaptorHandle),
19     }
20 
21     return mwa
22 }
23 
24 // Regist regist a adaptor
25 func (mw *MiddleWareAdaptor) Regist(url string, Adaptor ...AdaptorHandle) {
26     for _, adp := range Adaptor {
27         mw.URLs[url] = append(mw.URLs[url], adp)
28         // mw.URLs[url] = adp
29     }
30 }
31 
32 // Exec exec middleware adaptor funcs...
33 func (mw *MiddleWareAdaptor) Exec(url string, w http.ResponseWriter, r *http.Request) (bool, error) {
34     if adps, ok := mw.URLs[url]; ok {
35         for _, adp := range adps {
36             if next, err := adp(w, r); !next || (err != nil) {
37                 return next, err
38             }
39         }
40     }
41     return true, nil
42 }

The routing function is then treated with intermediate inlet wrap:

 1 func middlewareHandler(next http.Handler) http.Handler {
 2     return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
 3         // before call handler
 4         start := time.Now()
 5         do, _ := mwa.Exec(r.URL.Path, w, r) // exec middleware
 6         // call next handler
 7         if do {
 8             log.Println("middleware done. next...")
 9             next.ServeHTTP(w, r)
10         } else {
11             log.Println("middleware done.break...")
12         }
13         // after call handle
14         log.Printf("Comleted %s in %v", r.URL.Path, time.Since(start))
15     })
16 }
17 
18 mux.Handle("/", middlewareHandler(&uPlusRouterHandler{}))
19 
20 type uPlusRouterHandler struct {
21 }
22 
23 func (rh *uPlusRouterHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
24 ...
25 }
 

Finally, you need to register middleware:

mwa = MakeMiddleWareAdaptor() // init middleware
mwa.Regist("/", testMWAfunc, testMWAfunc2) // regist middleware
...
func testMWAfunc(w http.ResponseWriter, r *http.Request) (bool, error) {
    log.Println("我是第一个中间件...")
    log.Printf("Started %s %s", r.Method, r.URL.Path)
    return true, nil
}

func testMWAfunc2(w http.ResponseWriter, r *http.Request) (bool, error) {
    log.Println("I was the second middleware ... " )
     return  false , nil // return false, BREAK the Follow-up Actions. 
}

Although the code is simple to record an idea.

Guess you like

Origin www.cnblogs.com/codingnote/p/11265527.html