Skip to content

Commit af64171

Browse files
committed
docs: 修正文档错误
1 parent ebbad24 commit af64171

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

README.md

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,31 @@ import "github.com/issue9/web/config"
1919

2020
// main.go
2121
func main() {
22-
w, _ := config.Classic("./appconfig/logs.xml", "./appconfig/web.yaml")
22+
srv, _ := web.NewServer(&web.Options{})
2323

2424
// 注册模块信息
2525
m1.Init()
2626
m2.Init()
2727

28-
w.Serve()
28+
srv.Serve()
2929
}
3030

3131
// 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", "模块描述信息").
3434
Get("/admins", getAdmins).
3535
Get("/groups", getGroups)
36+
37+
return s.AddModule(m)
3638
}
3739

3840
// 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").
4143
Get("/admins", getAdmins).
4244
Get("/groups", getGroups)
45+
46+
return s.AddModule(m)
4347
}
4448
```
4549

@@ -57,8 +61,8 @@ package m1
5761

5862
import "github.com/issue9/web"
5963

60-
func Init(s *web.Web) {
61-
m := s.NewModule("test", "测试模块")
64+
func Init(s *web.Web) error {
65+
m := web.NewModule("test", "测试模块")
6266

6367
m.AddInit(func() error {
6468
// TODO 此处可以添加初始化模块的相关代码
@@ -68,37 +72,40 @@ func Init(s *web.Web) {
6872
m.AddService(func(ctx context.Context) error {
6973
// TODO 此处添加服务代码
7074
}, "服务描述")
75+
76+
return s.AddModule(m)
7177
}
7278
```
7379

7480
#### 插件
7581

7682
web 支持以插件的形式分发模块内容,只需要以 `-buildmode=plugin` 的形式编译每个模块,
77-
之后将编译后的模块文件放至 `Config.Plugins` 配置项所指定的目录下即可。具体的可参考下 internal/plugintest 下的插件示例。
83+
之后将编译后的模块通过 Server.LoadPlugin() 加载即可。具体的可参考下 internal/plugintest 下的插件示例。
7884

7985
Go 并不是在所有的平台下都支持插件模式,支持列表可查看:<https://golang.org/pkg/plugin/>
8086

8187
字符集和文档类型
8288
---
8389

84-
文档类型由 `Config.Marshalers``Config.Unmarshalers` 两个选项指定,分别对应编码和解码
90+
文档类型由 `Server.Mimetypes` 指定
8591
字符类型无需用户指定,<https://www.iana.org/assignments/character-sets/character-sets.xhtml>
8692
中列出的字符集都能自动转换。
8793

8894
```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+
})
100108

101-
srv := web.New(conf)
102109
srv.Serve()
103110
```
104111

0 commit comments

Comments
 (0)