From 97774d1cd7720414985ed72ed5972930338322bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8B=AC=E5=AD=A4=E4=BC=B6=E4=BF=9C?= <1184662350@qq.com> Date: Sat, 4 Feb 2023 19:00:36 +0800 Subject: [PATCH] =?UTF-8?q?7Days=20Gee=20Day1=20=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E9=83=A8=E5=88=86=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day1/base2/main.go | 24 ++++++++++++++++-------- day1/base3/gee/gee.go | 8 +++++++- day1/base3/main.go | 7 ++++++- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/day1/base2/main.go b/day1/base2/main.go index f1cb24a..159ab23 100644 --- a/day1/base2/main.go +++ b/day1/base2/main.go @@ -1,25 +1,33 @@ package main import ( - "fmt" "net/http" ) type Engine struct{} func (engine *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) { - switch r.URL.Path { + path := r.URL.Path + switch path { case "/": - fmt.Fprintf(w, "URL.Path = %q", r.URL.Path) - case "/hello": - for k, v := range r.Header { - fmt.Fprintf(w, "Header[%q] = %q", k, v) - } + _, _ = w.Write([]byte("Hello Go")) + case "/gee": + _, _ = w.Write([]byte("Gee!!!")) default: - fmt.Fprintf(w, "404 NOT FOUND: %s", r.URL) + _, _ = w.Write([]byte("天啊, 404啦!!!!")) } } + func main() { engine := new(Engine) http.ListenAndServe(":8000", engine) } + +//// 自定义ServeMux +//func main() { +// mux := new(http.ServeMux) +// mux.HandleFunc("/hello", func(writer http.ResponseWriter, request *http.Request) { +// _, _ = writer.Write([]byte("Hello Go")) +// }) +// _ = http.ListenAndServe(":8000", mux) +//} diff --git a/day1/base3/gee/gee.go b/day1/base3/gee/gee.go index de43842..d78e6e2 100644 --- a/day1/base3/gee/gee.go +++ b/day1/base3/gee/gee.go @@ -2,6 +2,7 @@ package gee import ( "fmt" + "log" "net/http" ) @@ -18,12 +19,15 @@ func (engine *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) { // 获取连接的请求方式和路径 key := r.Method + "-" + r.URL.Path // 对比和匹配路由 - if handler, ok := engine.router[key]; ok { + handler, ok := engine.router[key] + if ok { handler(w, r) } else { w.WriteHeader(http.StatusNotFound) _, _ = fmt.Fprintf(w, "404 NOT FOUND: %s", r.URL) } + // 这里我们加上时间,容易理解 + log.Println("请求方式:", r.Method, r.URL.Path, "对应:", key, "是否对应上:", ok) } // Run 启动服务 @@ -36,6 +40,8 @@ func (engine *Engine) Run(addr string) (err error) { func (engine *Engine) addRoute(method string, pattern string, handler HandlerFunc) { key := method + "-" + pattern engine.router[key] = handler + // 打印对应的 key + fmt.Println("请求方式:", method, pattern, " 对应: ", key) } // GET 添加GET路由规则 diff --git a/day1/base3/main.go b/day1/base3/main.go index bf747bf..62216da 100644 --- a/day1/base3/main.go +++ b/day1/base3/main.go @@ -7,19 +7,24 @@ import ( ) func main() { + // 创建 Engine 实例 r := gee.New() + // 增加一个 GET 路由, path 为 / r.GET("/", func(w http.ResponseWriter, r *http.Request) { _, _ = w.Write([]byte("Hello World")) }) + // 增加一个 GET 路由, path 为 /hello r.GET("/hello", func(w http.ResponseWriter, r *http.Request) { for k, v := range r.Header { _, _ = fmt.Fprintf(w, "Header[%q] = %q", k, v) } }) + // 增加一个 POST 路由, path 为 /login r.POST("/login", func(w http.ResponseWriter, r *http.Request) { _, _ = fmt.Fprintf(w, "POST login") }) - err := r.Run(":9000") + // 创建服务端口的监听并开启 Http 服务 + err := r.Run(":8000") if err != nil { return }