-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
5226fac
commit de134b7
Showing
1 changed file
with
38 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,38 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"fmt" | ||
) | ||
|
||
// 中间件 | ||
func logMiddle(next http.Handler) http.Handler{ | ||
return http.HandlerFunc(func(w http.ResponseWriter,r *http.Request){ | ||
fmt.Println("middle log") | ||
next.ServeHTTP(w,r) | ||
}) | ||
} | ||
// 中间件2 | ||
func hook(next http.Handler) http.Handler{ | ||
return http.HandlerFunc(func(w http.ResponseWriter,r *http.Request) { | ||
fmt.Println("hook log",r.URL.Path) | ||
next.ServeHTTP(w,r) | ||
}) | ||
} | ||
|
||
// 首页 | ||
func index(w http.ResponseWriter,r *http.Request){ | ||
if r.URL.Path != "/"{ | ||
return | ||
} | ||
w.Write([]byte("hello")) | ||
fmt.Println("index log") | ||
} | ||
|
||
func main() { | ||
|
||
http.Handle("/api",logMiddle(hook(http.HandlerFunc(index)))) | ||
http.Handle("/",hook(http.HandlerFunc(index))) | ||
|
||
http.ListenAndServe(":8000",nil) | ||
} |