1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| package main
import ( "encoding/json" "fmt" "io" "net/http" )
/***********************服务器抽象***********************/ // Server 抽象服务器 type Server interface { // Route 设定一个路由,命中该路由的会执行handleFunc的代码 Route(pattern string, handleFunc http.HandlerFunc)
// Start 启动我们的服务器 Start(address string) error }
/***********************服务器实现***********************/ type sdkHttpServer struct { Name string // Name 服务器应用名称查看方法 NewSdkHttpServer }
// Route 设定一个路由,命中该路由的会执行handleFunc的代码 实现了Server接口 func (server *sdkHttpServer) Route(pattern string, handleFunc http.HandlerFunc) { http.HandleFunc(pattern, handleFunc) }
// Start 启动我们的服务器 func (server *sdkHttpServer) Start(address string) error { return http.ListenAndServe(":8080", nil) }
// NewSdkHttpServer 新建名为name的sdkHttpServer func NewSdkHttpServer(name string) Server { return &sdkHttpServer{Name: name} }
/********************HttpContext抽象********************/ type Context struct { writer http.ResponseWriter request *http.Request }
// NewContext 构造方法 func NewContext(writer http.ResponseWriter, request *http.Request) *Context { return &Context{writer: writer, request: request} }
// ReadJson 读取request请求发过来的json数据 func (context *Context) ReadJson(data interface{}) error { body, err := io.ReadAll(context.request.Body) //读流错误 if err != nil { return err } return json.Unmarshal(body, data) }
// WriteJson 响应Json数据 func (context *Context) WriteJson(status int, data interface{}) error { bytes, err := json.Marshal(data) if err != nil { return err } _, err = context.writer.Write(bytes) if err != nil { return err } context.writer.WriteHeader(status) return nil }
// OkJson 请求成功 func (context Context) OkJson(data interface{}) error { return context.WriteJson(http.StatusOK, data) }
// SystemErrJson 系统内部异常 func (context Context) SystemErrJson(data interface{}) error { return context.WriteJson(http.StatusInternalServerError, data) }
// BadRequestJson 错误的请求数据 func (context Context) BadRequestJson(data interface{}) error { return context.WriteJson(http.StatusBadRequest, data) }
/**********************响应实体封装**********************/ // commonResponse 公共的响应结果包装 type commonResponse struct { Code int `json:"code"` Msg string `json:"msg"` Data interface{} `json:"data"` }
/***********************请求的实体***********************/ type signUpReq struct { Email string `json:"email"` Password string `json:"password"` ConfirmedPassword string `json:"confirmed_password"` }
/**********************路由处理方法**********************/ // handler func handler(writer http.ResponseWriter, request *http.Request) { fmt.Fprintf(writer, "Hi there, I love %s!", request.URL.Path) }
// parseForm func parseForm(writer http.ResponseWriter, request *http.Request) { fmt.Fprintf(writer, "before parse form %v\n", request.Form) err := request.ParseForm() if err != nil { fmt.Fprintf(writer, "parse form error %v\n", err) } fmt.Fprintf(writer, "before parse form %v", request.Form) }
// signUp 用户注册 func signUp(writer http.ResponseWriter, request *http.Request) { context := NewContext(writer, request) req := &signUpReq{} err := context.ReadJson(req) // 没有读到json请求错误 if err != nil { context.BadRequestJson(&commonResponse{ Code: 400, Msg: fmt.Sprintf("intalid request %v", err), }) return } _ = context.OkJson(&commonResponse{Code: 0, Msg: "success", Data: req}) }
// main 程序入口 func main() { httpServer := NewSdkHttpServer("test") httpServer.Route("/", handler) httpServer.Route("/parseForm", parseForm) httpServer.Route("/signUp", signUp) httpServer.Start(":8080") }
|