Skip to content

Commit

Permalink
docs: add guide/config
Browse files Browse the repository at this point in the history
  • Loading branch information
sanjayheaven committed Jan 18, 2024
1 parent d83ec61 commit 8af6e3b
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 9 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches:
- main
- docs
paths:
- "docs/go-gin-boilerplate/**"
- ".github/workflows/deploy.yml"
Expand Down
4 changes: 1 addition & 3 deletions docs/go-gin-boilerplate/docs/guide/_category_.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"label": "指南",
"position": 3,
"link": {
"type": "generated-index"
}
"link": {}
}
164 changes: 164 additions & 0 deletions docs/go-gin-boilerplate/docs/guide/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,168 @@ sidebar_position: 0.9

# 配置

配置功能是软件开发当中常用的一个功能,在多环境中,例如开发环境、测试环境、生产环境等等,我们需要不同的配置,这时候就需要配置文件来管理。

同时,为了保持配置文件的安全性,我们通常会将配置环境文件保存在各个独立的环境中,通过加载文件,来获取对应的配置内容。

## 配置目录

遵循 [project-layout](https://github.com/golang-standards/project-layout) 规范,我们将 `配置文件模板或默认配置` 保存在 `configs` 目录下。

`configs` 目录下,我们可以看到以下文件:

```sh
configs
├── config.example.yaml
├── config.go
├── config.yaml
├── jwt.go
├── mongo.go
├── mysql.go
├── redis.go
└── server.go
```

- `config.example.yaml` 为配置文件示例,供参考
- `config.yaml` 为运行时项目加载的配置文件
- `config.go` 文件,这个文件是用来解析配置文件之后,保存配置内容的,也是 `config` 包的入口

而剩下的,则是各个工具或者组件的配置文件,例如 `jwt.go``JWT` 的配置文件,`mongo.go``MongoDB` 的配置文件,`mysql.go``MySQL` 的配置文件,`redis.go``Redis` 的配置文件,`server.go``项目服务`的配置文件。

## 解析配置文件

我们在这里,通过 [Viper](https://github.com/spf13/viper) 来解析配置文件。

Viper 是 Go 应用程序的完整配置解决方案,可以处理所有类型的配置需求和格式,功能十分强大,它支持:

- 设置默认值
- 从 JSON、TOML、YAML、HCL 和 Java 属性文件中读取配置数据
- 实时监控和重新加载配置文件
- 从环境变量中读取
- 从远程配置系统(etcd 或 Consul)中读取
- 从命令行参数中读取
- 从缓冲区中读取
- 从密钥/值存储中读取
- 调用远程配置系统(etcd 或 Consul)以存储配置数据

## Viper 的应用

`configs/config.go` 文件中,我们通过 `init()` 函数来初始化配置文件,并将配置内容赋于变量 **EnvConfig**,代码如下:

```go
package configs

import (
"fmt"
"os"

"github.com/spf13/viper"
)

type Config struct {
Mysql Mysql
Redis Redis
Server Server
Jwt Jwt
Mongo Mongo
}

var EnvConfig *Config

func LoadConfig() *Config {

path, err := os.Getwd() // get curent path
if err != nil {
panic(err)
}

viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(path + "/configs") // path to look for the config file in

if err := viper.ReadInConfig(); err != nil { // Handle errors reading the config file
panic(fmt.Errorf("fatal error config file: %w", err))
}

config := &Config{}
if err := viper.Unmarshal(config); err != nil {
panic(err)
}

return config
}

func init() {
EnvConfig = LoadConfig()
fmt.Printf("👻 EnvConfig: %+v\n", EnvConfig)
}

```

## 配置文件示例

目前启动 Go-Gin-Boilerplate 项目,至少需要 **MySQL****Redis** 两个服务的配置。

`configs/config.example.yaml` 文件中,我们可以看到以下内容:

```yaml
# HERE IS JUST THE CONFIGURATION EXAMPLE FOR THE APP
# DO NOT USING THIS FILE AS YOUR CONFIGURATION DIRECTLY
# PLEASE COPY THIS FILE TO configs/config.yaml AND MODIFY IT

# server
server:
port: ":YOUR_SERVER_PORT"

# jwt
jwt:
secret: YOUR_JWT_SECRET

# mysql
mysql:
host: YOUR_MYSQL_HOST
port: YOUR_MYSQL_PORT
user: YOUR_MYSQL_USER
password: YOUR_MYSQL_PASSWORD
database: YOUR_DATABASE_NAME

# redis
redis:
host: YOUR_REDIS_HOST
port: YOUR_REDIS_PORT
password: YOUR_REDIS_PASSWORD
db: YOUR_REDIS_DB
```
在克隆项目之后,我们也可以非常方便的根据 `config.example.yaml` 文件,创建 `config.yaml` 文件,并修改其中的配置内容。

```sh
cp configs/config.example.yaml configs/config.yaml
```

修改 `config.yaml` 文件中的配置内容,将变量值替换为你自己的配置内容。

```sh
vi configs/config.yaml
```

## 使用配置包

配置包的入口是 `configs/config.go` 文件,因为我们已经在 `init()` 函数中,初始化了配置文件,所以我们可以在项目其他地方,通过 `configs.EnvConfig` 来获取配置内容。

例如在 项目 启动程序中,我们可以直接调用 `configs.EnvConfig` 来获取配置内容,代码如下:

```go
package main
import (
"fmt"
"go-gin-boilerplate/configs"
)
func main() {
fmt.Printf("👻 EnvConfig: %+v\n", configs.EnvConfig)
}
```
7 changes: 4 additions & 3 deletions docs/go-gin-boilerplate/docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const config: Config = {
url: "https://sanjayheaven.github.io",
// Set the /<baseUrl>/ pathname under which your site is served
// For GitHub pages deployment, it is often '/<projectName>/'
baseUrl: "/go-gin-boilerplate/",
baseUrl: "/",

// GitHub pages deployment config.
// If you aren't using GitHub pages, you don't need these.
Expand All @@ -36,10 +36,11 @@ const config: Config = {
{
docs: {
sidebarPath: "./sidebars.ts",
// showLastUpdateTime: true,
// showLastUpdateAuthor: true,
// Please change this to your repo.
// Remove this to remove the "edit this page" links.
editUrl:
"https://github.com/facebook/docusaurus/tree/main/packages/create-docusaurus/templates/shared/",
// editUrl: "https://github.com/sanjayheaven/go-gin-boilerplate/issure",
},
blog: {
showReadingTime: true,
Expand Down
2 changes: 1 addition & 1 deletion docs/go-gin-boilerplate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"docusaurus": "docusaurus",
"start": "docusaurus start",
"build": "docusaurus build",
"build": "docusaurus build --baseUrl /go-gin-boilerplate/",
"swizzle": "docusaurus swizzle",
"deploy": "docusaurus deploy",
"clear": "docusaurus clear",
Expand Down
4 changes: 2 additions & 2 deletions docs/go-gin-boilerplate/sidebars.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {SidebarsConfig} from '@docusaurus/plugin-content-docs';
import type { SidebarsConfig } from "@docusaurus/plugin-content-docs";

/**
* Creating a sidebar enables you to:
Expand All @@ -12,7 +12,7 @@ import type {SidebarsConfig} from '@docusaurus/plugin-content-docs';
*/
const sidebars: SidebarsConfig = {
// By default, Docusaurus generates a sidebar from the docs folder structure
tutorialSidebar: [{type: 'autogenerated', dirName: '.'}],
tutorialSidebar: [{ type: "autogenerated", dirName: "." }],

// But you can create a sidebar manually
/*
Expand Down

0 comments on commit 8af6e3b

Please sign in to comment.