-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
183 lines (152 loc) · 3.78 KB
/
main.go
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package main
import (
"context"
"fmt"
"net/http"
"time"
"log/slog"
"github.com/gin-gonic/gin"
"github.com/parkingwang/igo"
"github.com/parkingwang/igo/pkg/http/code"
"github.com/parkingwang/igo/pkg/http/web"
)
// Something 你好
type Something struct {
Value string
}
var info = igo.AppInfo{
Name: "myname",
Description: "这是一个演示",
}
func main() {
igo.SetConfig("config.toml")
app := igo.New(info)
app.Provide(
// 添加一个构造函数 有些地方依赖它
// 具体可以参考go-uber/fx
func() *Something {
return &Something{Value: "nihao"}
},
)
app.Run(
// 简单的定时器服务示例
// 需要实现 igo.Servicer
// 依赖 Provide 提供 *Something
func(s *Something) igo.Servicer {
return &ticker{value: s.Value}
},
// web服务
func() igo.Servicer {
srv := app.CreateWebServer()
initRoutes(srv)
return srv
},
)
}
func initRoutes(srv *web.Server) {
r := srv.Router()
r.Get("/", Hello)
user := r.Group("/user")
user.Comment("user object")
user.Get("/", middleGinHandler, ListUser)
user.Get("/:id", web.CustomBindRequest(func(c *gin.Context) *UserIDReq {
req := &UserIDReq{
Comment: "重写请求",
}
req.Content.ID = 1
return req
}), GetUser).Comment("测试用 id 可以是 1,2,3,4 试试换成不同的值看看")
user.Post("/:id/add", CreateUser)
}
// UserInfo 用户信息
type UserInfo struct {
ID int `json:"id" uri:"id"`
Name string `json:"name" form:"name" comment:"备注再这里"`
X string `query:"x"`
}
// UserInfoListResponse 用户信息列表
type UserInfoListResponse struct {
Items []UserInfo `json:"items"`
}
// UserInfoListRequest 请求
type UserInfoListRequest struct {
Page int `form:"page" binding:"gte=0" comment:"第几页"`
PageSize int `form:"pageSize" binding:"gte=0,lte=100" comment:"每页条数"`
Keyword string `form:"keyword" comment:"按指定关键字查询"`
}
func ListUser(ctx context.Context, in *UserInfoListRequest) (*UserInfoListResponse, error) {
slog.InfoContext(ctx, "get users", "count", len(userlist))
if v := ctx.Value("value"); v != nil {
slog.InfoContext(ctx, "get middle value", "value", v)
}
return &UserInfoListResponse{Items: userlist}, nil
}
var userlist = []UserInfo{
{1, "afocus", ""},
{2, "umiko", ""},
{3, "tom", ""},
{4, "jack", ""},
}
type Content struct {
ID int `uri:"id" form:"id" binding:"required" comment:"用户id"`
}
type UserIDReq struct {
Content
Comment string `form:"comment"`
}
// GetUser 获取单个用户
func GetUser(ctx context.Context, in *UserIDReq) (*UserInfo, error) {
for _, user := range userlist {
if user.ID == in.ID {
return &user, nil
}
}
return nil, code.NewNotfoundError("user not found")
}
// Hello 通过使用gin.Context 可以突破rpc风格上的使用限制
func Hello(ctx context.Context, in *web.Empty) error {
c, ok := web.GinContext(ctx)
if ok {
fmt.Println(c.Request.URL.Hostname())
fmt.Println(c.Request.Host)
c.String(http.StatusOK, "hello,world")
}
return nil
}
func CreateUser(ctx context.Context, in *UserInfo) (*UserInfo, error) {
return in, nil
}
// 一个gin风格的中间件
func middleGinHandler(c *gin.Context) {
// 传递值
c.Set("value", "123")
slog.InfoContext(c, "start")
c.Next()
slog.InfoContext(c, "end")
}
// ////////////////////
// 自定义一个服务 实现igo.Servicer接口
type ticker struct {
t *time.Ticker
value string
log *slog.Logger
}
func (tk *ticker) Start(ctx context.Context) error {
log := slog.With("type", "ticker")
log.Info("start")
tk.log = log
tk.t = time.NewTicker(time.Second * 3)
var i int
go func() {
for range tk.t.C {
log.InfoContext(ctx, tk.value, "index", i)
i++
}
}()
return nil
}
func (tk *ticker) Stop(ctx context.Context) error {
tk.log.Info("ticker end")
tk.t.Stop()
return nil
}