Go-Spring v1.2.0 新增 web 服务器支持,底层路由器基于mux实现,旨在提供更加简单易用的开发体验。
特性:
path/header/cookie/from/body
绑定输入参数支持绑定的函数形式 (将下列函数转换为 http.HandlerFunc ):
友情提示:web 包不依赖 go-spring 容器,可以单独使用 (下方有例子) 😀
Demo 代码:
package main
import (
"context"
"log/slog"
"math/rand"
"mime/multipart"
"net/http"
"time"
"github.com/go-spring-projects/go-spring/gs"
"github.com/go-spring-projects/go-spring/web"
_ "github.com/go-spring-projects/go-spring/web/starter"
)
type Greeting struct {
Logger *slog.Logger `logger:""`
Server *web.Server `autowire:""`
}
func (g *Greeting) OnInit(ctx context.Context) error {
g.Server.Get("/greeting", g.Greeting)
g.Server.Get("/health", g.Health)
g.Server.Post("/user/register/{username}/{password}", g.Register)
g.Server.Post("/user/password", g.UpdatePassword)
return nil
}
func (g *Greeting) Greeting(ctx context.Context) string {
web.FromContext(ctx).
SetCookie("token", "1234567890", 500, "", "", false, false)
return "greeting!!!"
}
func (g *Greeting) Health(ctx context.Context) error {
if 0 == rand.Int()%2 {
return web.Error(400, "health check failed")
}
return nil
}
func (g *Greeting) Register(
ctx context.Context,
req struct {
Username string `path:"username" expr:"len($)>6 && len($)<20"`
Password string `path:"password" expr:"len($)>6 && len($)<20"`
HeadImg *multipart.FileHeader `form:"headImg"`
Captcha string `form:"captcha" expr:"len($)==4"`
UserAgent string `header:"User-Agent"`
Ad string `query:"ad"`
Token string `cookie:"token"`
},
) string {
g.Logger.Info("register user",
slog.String("username", req.Username),
slog.String("password", req.Password),
slog.String("userAgent", req.UserAgent),
slog.String("headImg", req.HeadImg.Filename),
slog.String("captcha", req.Captcha),
slog.String("userAgent", req.UserAgent),
slog.String("ad", req.Ad),
slog.String("token", req.Token),
)
return "ok"
}
func (g *Greeting) UpdatePassword(
ctx context.Context,
req struct {
Password string `json:"password" expr:"len($) > 6 && len($) < 20"` // password
Token string `cookie:"token"` // token
},
) string {
g.Logger.Info("change password", slog.String("password", req.Password))
return "ok"
}
func main() {
gs.Object(new(Greeting))
if err := gs.Run(); nil != err {
panic(err)
}
}
单独使用 web 服务例子:
package main
import (
"context"
"log/slog"
"mime/multipart"
"github.com/go-spring-projects/go-spring/web"
)
func Greeting(ctx context.Context) string {
return "Greeting!!!"
}
func Feedback(
ctx context.Context,
req struct {
Title string `form:"title"` // 反馈标题
Message string `form:"message"` // 反馈说明
Attachments []*multipart.FileHeader `form:"files"` // 上传附件文件列表
EMail string `form:"email"` // 联系邮箱
UserAgent string `header:"User-Agent"` // 浏览器用户代理
},
) string {
slog.Info("received user feedback",
slog.String("title", req.Title),
slog.String("message", req.Message),
slog.String("email", req.EMail),
slog.String("user-Agent", req.UserAgent),
)
//TODO: save feedback to database.
//
return "Thanks for your feedback!!!"
}
func main() {
webSvr := web.NewServer(web.NewRouter(), web.Options{Addr: ":8080"})
webSvr.Get("/greeting", Greeting)
webSvr.Post("/feedback", Feedback)
webSvr.Run()
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.