Skip to content

Commit a3a3112

Browse files
committed
feat: 配置模块(配置文件,配置解析,全局变量初始化)
1 parent 3e79e5a commit a3a3112

File tree

8 files changed

+299
-14
lines changed

8 files changed

+299
-14
lines changed

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,18 @@ golang写的博客系统(Go 语言编程之旅第二章的练习)
88

99
### 功能与目录
1010

11-
|功能|目录|commit id|备注|
12-
|:---|:---|:---|:---|
13-
|数据库与model|[scripts/init.sql](scripts/init.sql)[internal/model](internal/model)|[9c23f238a89208d80f5a7e4892091b6baad846fb](https://github.com/golang-minibear2333/gin-blog/commit/9c23f238a89208d80f5a7e4892091b6baad846fb)|创建数据库脚本与model|
14-
|路由|[internal/routers](internal/routers)|[9fc61621bde26e3181a0c64d87aa1f7c8f758a5c](https://github.com/golang-minibear2333/gin-blog/commit/9fc61621bde26e3181a0c64d87aa1f7c8f758a5c)|新增router和controller|
11+
[CHANGELOG.md](CHANGELOG.md)
12+
13+
###
14+
15+
gin框架: Go 编写的一个 HTTP Web 框架,除了快以外,还具备小巧、精美且易用的特性,目前广受 Go 语言开发者的喜爱,是最流行的 HTTP Web 框架
16+
17+
```shell
18+
go get -u github.com/gin-gonic/gin@v1.6.3
19+
```
20+
21+
配置管理
22+
23+
```shell
24+
go get -u github.com/spf13/viper@v1.4.0
25+
```

configs/config.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Server:服务配置,设置 gin 的运行模式、默认的 HTTP 监听端口、允许读取和写入的最大持续时间。
2+
Server:
3+
RunMode: debug
4+
HttpPort: 8000
5+
ReadTimeout: 60
6+
WriteTimeout: 60
7+
# App:应用配置,设置默认每页数量、所允许的最大每页数量以及默认的应用日志存储路径。
8+
App:
9+
DefaultPageSize: 10
10+
MaxPageSize: 100
11+
LogSavePath: storage/logs
12+
LogFileName: app
13+
LogFileExt: .log
14+
# Database:数据库配置,主要是连接实例所必需的基础参数。
15+
Database:
16+
DBType: mysql
17+
Username: blog_service # 填写你的数据库账号
18+
Password: blog_service # 填写你的数据库密码
19+
Host: 127.0.0.1:3306
20+
DBName: blog_service
21+
TablePrefix: blog_
22+
Charset: utf8
23+
ParseTime: True
24+
MaxIdleConns: 10
25+
MaxOpenConns: 30

global/setting.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package global
2+
3+
import "github.com/golang-minibear2333/gin-blog/pkg/setting"
4+
5+
var (
6+
ServerSetting *setting.ServerSettingS
7+
AppSetting *setting.AppSettingS
8+
DatabaseSetting *setting.DatabaseSettingS
9+
)

go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@ module github.com/golang-minibear2333/gin-blog
33
go 1.14
44

55
require (
6+
github.com/fsnotify/fsnotify v1.4.9 // indirect
67
github.com/gin-gonic/gin v1.6.3
78
github.com/go-playground/validator/v10 v10.6.1 // indirect
89
github.com/golang/protobuf v1.5.2 // indirect
910
github.com/json-iterator/go v1.1.11 // indirect
1011
github.com/leodido/go-urn v1.2.1 // indirect
12+
github.com/magiconair/properties v1.8.5 // indirect
1113
github.com/mattn/go-isatty v0.0.13 // indirect
14+
github.com/mitchellh/mapstructure v1.4.1 // indirect
1215
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
1316
github.com/modern-go/reflect2 v1.0.1 // indirect
17+
github.com/pelletier/go-toml v1.9.2 // indirect
18+
github.com/spf13/afero v1.6.0 // indirect
19+
github.com/spf13/cast v1.3.1 // indirect
20+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
21+
github.com/spf13/pflag v1.0.5 // indirect
22+
github.com/spf13/viper v1.4.0
1423
github.com/ugorji/go v1.2.6 // indirect
1524
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
1625
golang.org/x/sys v0.0.0-20210611083646-a4fc73990273 // indirect

go.sum

Lines changed: 142 additions & 0 deletions
Large diffs are not rendered by default.

main.go

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,53 @@ package main
22

33
import (
44
"github.com/gin-gonic/gin"
5+
"github.com/golang-minibear2333/gin-blog/global"
56
"github.com/golang-minibear2333/gin-blog/internal/routers"
7+
"github.com/golang-minibear2333/gin-blog/pkg/setting"
8+
"log"
69
"net/http"
710
"time"
811
)
912

13+
func init() {
14+
// 配置初始化,读取到全局model里面
15+
err := setupSetting()
16+
if err != nil {
17+
log.Fatalf("init.setupSetting err: %v", err)
18+
}
19+
}
1020
func main() {
21+
gin.SetMode(global.ServerSetting.RunMode)
1122
router := routers.NewRouter()
1223
s := &http.Server{
13-
Addr: ":8080",
24+
Addr: ":" + global.ServerSetting.HttpPort,
1425
Handler: router,
15-
ReadTimeout: 10 * time.Second,
16-
WriteTimeout: 10 * time.Second,
26+
ReadTimeout: global.ServerSetting.ReadTimeout,
27+
WriteTimeout: global.ServerSetting.WriteTimeout,
1728
MaxHeaderBytes: 1 << 20,
1829
}
1930
s.ListenAndServe()
2031
}
2132

22-
// 仅仅是hello测试
23-
func helloWorld() {
24-
r := gin.Default()
25-
r.GET("/ping", func(c *gin.Context) {
26-
c.JSON(200, gin.H{"message": "pong"})
27-
})
28-
r.Run()
33+
func setupSetting() error {
34+
setting, err := setting.NewSetting()
35+
if err != nil {
36+
return err
37+
}
38+
err = setting.ReadSection("Server", &global.ServerSetting)
39+
if err != nil {
40+
return err
41+
}
42+
err = setting.ReadSection("App", &global.AppSetting)
43+
if err != nil {
44+
return err
45+
}
46+
err = setting.ReadSection("Database", &global.DatabaseSetting)
47+
if err != nil {
48+
return err
49+
}
50+
51+
global.ServerSetting.ReadTimeout *= time.Second
52+
global.ServerSetting.WriteTimeout *= time.Second
53+
return nil
2954
}

pkg/setting/section.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Package setting 配置文件model
2+
package setting
3+
4+
import "time"
5+
6+
type ServerSettingS struct {
7+
RunMode string
8+
HttpPort string
9+
ReadTimeout time.Duration
10+
WriteTimeout time.Duration
11+
}
12+
13+
type AppSettingS struct {
14+
DefaultPageSize int
15+
MaxPageSize int
16+
LogSavePath string
17+
LogFileName string
18+
LogFileExt string
19+
}
20+
21+
type DatabaseSettingS struct {
22+
DBType string
23+
UserName string
24+
Password string
25+
Host string
26+
DBName string
27+
TablePrefix string
28+
Charset string
29+
ParseTime bool
30+
MaxIdleConns int
31+
MaxOpenConns int
32+
}
33+
34+
func (s *Setting) ReadSection(k string, v interface{}) error {
35+
err := s.vp.UnmarshalKey(k, v)
36+
if err != nil {
37+
return err
38+
}
39+
40+
return nil
41+
}

pkg/setting/setting.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package setting
2+
3+
import "github.com/spf13/viper"
4+
5+
type Setting struct {
6+
vp *viper.Viper
7+
}
8+
9+
// NewSetting 设置配置文件的信息
10+
func NewSetting() (*Setting, error) {
11+
vp := viper.New()
12+
vp.SetConfigName("config")
13+
vp.AddConfigPath("configs/")
14+
// 可以通过不断调用下面的函数,设置多个配置路径用于查找配置文件
15+
//vp.AddConfigPath("etc/")
16+
vp.SetConfigType("yaml")
17+
err := vp.ReadInConfig()
18+
if err != nil {
19+
return nil, err
20+
}
21+
22+
return &Setting{vp}, nil
23+
}

0 commit comments

Comments
 (0)