Skip to content

Commit b066c94

Browse files
committed
Adjust the code paths under the examples/ directory to prevent the redefinition of main.
1 parent d44576b commit b066c94

File tree

3 files changed

+156
-156
lines changed

3 files changed

+156
-156
lines changed

examples/zinx_routerSlices/router_func_server/routerFuncApi.go

Lines changed: 0 additions & 80 deletions
This file was deleted.

examples/zinx_routerSlices/router_func_server/server.go

Lines changed: 58 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -2,97 +2,79 @@ package main
22

33
import (
44
"fmt"
5-
65
"github.com/aceld/zinx/zconf"
76
"github.com/aceld/zinx/ziface"
87
"github.com/aceld/zinx/znet"
98
)
109

11-
func Test1(request ziface.IRequest) {
12-
fmt.Println("test1")
13-
}
14-
func Test2(request ziface.IRequest) {
15-
fmt.Println("Test2")
16-
}
17-
func Test3(request ziface.IRequest) {
18-
fmt.Println("Test3")
19-
}
20-
func Test4(request ziface.IRequest) {
21-
fmt.Println("Test4")
22-
}
23-
func Test5(request ziface.IRequest) {
24-
fmt.Println("Test5")
25-
}
26-
func Test6(request ziface.IRequest) {
27-
fmt.Println("Test6")
28-
}
10+
func Auth1(request ziface.IRequest) {
2911

30-
type router struct {
31-
znet.BaseRouter
12+
// Verify business, default to pass. (验证业务 默认固定放行)
13+
fmt.Println("I am the Auth1, I will always pass.")
14+
// I am validation handler 1, and I must pass.
15+
// Note that the next function will start executing and return here after all functions are executed.
16+
// (注意是进入下一个函数开始执行,全部执行完后 回到此处)
17+
request.RouterSlicesNext()
3218
}
3319

34-
func (r *router) PreHandle(req ziface.IRequest) {
35-
fmt.Println(" hello router1")
20+
func Auth2(request ziface.IRequest) {
21+
// I am the validation handler 2, and by default, I do not allow the request to pass.(验证业务 默认固定不放行)
22+
23+
// Terminate execution function, no more handlers will be executed after this one.(终结执行函数,再这个处理器结束后不会在执行后面的处理器)
24+
request.Abort()
25+
fmt.Println("I am the Auth2, I will definitely not pass.")
26+
fmt.Println("The business terminates here and the subsequent handlers will not be executed.")
3627
}
37-
func (r *router) Handle(req ziface.IRequest) {
38-
req.Abort()
39-
fmt.Println(" hello router2")
28+
29+
func Auth3(request ziface.IRequest) {
30+
31+
fmt.Println("I am the group validation function.")
4032
}
41-
func (r *router) PostHandle(req ziface.IRequest) {
42-
fmt.Println(" hello router3")
33+
34+
// I am a business function.
35+
func TestFunc(request ziface.IRequest) {
36+
fmt.Println("I am a business function.")
4337
}
4438

4539
func main() {
4640

47-
// Old version router method (旧版本路由方法)
48-
//{
49-
// server := znet.NewUserConfServer(&zconf.Config{TCPPort: 8999, Host: "127.0.0.1"})
50-
//
51-
// // Even without manually calling the router mode, the default is 1 (old version) 即使不手动调路由模式也可以,默认是1(旧版本)
52-
// //server := znet.NewServer()
53-
//
54-
// // Old version runs normally(旧版正常执行)
55-
// r := &router{}
56-
// server.AddRouter(1, r)
57-
// server.Serve()
58-
//}
59-
//{
60-
61-
// New version usage and explanation(新版本使用方法以及说明)
41+
// New version usage and explanation.(新版本使用方法以及说明)
42+
server := znet.NewUserConfServer(&zconf.Config{RouterSlicesMode: true, TCPPort: 8999, Host: "127.0.0.1"})
43+
44+
// Simulation scenario 1: A normal business that only executes a single operation function separately.
45+
// 模拟场景 1,普通业务单独只执行一个操作函数
46+
//server.AddRouterSlices(1, TestFunc)
47+
48+
// Simulated scenario 2: All operations below require verification of request permissions, so a verification function is needed.
49+
// the verification component has been added to all the routes under the use method, such as 1 and 2.
50+
// 模拟场景 2, 以下所有操作都需要验证请求权限,所以需要一个验证函数
51+
// 将验证组件添加到了所有再use方法下的所有路由中了 如1,2 中都会带有
52+
//routerSlices := server.Use(Auth1)
53+
//routerSlices.AddHandler(1, TestFunc)
54+
//routerSlices.AddHandler(2, TestFunc)
55+
56+
// Equivalent to the following:(等价于下面:)
57+
//routerSlices.AddHandler(1, Auth1, TestFunc)
58+
59+
// Simulated scenario 3: Authorization is required, but some route operations require additional verification.
60+
// 模拟场景3 需要权限,但是某些路由操作需要更多额外验证
61+
server.Use(Auth1)
62+
group1 := server.Group(1, 2, Auth3)
6263
{
63-
server := znet.NewUserConfServer(&zconf.Config{RouterSlicesMode: true, TCPPort: 8999, Host: "127.0.0.1"})
64-
// Grouping(分组)
65-
group := server.Group(3, 10, Test1)
66-
67-
// Add router. Will panic if not within the group range.(添加路由 如果不在组范围会直接panic)
68-
//group.AddHandler(11, Test2)
69-
70-
// Within the group, not affected by Use, has processors 1 and 2.(在组中 不受Use影响 有 1 2 处理器)
71-
group.AddHandler(3, Test2)
72-
73-
// Not within the group and before Use, only has its own processor 3.(既不在组里也在Use之前只会有自己的处理器 3)
74-
server.AddRouterSlices(1, Test3)
75-
76-
// If you want the group processor to have priority, you should do the following before Use.
77-
// You can manually add it via group.AddHandler(5, Test4, Test5,Test2, Test3, Test6)
78-
// or use the Group's Use method as shown below, which would have the order of 1 4 5 6 and not be affected by Use.
79-
// 如果希望group处理器优先,应当在Use之前如下操作
80-
// 可以手动添加 入 group.AddHandler(5, Test4, Test5,Test2, Test3, Test6)
81-
// 或者如下使用Group的Use方法 那么就是 1 4 5 6的顺序 不被use影响
82-
group.Use(Test2, Test3)
83-
group.AddHandler(5, Test4, Test5, Test6)
84-
85-
// Common components, but not affected by the groups or routers before Use. (公共组件,但是,在使用Use之前的组或者路由不会影响到)
86-
router := server.Use(Test4, Test5)
87-
// Add router. Not within the group but is affected by Use, has processors 4, 5, and 6.
88-
// (添加路由 不在组中但是收Use影响 有4 5 6处理器)
89-
router.AddHandler(2, Test6)
90-
91-
// Within the group and affected by Use, has all processors in the order of 4, 5, 1, 2, 3, 6 because the processors in Use are always at the forefront.
92-
// (在组里也受到Use影响 有所有处理器 且顺序应该是 4 5 1 2 3 6 因为use中的处理器始终在最前端)
93-
group.AddHandler(4, Test6)
94-
95-
server.Serve()
64+
// MsgId=1, there will be Auth3 and Auth1. (1中就会有Auth3和Auth1)
65+
group1.AddHandler(1, TestFunc)
66+
67+
// More specific scenario: Some operations within the group require an additional validation process.
68+
// 更特殊的情况,组内另一些操作还需要另一道校验处理
69+
group1.Use(Auth2)
70+
// MsgId=2, Auth3 and Auth1 will be added to all routes under the use method.
71+
// 2中就会有Auth1和Auth3以及Auth2
72+
group1.AddHandler(2, TestFunc)
73+
9674
}
75+
// MsgId=3, Auth3 will not be included. (3中就不会有Auth3)
76+
server.AddRouterSlices(3, TestFunc)
77+
78+
server.Serve()
9779

9880
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/aceld/zinx/zconf"
7+
"github.com/aceld/zinx/ziface"
8+
"github.com/aceld/zinx/znet"
9+
)
10+
11+
func Test1(request ziface.IRequest) {
12+
fmt.Println("test1")
13+
}
14+
func Test2(request ziface.IRequest) {
15+
fmt.Println("Test2")
16+
}
17+
func Test3(request ziface.IRequest) {
18+
fmt.Println("Test3")
19+
}
20+
func Test4(request ziface.IRequest) {
21+
fmt.Println("Test4")
22+
}
23+
func Test5(request ziface.IRequest) {
24+
fmt.Println("Test5")
25+
}
26+
func Test6(request ziface.IRequest) {
27+
fmt.Println("Test6")
28+
}
29+
30+
type router struct {
31+
znet.BaseRouter
32+
}
33+
34+
func (r *router) PreHandle(req ziface.IRequest) {
35+
fmt.Println(" hello router1")
36+
}
37+
func (r *router) Handle(req ziface.IRequest) {
38+
req.Abort()
39+
fmt.Println(" hello router2")
40+
}
41+
func (r *router) PostHandle(req ziface.IRequest) {
42+
fmt.Println(" hello router3")
43+
}
44+
45+
func main() {
46+
47+
// Old version router method (旧版本路由方法)
48+
//{
49+
// server := znet.NewUserConfServer(&zconf.Config{TCPPort: 8999, Host: "127.0.0.1"})
50+
//
51+
// // Even without manually calling the router mode, the default is 1 (old version) 即使不手动调路由模式也可以,默认是1(旧版本)
52+
// //server := znet.NewServer()
53+
//
54+
// // Old version runs normally(旧版正常执行)
55+
// r := &router{}
56+
// server.AddRouter(1, r)
57+
// server.Serve()
58+
//}
59+
//{
60+
61+
// New version usage and explanation(新版本使用方法以及说明)
62+
{
63+
server := znet.NewUserConfServer(&zconf.Config{RouterSlicesMode: true, TCPPort: 8999, Host: "127.0.0.1"})
64+
// Grouping(分组)
65+
group := server.Group(3, 10, Test1)
66+
67+
// Add router. Will panic if not within the group range.(添加路由 如果不在组范围会直接panic)
68+
//group.AddHandler(11, Test2)
69+
70+
// Within the group, not affected by Use, has processors 1 and 2.(在组中 不受Use影响 有 1 2 处理器)
71+
group.AddHandler(3, Test2)
72+
73+
// Not within the group and before Use, only has its own processor 3.(既不在组里也在Use之前只会有自己的处理器 3)
74+
server.AddRouterSlices(1, Test3)
75+
76+
// If you want the group processor to have priority, you should do the following before Use.
77+
// You can manually add it via group.AddHandler(5, Test4, Test5,Test2, Test3, Test6)
78+
// or use the Group's Use method as shown below, which would have the order of 1 4 5 6 and not be affected by Use.
79+
// 如果希望group处理器优先,应当在Use之前如下操作
80+
// 可以手动添加 入 group.AddHandler(5, Test4, Test5,Test2, Test3, Test6)
81+
// 或者如下使用Group的Use方法 那么就是 1 4 5 6的顺序 不被use影响
82+
group.Use(Test2, Test3)
83+
group.AddHandler(5, Test4, Test5, Test6)
84+
85+
// Common components, but not affected by the groups or routers before Use. (公共组件,但是,在使用Use之前的组或者路由不会影响到)
86+
router := server.Use(Test4, Test5)
87+
// Add router. Not within the group but is affected by Use, has processors 4, 5, and 6.
88+
// (添加路由 不在组中但是收Use影响 有4 5 6处理器)
89+
router.AddHandler(2, Test6)
90+
91+
// Within the group and affected by Use, has all processors in the order of 4, 5, 1, 2, 3, 6 because the processors in Use are always at the forefront.
92+
// (在组里也受到Use影响 有所有处理器 且顺序应该是 4 5 1 2 3 6 因为use中的处理器始终在最前端)
93+
group.AddHandler(4, Test6)
94+
95+
server.Serve()
96+
}
97+
98+
}

0 commit comments

Comments
 (0)