Compare commits
No commits in common. "6633420cf67b9966525c1a28041ae41d01f6d0a1" and "acadb8457fb21738f02c7d12a2b10c678b0103ca" have entirely different histories.
6633420cf6
...
acadb8457f
@ -1,26 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
|
|
||||||
// 路由1
|
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
fmt.Fprint(w, "hello basic web!")
|
|
||||||
})
|
|
||||||
|
|
||||||
// 路由2
|
|
||||||
http.HandleFunc("/xiaowei", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
headers := r.Header
|
|
||||||
for k, v := range headers {
|
|
||||||
fmt.Fprintf(w, "[%v]-[%v]\n", k, v)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
// 启动一个httpserver
|
|
||||||
if err := http.ListenAndServe(":9000", nil); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,67 +0,0 @@
|
|||||||
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))
|
|
||||||
}
|
|
@ -1,38 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
@ -1,29 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
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")
|
|
||||||
}
|
|
@ -1 +0,0 @@
|
|||||||
package gee
|
|
@ -1 +0,0 @@
|
|||||||
package framework
|
|
@ -1,29 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Engine 定义一个结构体
|
|
||||||
type Engine struct {
|
|
||||||
}
|
|
||||||
|
|
||||||
// 实现Handler的ServerHTTP方法
|
|
||||||
func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
||||||
switch r.RequestURI {
|
|
||||||
case "/":
|
|
||||||
fmt.Fprint(w, "hello handler-basic")
|
|
||||||
case "/web":
|
|
||||||
for k, v := range r.Header {
|
|
||||||
fmt.Fprintln(w, k, "-->", v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
engine := new(Engine)
|
|
||||||
if err := http.ListenAndServe(":9000", engine); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user