7Days Gee Day1
parent
04ef9336c9
commit
47b78747a5
|
@ -0,0 +1,3 @@
|
|||
module git.srv.ink/7DaysGee/base1
|
||||
|
||||
go 1.19
|
|
@ -0,0 +1,24 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 绑定路由规则,执行的函数
|
||||
http.HandleFunc("/", indexHander)
|
||||
http.HandleFunc("/hello", helloHandler)
|
||||
// 启动服务
|
||||
http.ListenAndServe(":8000", nil)
|
||||
}
|
||||
|
||||
func indexHander(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "Hello World")
|
||||
}
|
||||
|
||||
func helloHandler(w http.ResponseWriter, r *http.Request) {
|
||||
for k, v := range r.Header {
|
||||
fmt.Fprintf(w, "Header[%q] = %q", k, v)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
module git.srv.ink/7DaysGee/Day1/base2
|
||||
|
||||
go 1.19
|
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Engine struct{}
|
||||
|
||||
func (engine *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.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)
|
||||
}
|
||||
default:
|
||||
fmt.Fprintf(w, "404 NOT FOUND: %s", r.URL)
|
||||
}
|
||||
}
|
||||
func main() {
|
||||
engine := new(Engine)
|
||||
http.ListenAndServe(":8000", engine)
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package gee
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// HandlerFunc 路由匹配到后的具体函数
|
||||
type HandlerFunc func(w http.ResponseWriter, r *http.Request)
|
||||
|
||||
// Engine 路由表 实现了ServeHTTP接口
|
||||
type Engine struct {
|
||||
router map[string]HandlerFunc
|
||||
}
|
||||
|
||||
// ServeHTTP 实现了ServeHTTP接口
|
||||
func (engine *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
// 获取连接的请求方式和路径
|
||||
key := r.Method + "-" + r.URL.Path
|
||||
// 对比和匹配路由
|
||||
if handler, ok := engine.router[key]; ok {
|
||||
handler(w, r)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
_, _ = fmt.Fprintf(w, "404 NOT FOUND: %s", r.URL)
|
||||
}
|
||||
}
|
||||
|
||||
// Run 启动服务
|
||||
func (engine *Engine) Run(addr string) (err error) {
|
||||
return http.ListenAndServe(addr, engine)
|
||||
}
|
||||
|
||||
// 封装路由规则和具体的处理函数
|
||||
// addRoute 添加路由规则
|
||||
func (engine *Engine) addRoute(method string, pattern string, handler HandlerFunc) {
|
||||
key := method + "-" + pattern
|
||||
engine.router[key] = handler
|
||||
}
|
||||
|
||||
// GET 添加GET路由规则
|
||||
func (engine *Engine) GET(pattern string, handler HandlerFunc) {
|
||||
engine.addRoute("GET", pattern, handler)
|
||||
}
|
||||
|
||||
// POST 添加POST路由规则
|
||||
func (engine *Engine) POST(pattern string, handler HandlerFunc) {
|
||||
engine.addRoute("POST", pattern, handler)
|
||||
}
|
||||
|
||||
// New 实例化Engine
|
||||
func New() *Engine {
|
||||
return &Engine{router: make(map[string]HandlerFunc)}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
module gee
|
||||
|
||||
go 1.19
|
|
@ -0,0 +1,10 @@
|
|||
module git.srv.ink/7DaysGee/Day1/base3
|
||||
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
gee v0.0.1
|
||||
)
|
||||
replace (
|
||||
gee => ./gee
|
||||
)
|
|
@ -0,0 +1,6 @@
|
|||
go 1.19
|
||||
|
||||
use (
|
||||
.
|
||||
gee
|
||||
)
|
|
@ -0,0 +1,26 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gee"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r := gee.New()
|
||||
r.GET("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = w.Write([]byte("Hello World"))
|
||||
})
|
||||
r.GET("/hello", func(w http.ResponseWriter, r *http.Request) {
|
||||
for k, v := range r.Header {
|
||||
_, _ = fmt.Fprintf(w, "Header[%q] = %q", k, v)
|
||||
}
|
||||
})
|
||||
r.POST("/login", func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = fmt.Fprintf(w, "POST login")
|
||||
})
|
||||
err := r.Run(":9000")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
module git.srv.ink/7DaysGee
|
||||
|
||||
go 1.19
|
Loading…
Reference in New Issue