This is a creation in Article, where the information may have evolved or changed.
gweb not a web framework, but a web scaffolding that you organize yourself? Also do not know how to call more appropriate, so the main purpose is to summarize the relevant knowledge. The third-party packages that are used are available in the project's
vendor folder. I also want to make a web framework, while summing up and moving in this direction.
Project background
At first I used to Python write Web services, as for Python to write code is what kind of experience, everyone has experience, when you are accustomed to using Python form resolution check, and then use Golang to do the same thing, for the first touch Golang of the program Ape, It fell from heaven to hell. The premise, of course, is to perform a form check gracefully. And not like the following example:
func Foo(w http.ResponseWriter req *http.Request) { req.ParseForm() bar := req.FormValue("bar") if bar == "" { // do some work io.WriteString(w, "param invalid") }}
Above is just a simple example (handwritten code, please forgive the mistake).
So what we want is: just write the business logic, define the form and the response body. Other work is offered to the other modules for unified processing. The final effect is posted here:
// 定义表单type HelloGetForm struct { Name string `schema:"name" valid:"Required" json:"name"` Age int `schema:"age" valid:"Required;Min(18)" json:"age"`}var PoolHelloGetForm = &sync.Pool{New: func() interface{} { return &HelloGetForm{} }}// 定义响应体type HelloGetResp struct { CodeInfo Tip string `json:"tip"`}var PoolHelloGetResp = &sync.Pool{New: func() interface{} { return &HelloGetResp{} }}// 业务逻辑处理func HelloGet(req *HelloGetForm) *HelloGetResp { resp := PoolHelloGetResp.Get().(*HelloGetResp) defer PoolHelloGetResp.Put(resp) resp.Tip = fmt.Sprintf("Get Hello, %s! your age[%d] is valid to access", req.Name, req.Age) // TODO: sleep over 10 *time.Second, test Response TimeOut time.Sleep(10 * time.Second) Response(resp, NewCodeInfo(CodeOk, "")) return resp}
Official opening
Put it on the demo, which includes the method of the RESTful API, the parsing of JSON-formatted data, and examples of file processing:
- Get
- Post
- Put
- Delete [No example at the moment]
- Jsonbody
- Files
Subsequent
I will put in gweb the various modules recently such as:
- Router/middleware/* & Router/router Implements a unified parsing, checksum response module, and the most important part
- Models is basically the use of ORM
- Mainc/servers Http Server & RPC Server
- The processing function of controller's business logic
- Logger's own web logger
- Sh/makefile Deploying Golang programs through Makefile
Welcome Fork
Github.com/yeqown/gweb