add: 模仿gin框架

This commit is contained in:
洪晓威 2022-02-13 11:34:46 +08:00
parent 500c147acb
commit 6633420cf6
6 changed files with 158 additions and 0 deletions

View File

@ -0,0 +1,67 @@
package gee
import (
"encoding/json"
"fmt"
"net/http"
)
type H map[string]interface{}
type Context struct {
Writer http.ResponseWriter
Req *http.Request
Path string
Method string
StatusCode int
}
func newContext(w http.ResponseWriter, r *http.Request) *Context {
return &Context{
Writer: w,
Req: r,
Path: r.URL.Path,
Method: r.Method,
}
}
func (c *Context) PostForm(key string) string {
return c.Req.FormValue(key)
}
func (c *Context) Query(key string) string {
return c.Req.URL.Query().Get(key)
}
func (c *Context) Status(code int) {
c.StatusCode = code
c.Writer.WriteHeader(code)
}
func (c *Context) SetHeader(key string, value string) {
c.Writer.Header().Set(key, value)
}
func (c *Context) String(code int, format string, values ...interface{}) {
c.SetHeader("Content-Type", "text/plain")
c.Status(code)
_, _ = c.Writer.Write([]byte(fmt.Sprintf(format, values...)))
}
func (c *Context) JSON(code int, obj interface{}) {
c.SetHeader("Content-Type", "application/json")
c.Status(code)
encoder := json.NewEncoder(c.Writer)
if err := encoder.Encode(obj); err != nil {
http.Error(c.Writer, err.Error(), 500)
}
}
func (c *Context) Data(code int, data []byte) {
c.Status(code)
_, _ = c.Writer.Write(data)
}
func (c *Context) HTML(code int, html string) {
c.SetHeader("Content-Type", "text/html")
c.Status(code)
_, _ = c.Writer.Write([]byte(html))
}

View File

@ -0,0 +1,38 @@
package gee
import (
"net/http"
)
type HandlerFunc func(*Context)
type Engine struct {
router *router
}
func New() *Engine {
return &Engine{
router: newRouter(),
}
}
func (e *Engine) addRoute(method string, pattern string, handler HandlerFunc) {
e.router.addRoute(method, pattern, handler)
}
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) {
context := newContext(writer, request)
e.router.handle(context)
}

View File

@ -0,0 +1,29 @@
package gee
import (
"log"
"net/http"
)
type router struct {
handlers map[string]HandlerFunc
}
func newRouter() *router {
return &router{handlers: make(map[string]HandlerFunc)}
}
func (r *router) addRoute(method string, pattern string, handler HandlerFunc) {
log.Printf("[Route ] %4s - %s", method, pattern)
key := method + "-" + pattern
r.handlers[key] = handler
}
func (r *router) handle(c *Context) {
key := c.Method + "-" + c.Path
if handler, ok := r.handlers[key]; ok {
handler(c)
} else {
c.String(http.StatusNotFound, "404 not found:%s\n", c.Path)
}
}

View File

@ -0,0 +1,22 @@
package main
import (
"golanglearn/web/framework/framework-context/gee"
)
func main() {
engine := gee.New()
engine.GET("/", func(c *gee.Context) {
c.HTML(200, "<h1>hello</h1>")
})
engine.GET("/hello", func(c *gee.Context) {
c.String(200, "hello,%v", c.Query("name"))
})
engine.POST("/login", func(c *gee.Context) {
c.JSON(200, gee.H{
"username": "xxxx",
"password": "1234",
})
})
_ = engine.Run(":9000")
}

View File

@ -0,0 +1 @@
package gee

View File

@ -0,0 +1 @@
package framework