Web¶
Quick start¶
s := web.Default(":8080")
s.Get("/", func(c web.Context) error {
return c.Text("Hello, world.")
})
log.Fatal(s.Serve())
Route¶
- Register handler function
s.Post("/users", createUser)
s.Get("/users/:id", getUser)
s.Put("/users/:id", updateUser, web.WithName("user.update"))
s.Delete("/users/:id", deleteUser, web.WithName("user.delete"))
You can use Any
to register a handler for all HTTP methods. If you want to register it for some methods use Match
.
s.Any("/hello", hello)
s.Match([]string{"GET", "POST"}, "/users/:id", getUser)
- Register controller
package controller
import (
"github.com/cuigh/auxo/log"
"github.com/cuigh/auxo/net/web"
"github.com/cuigh/swirl/biz"
"github.com/cuigh/swirl/model"
)
// UserController is a controller of user
type UserController struct {
Index web.HandlerFunc `path:"/" name:"user.list" authorize:"!"`
New web.HandlerFunc `path:"/new" name:"user.new" authorize:"!"`
Create web.HandlerFunc `path:"/new" method:"post" name:"user.create" authorize:"!"`
Detail web.HandlerFunc `path:"/:name/detail" name:"user.detail" authorize:"!"`
Edit web.HandlerFunc `path:"/:name/edit" name:"user.edit" authorize:"!"`
Update web.HandlerFunc `path:"/:name/update" method:"post" name:"user.update" authorize:"!"`
Delete web.HandlerFunc `path:"/delete" method:"post" name:"user.delete" authorize:"!"`
}
// User creates an instance of UserController
func User() (c *UserController) {
return &UserController{
Index: userIndex,
New: userNew,
Create: userCreate,
Detail: userDetail,
Edit: userEdit,
Update: userUpdate,
Delete: userDelete,
Search: userSearch,
}
}
s.Handle("", controller.Home())
s.Handle("/profile", controller.Profile())
- Register static
s.File("/favicon.ico", filepath.Join(dir, "assets/favicon.ico"))
s.Static("/css", filepath.Join(dir, "assets/css"))
s.Static("/js", filepath.Join(dir, "assets/js"))
s.Static("/fonts", filepath.Join(dir, "assets/fonts"))