Skip to content

Commit 0c7ed80

Browse files
committed
init
1 parent 5aa1b62 commit 0c7ed80

17 files changed

+789
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# SCA基础配置模块(Base Module Config)
2+
3+
4+
5+
6+
7+
8+
9+
10+

config.example.yaml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# 日志相关参数配置
2+
logger:
3+
# 日志文件的输出目录
4+
directory: "./"
5+
# 日志开启哪些端
6+
enable:
7+
# 是否开启标准输出
8+
stdout: true
9+
# 是否开启文件输出
10+
file: true
11+
12+
# 爬虫相关配置
13+
crawler:
14+
# 爬虫使用到的代理
15+
proxy:
16+
# 动态代理
17+
dynamic-proxy: "xxx"
18+
# 爬虫的队列
19+
queue:
20+
# 队列名称
21+
name: "xxxx"
22+
# 消费者参数
23+
consumer:
24+
# 消费者数量
25+
worker-num: 1
26+
27+
# OSS相关参数配置
28+
oss:
29+
# 公网
30+
endpoint: "oss-cn-beijing.aliyuncs.com"
31+
# 认证相关
32+
access-key-id: "xxxx"
33+
access-key-secret: "xxxx"
34+
35+
# Redis配置
36+
redis:
37+
address: "xxx:6379"
38+
passwd: "xxxx:xxxx"
39+
40+
# 数据库配置
41+
database:
42+
mysql:
43+
dsn: "xxxx:xxxxxxxx@tcp(xxxx)/xxxx?parseTime=true&loc=Local&charset=utf8mb4"

config.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package sca_base_module_config
2+
3+
import "github.com/spf13/viper"
4+
5+
// Configuration 对应着一个映射文件
6+
type Configuration struct {
7+
Database Database `yaml:"database" mapstructure:"database"`
8+
Redis Redis `yaml:"redis" mapstructure:"redis"`
9+
Logger Logger `yaml:"logger" mapstructure:"logger"`
10+
OSS OSS `yaml:"oss" mapstructure:"oss"`
11+
Crawler Crawler `yaml:"crawler" mapstructure:"crawler"`
12+
13+
// 把原来的viper也获取一下放在这里,随时都可以通过viper的方法来获取
14+
*viper.Viper
15+
16+
// 未明确声明的就放在这里吧...
17+
ViperRemainMap map[string]any `mapstructure:",remain"`
18+
}
19+
20+
func (x *Configuration) GetStringOrDefault(name, defaultValue string) string {
21+
return OrDefault(x.Viper.GetString(name), defaultValue)
22+
}

crawler.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package sca_base_module_config
2+
3+
type Crawler struct {
4+
Proxy CrawlerProxy `yaml:"proxy" mapstructure:"proxy"`
5+
Queue CrawlerQueue `yaml:"queue" mapstructure:"queue"`
6+
Consumer CrawlerConsumer `yaml:"consumer" mapstructure:"consumer"`
7+
}
8+
9+
type CrawlerQueue struct {
10+
Name string `yaml:"name" mapstructure:"name"`
11+
}
12+
13+
type CrawlerConsumer struct {
14+
WorkerNum int `yaml:"worker-num" mapstructure:"worker-num"`
15+
}

database.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package sca_base_module_config
2+
3+
type Database struct {
4+
Mysql Mysql `yaml:"mysql" mapstructure:"mysql"`
5+
}

default.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package sca_base_module_config
2+
3+
import (
4+
reflect_utils "github.com/golang-infrastructure/go-reflect-utils"
5+
)
6+
7+
// OrDefault 如果给定的值为空的话则返回默认值
8+
func OrDefault[T any](value, defaultValue T) T {
9+
if reflect_utils.IsNil(value) {
10+
return defaultValue
11+
} else {
12+
return value
13+
}
14+
}

env.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package sca_base_module_config
2+
3+
import (
4+
"github.com/golang-infrastructure/go-pointer"
5+
"os"
6+
"strconv"
7+
"strings"
8+
)
9+
10+
const ScaAutoInitConfigEnvName = "SCA_AUTO_INIT_CONFIG"
11+
12+
func GetEnvString(name string) *string {
13+
env, b := os.LookupEnv(name)
14+
if !b {
15+
return nil
16+
}
17+
return pointer.ToPointer(env)
18+
}
19+
20+
func GetEnvStringOrDefault(name, defaultValue string) string {
21+
return pointer.FromPointerOrDefault(GetEnvString(name), defaultValue)
22+
}
23+
24+
func GetEnvBool(name string) *bool {
25+
env, b := os.LookupEnv(name)
26+
if !b {
27+
return nil
28+
}
29+
return pointer.ToPointer(strings.ToLower(env) == "true")
30+
}
31+
32+
func GetEnvBoolOrDefault(name string, defaultValue bool) bool {
33+
return pointer.FromPointerOrDefault(GetEnvBool(name), defaultValue)
34+
}
35+
36+
func GetEnvInt(name string) *int {
37+
env, b := os.LookupEnv(name)
38+
if !b {
39+
return nil
40+
}
41+
atoi, err := strconv.Atoi(env)
42+
if err != nil {
43+
return nil
44+
}
45+
return pointer.ToPointer(atoi)
46+
}
47+
func GetEnvIntOrDefault(name string, defaultValue int) int {
48+
return pointer.FromPointerOrDefault(GetEnvInt(name), defaultValue)
49+
}

go.mod

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module github.com/scagogogo/sca-base-module-config
2+
3+
go 1.18
4+
5+
require (
6+
github.com/golang-infrastructure/go-pointer v0.0.4
7+
github.com/golang-infrastructure/go-project-root-directory v0.0.1
8+
github.com/golang-infrastructure/go-reflect-utils v0.0.0-20221130143747-965ef2eb09c3
9+
github.com/spf13/viper v1.16.0
10+
github.com/stretchr/testify v1.8.3
11+
)
12+
13+
require (
14+
github.com/davecgh/go-spew v1.1.1 // indirect
15+
github.com/fsnotify/fsnotify v1.6.0 // indirect
16+
github.com/golang-infrastructure/go-how-run v0.0.0-20230107060855-56163adc7748 // indirect
17+
github.com/hashicorp/hcl v1.0.0 // indirect
18+
github.com/magiconair/properties v1.8.7 // indirect
19+
github.com/mitchellh/mapstructure v1.5.0 // indirect
20+
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
21+
github.com/pmezard/go-difflib v1.0.0 // indirect
22+
github.com/spf13/afero v1.9.5 // indirect
23+
github.com/spf13/cast v1.5.1 // indirect
24+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
25+
github.com/spf13/pflag v1.0.5 // indirect
26+
github.com/subosito/gotenv v1.4.2 // indirect
27+
golang.org/x/sys v0.10.0 // indirect
28+
golang.org/x/text v0.11.0 // indirect
29+
gopkg.in/ini.v1 v1.67.0 // indirect
30+
gopkg.in/yaml.v3 v3.0.1 // indirect
31+
)

0 commit comments

Comments
 (0)