add: 模仿gin框架
This commit is contained in:
parent
6633420cf6
commit
ea348b07a6
@ -13,9 +13,13 @@ func main() {
|
|||||||
c.String(200, "hello,%v", c.Query("name"))
|
c.String(200, "hello,%v", c.Query("name"))
|
||||||
})
|
})
|
||||||
engine.POST("/login", func(c *gee.Context) {
|
engine.POST("/login", func(c *gee.Context) {
|
||||||
|
//c.JSON(200, gee.H{
|
||||||
|
// "username": "xxxx",
|
||||||
|
// "password": "1234",
|
||||||
|
//})
|
||||||
c.JSON(200, gee.H{
|
c.JSON(200, gee.H{
|
||||||
"username": "xxxx",
|
"username": c.PostForm("username"),
|
||||||
"password": "1234",
|
"password": c.PostForm("password"),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
_ = engine.Run(":9000")
|
_ = engine.Run(":9000")
|
||||||
|
@ -1 +1,46 @@
|
|||||||
package gee
|
package gee
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type HandlerFunc func(w http.ResponseWriter, r *http.Request)
|
||||||
|
|
||||||
|
type Engine struct {
|
||||||
|
router map[string]HandlerFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() *Engine {
|
||||||
|
return &Engine{
|
||||||
|
router: make(map[string]HandlerFunc),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) addRoute(method string, pattern string, handler HandlerFunc) {
|
||||||
|
key := method + "-" + pattern
|
||||||
|
e.router[key] = handler
|
||||||
|
log.Println(e.router)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) GET(pattern string, handler HandlerFunc) {
|
||||||
|
e.addRoute("GET", pattern, handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) POST(pattern string, handler HandlerFunc) {
|
||||||
|
e.addRoute("POST", pattern, handler)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) Run(addr string) error {
|
||||||
|
return http.ListenAndServe(addr, e)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *Engine) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
|
||||||
|
key := request.Method + "-" + request.URL.Path
|
||||||
|
if handler, ok := e.router[key]; ok {
|
||||||
|
handler(writer, request)
|
||||||
|
} else {
|
||||||
|
_, _ = fmt.Fprintf(writer, "404 not found:%v\n", request.URL)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1 +1,19 @@
|
|||||||
package framework
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"golanglearn/web/framework/framework/gee"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
engine := gee.New()
|
||||||
|
engine.GET("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintf(w, "hello web frame work")
|
||||||
|
})
|
||||||
|
|
||||||
|
engine.GET("/hello", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintf(w, "hello web frame work")
|
||||||
|
})
|
||||||
|
engine.Run(":9000")
|
||||||
|
}
|
||||||
|
@ -11,7 +11,7 @@ type Engine struct {
|
|||||||
|
|
||||||
// 实现Handler的ServerHTTP方法
|
// 实现Handler的ServerHTTP方法
|
||||||
func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
switch r.RequestURI {
|
switch r.URL.Path {
|
||||||
case "/":
|
case "/":
|
||||||
fmt.Fprint(w, "hello handler-basic")
|
fmt.Fprint(w, "hello handler-basic")
|
||||||
case "/web":
|
case "/web":
|
||||||
|
Loading…
Reference in New Issue
Block a user