/
config.go
114 lines (97 loc) · 2.77 KB
/
config.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
/*
@Date: 2021/11/10 11:27
@Author: yvanz
@File : config
*/
package apiserver
import (
"context"
"encoding/json"
"fmt"
"github.com/ilyakaznacheev/cleanenv"
"github.com/spf13/cobra"
"github.com/yvanz/gin-tmpl/pkg/gormdb"
"github.com/yvanz/gin-tmpl/pkg/kafka"
"github.com/yvanz/gin-tmpl/pkg/logger"
"github.com/yvanz/gin-tmpl/pkg/rediscache"
"github.com/yvanz/gin-tmpl/pkg/tracer"
)
const (
RunModeDebug = "debug"
RunModeTest = "test"
RunModeDev = "dev"
RunModeProd = "prod"
RunModeProduction = "production"
RunModeRelease = "release"
)
type APIConfig struct {
App AppConfig `yaml:"app"`
Log logger.Config `yaml:"log"`
MySQL gormdb.DBConfig `yaml:"mysql"`
Redis rediscache.Config `yaml:"redis"`
Kafka kafka.Config `yaml:"kafka"`
Tracer tracer.Config `yaml:"tracer"`
}
type AppConfig struct {
ServiceName string `yaml:"service_name" env-default:"gin-project" env-description:"the name of the service"`
HostIP string `yaml:"local_ip" env:"HostIP" env-default:"0.0.0.0" env-description:"listening on which IP"`
APIPort int `yaml:"api_port" env:"APIPort" env-default:"8000" env-description:"listening on which port"`
AdminPort int `yaml:"admin_port" env:"AdminPort" env-default:"8001" env-description:"listening on which port of admin service"`
RunMode string `yaml:"run_mode" env:"RunMode" env-description:"run mode of the service"`
CertFile string `yaml:"cert_file" env:"CertFile" env-description:"cert file if server need to use tls"`
KeyFile string `yaml:"key_file" env:"KeyFile" env-description:"key file if server need to use tls"`
}
func (c *APIConfig) buildLogger() *logger.DemoLog {
if c.Log.LogName == "" {
c.Log.LogName = c.App.ServiceName
}
return logger.ConfigureLogger(&logger.Options{Config: c.Log})
}
func (c *APIConfig) String() string {
configData, err := json.Marshal(c)
if err != nil {
fmt.Println(err)
}
return string(configData)
}
func (c *APIConfig) initService(ctx context.Context, opts *serverOptions) (err error) {
if c.MySQL.WriteDBHost != "" {
if opts.tableColumnWithRaw {
c.MySQL.RawColumn = true
}
db, e := c.MySQL.BuildMySQLClient(ctx)
if e != nil {
err = e
return
}
if len(opts.migrationList) > 0 {
err = db.Migration(opts.migrationList...)
if err != nil {
return
}
}
}
if c.Redis.Addr != "" {
err = c.Redis.NewRedisCli(ctx)
if err != nil {
return
}
}
if c.Kafka.Addr != "" {
_, err = c.Kafka.BuildKafka(ctx)
if err != nil {
return
}
}
return err
}
func NewConfigEnvCommand(c interface{}) *cobra.Command {
return &cobra.Command{
Use: "env",
Short: "Prints environment variables.",
Run: func(*cobra.Command, []string) {
help, _ := cleanenv.GetDescription(c, nil)
fmt.Println(help)
},
}
}