Skip to content

Commit 4976456

Browse files
committed
移动swagger config
1 parent 00603f3 commit 4976456

File tree

5 files changed

+25
-26
lines changed

5 files changed

+25
-26
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ go mod tidy
1818

1919
generate swagger UI
2020
```bash
21-
swag init -g cmd/log-writer/main.go
21+
swag init -g cmd/LogBeetle/main.go
2222
```
2323

24-
run
24+
runs
2525
```bash
2626
go run github.com/reggiepy/LogBeetle/cmd/LogBeetle server
2727
```

cmd/LogBeetle/sub/root.go

+4-18
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,6 @@ func Execute() {
110110
}
111111
}
112112

113-
// @title Swagger Example API
114-
// @version 1.0
115-
// @description This is a sample server celler server.
116-
// @termsOfService http://swagger.io/terms/
117-
118-
// @contact.name API Support
119-
// @contact.url http://www.swagger.io/support
120-
// @contact.email [email protected]
121-
122-
// @license.name Apache 2.0
123-
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
124-
125-
// @host localhost:8080
126-
// @BasePath /api/v1
127-
128-
// @externalDocs.description OpenAPI
129-
// @externalDocs.url https://swagger.io/resources/open-api/
130113
func serverStart() {
131114
// 创建一个上下文,以便能够在主程序退出时取消所有 Goroutine
132115
ctx, cancel := context.WithCancel(context.Background())
@@ -141,6 +124,9 @@ func serverStart() {
141124
// 获取配置
142125
nsqConfig := config.Instance.NSQConfig
143126
consumerConfig := config.Instance.ConsumerConfig
127+
if len(consumerConfig.NSQConsumers) == 0 {
128+
_ = fmt.Errorf("consumer config is empty")
129+
}
144130

145131
// 定义工作线程
146132
workers := []*worker.Worker{
@@ -189,7 +175,7 @@ func serverStart() {
189175
)
190176

191177
// 添加其他消费者
192-
for _, consumerConfig := range consumerConfig.Consumers {
178+
for _, consumerConfig := range consumerConfig.NSQConsumers {
193179
logconsumer.AddConsumer(
194180
logconsumer.NewLogConsumer(
195181
consumerConfig.Name,

log-beetle.tmp.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LogConfig: # 日志配置
1111
LogFormat: json # 日志等级 JSON | logfmt
1212
ConsumerConfig:
1313
LogPath: ./logs
14-
Consumers:
14+
NSQConsumers:
1515
- Name: "test"
1616
Topic: "test"
1717
FileName: "test.log"

pkg/config/config.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
var Instance *Config
1212

13-
type Consumer struct {
13+
type NSQConsumers struct {
1414
Name string `yaml:"Name"`
1515
Topic string `yaml:"Topic"`
1616
FileName string `yaml:"FileName"`
@@ -33,8 +33,8 @@ type Config struct {
3333
} `yaml:"LogConfig"`
3434

3535
ConsumerConfig struct {
36-
LogPath string `yaml:"LogPath"` //日志输出路径
37-
Consumers []Consumer `yaml:"Consumers"`
36+
LogPath string `yaml:"LogPath"` //日志输出路径
37+
NSQConsumers []NSQConsumers `yaml:"NSQConsumers"`
3838
} `yaml:"ConsumerConfig"`
3939

4040
NSQConfig struct {
@@ -59,7 +59,7 @@ func DefaultConfig() *Config {
5959
config.LogConfig.LogFormat = "json"
6060

6161
config.ConsumerConfig.LogPath = "./logs"
62-
config.ConsumerConfig.Consumers = make([]Consumer, 0)
62+
config.ConsumerConfig.NSQConsumers = make([]NSQConsumers, 0)
6363

6464
config.NSQConfig.AuthSecret = ""
6565
config.NSQConfig.NSQDAddress = "127.0.0.1:4150"
@@ -138,7 +138,7 @@ func setStructDefaults(config, defaultConfig interface{}) {
138138
field := configValue.Field(i)
139139

140140
// 如果字段是切片类型,则递归处理每个元素
141-
if configValue.Type().Field(i).Name == "Consumers" && field.Kind() == reflect.Slice {
141+
if configValue.Type().Field(i).Name == "NSQConsumers" && field.Kind() == reflect.Slice {
142142
// 删除目标配置中的空切片以及空元素
143143
var updatedSlice reflect.Value
144144
updatedSlice = reflect.MakeSlice(field.Type(), 0, 0)

web/routers.go

+13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ import (
1313
)
1414

1515
// SetupRouter 设置路由
16+
// @title Swagger Example API
17+
// @version 1.0
18+
// @description This is a sample server celler server.
19+
// @termsOfService http://swagger.io/terms/
20+
// @contact.name API Support
21+
// @contact.url http://www.swagger.io/support
22+
// @contact.email [email protected]
23+
// @license.name Apache 2.0
24+
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
25+
// @host localhost:8080
26+
// @BasePath /api/v1
27+
// @externalDocs.description OpenAPI
28+
// @externalDocs.url https://swagger.io/resources/open-api/
1629
func SetupRouter() *gin.Engine {
1730
// programatically set swagger info
1831
docs.SwaggerInfo.Title = "Log API"

0 commit comments

Comments
 (0)