GoFrame 框架培训大纲 - 第13章:GoFrame Web开发
13.1 Web基础
13.1.1 HTTP协议详解
type HTTPRequest struct {
Method string
URL *url.URL
Proto string
Header http.Header
Body io.ReadCloser
ContentLength int64
Host string
RemoteAddr string
}
type HTTPResponse struct {
StatusCode int
Status string
Proto string
Header http.Header
Body io.ReadCloser
ContentLength int64
}
type Resource struct {
ID string
Name string
CreatedAt time.Time
UpdatedAt time.Time
Links map[string]string
}
13.1.2 Web应用架构
type WebApplication struct {
Router *Router
Controllers []Controller
Services []Service
Repository Repository
Middleware []Middleware
}
type OnionArchitecture struct {
Presentation Layer
Application Layer
Domain Layer
Infrastructure Layer
}
13.2 路由与控制器
13.2.1 路由配置
package main
import (
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
func main() {
s := g.Server()
s.Group("/", func(group *ghttp.RouterGroup) {
group.GET("/users", getUserList)
group.POST("/users", createUser)
group.GET("/users/:id", getUserByID)
group.PUT("/users/:id", updateUser)
group.DELETE("/users/:id", deleteUser)
})
s.Group("/api", func(group *ghttp.RouterGroup) {
group.Bind([]*ghttp.GroupRouter{
{
Type: "GET",
Route: "/products",
Handler: getProductList,
},
})
})
s.Run()
}
type UserController struct {
g.Controller
}
func (c *UserController) Index() {
c.Response.WriteJson(g.Map{
"message": "User list",
})
}
func (c