@@ -19,27 +19,31 @@ import "github.com/issue9/web/config"
19
19
20
20
// main.go
21
21
func main () {
22
- w , _ := config. Classic ( " ./appconfig/logs.xml " , " ./appconfig/ web.yaml " )
22
+ srv , _ := web. NewServer (& web.Options {} )
23
23
24
24
// 注册模块信息
25
25
m1.Init ()
26
26
m2.Init ()
27
27
28
- w .Serve ()
28
+ srv .Serve ()
29
29
}
30
30
31
31
// modules/m1/module.go
32
- func Init (s *web .Server ) {
33
- s .NewModule (" m1" , " 模块描述信息" ).
32
+ func Init (s *web .Server ) error {
33
+ m := web .NewModule (" m1" , " 模块描述信息" ).
34
34
Get (" /admins" , getAdmins).
35
35
Get (" /groups" , getGroups)
36
+
37
+ return s.AddModule (m)
36
38
}
37
39
38
40
// modules/m2/module.go
39
- func Init (s *web .Server ) {
40
- s .NewModule (" m2" , " 模块描述信息" , " m1" ).
41
+ func Init (s *web .Server ) error {
42
+ m := web .NewModule (" m2" , " 模块描述信息" , " m1" ).
41
43
Get (" /admins" , getAdmins).
42
44
Get (" /groups" , getGroups)
45
+
46
+ return s.AddModule (m)
43
47
}
44
48
```
45
49
@@ -57,8 +61,8 @@ package m1
57
61
58
62
import " github.com/issue9/web"
59
63
60
- func Init (s *web .Web ) {
61
- m := s .NewModule (" test" , " 测试模块" )
64
+ func Init (s *web .Web ) error {
65
+ m := web .NewModule (" test" , " 测试模块" )
62
66
63
67
m.AddInit (func () error {
64
68
// TODO 此处可以添加初始化模块的相关代码
@@ -68,37 +72,40 @@ func Init(s *web.Web) {
68
72
m.AddService (func (ctx context.Context ) error {
69
73
// TODO 此处添加服务代码
70
74
}, " 服务描述" )
75
+
76
+ return s.AddModule (m)
71
77
}
72
78
```
73
79
74
80
#### 插件
75
81
76
82
web 支持以插件的形式分发模块内容,只需要以 ` -buildmode=plugin ` 的形式编译每个模块,
77
- 之后将编译后的模块文件放至 ` Config.Plugins ` 配置项所指定的目录下即可 。具体的可参考下 internal/plugintest 下的插件示例。
83
+ 之后将编译后的模块通过 Server.LoadPlugin() 加载即可 。具体的可参考下 internal/plugintest 下的插件示例。
78
84
79
85
Go 并不是在所有的平台下都支持插件模式,支持列表可查看:< https://golang.org/pkg/plugin/ >
80
86
81
87
字符集和文档类型
82
88
---
83
89
84
- 文档类型由 ` Config.Marshalers ` 和 ` Config.Unmarshalers ` 两个选项指定,分别对应编码和解码 。
90
+ 文档类型由 ` Server.Mimetypes ` 指定 。
85
91
字符类型无需用户指定,< https://www.iana.org/assignments/character-sets/character-sets.xhtml >
86
92
中列出的字符集都能自动转换。
87
93
88
94
``` go
89
- conf := &web.Config {
90
- Marshalers : map [string ]content.MarhsalFunc {
91
- " application/json" : json.Marshal ,
92
- " application/xml" : xml.Marshal ,
93
- },
94
- Unmarshalers : map [string ]content.UnmarhsalFunc {
95
- " application/json" : json.Unmarshal ,
96
- " application/xml" : xml.Unmarshal ,
97
- },
98
- // 其它设置项
99
- }
95
+ import " github.com/issue9/web"
96
+
97
+ srv := web.NewServer (&web.Options {})
98
+
99
+ srv.Mimetypes ().AddMarshalers (map [string ]content.MarshalFunc {
100
+ " application/json" : json.Marshal ,
101
+ " application/xml" : xml.Marshal ,
102
+ })
103
+
104
+ srv.Mimetypes ().AddUnmarshalers (map [string ]content.UnmarshalFunc {
105
+ " application/json" : json.Unmarshal ,
106
+ " application/xml" : xml.Unmarshal ,
107
+ })
100
108
101
- srv := web.New (conf)
102
109
srv.Serve ()
103
110
```
104
111
0 commit comments