Skip to main content

air + viper + gin

默认情况下,air 主要监视 Go 源文件的变化来触发重启。如果你希望在修改配置文件(如 config.yaml)时也能触发重启,你需要在 air 的配置文件中指定要监视的文件类型。

以下是如何配置 air 以监视配置文件变化的步骤:

1. 修改 .air.toml 配置文件

在你的 .air.toml 文件中,确保 include_ext 包含配置文件的扩展名(如 yaml)。示例如下:

[build]
bin = "tmp/main" # Binary output path
cmd = "go build -o ./tmp/main" # Build command
full_bin = "APP_ENV=dev APP_USER=air ./tmp/main" # Full binary run command
include_ext = ["go", "tpl", "tmpl", "html", "yaml", "yml"] # File extensions to watch
exclude_dir = ["assets", "tmp", "logs"] # Directories to ignore
exclude_file = ["air.conf"] # Files to ignore
follow_symlink = true # Follow symlinks
kill_delay = 1000 # Delay before killing the old process (ms)
delay = 1000 # Delay before starting new process (ms)
grace_time = 500 # Grace time for old process to shutdown (ms)
grace_time_slack = 0 # Additional grace time for old process to shutdown (ms)
cmd_args = ["-some_arg=some_value"] # Arguments for the build command
run_args = ["-some_arg=some_value"] # Arguments for the run command
build_args = ["-a", "-v"] # Arguments for the build command

[log]
color = true # Use colored output
time = true # Show time in log
level = "info" # Log level (debug, info, warn, error, fatal)

[watch]
exclude = ["assets", "tmp", "logs"] # Directories to ignore
include = ["*.go", "*.tpl", "*.tmpl", "*.html", "*.yaml", "*.yml"] # File patterns to watch
follow_symlink = true # Follow symlinks
delay = 1000 # Delay before rebuilding (ms)

2. 运行 air

在项目根目录下运行 air

air

3. 触发重启

现在,当你修改 config.yaml 文件时,air 也会监视到这些变化并自动重启你的应用。

示例

假设你有以下配置文件 config.yaml

server:
port: 8080

当你修改 config.yaml 文件中的端口号,例如:

server:
port: 9090

air 会自动检测到这个变化并重启你的应用,使得新配置生效。

完整示例

以下是一个完整的项目结构和代码示例:

your_project_name/
├── .air.toml
├── config.yaml
├── go.mod
└── main.go

.air.toml

[build]
bin = "tmp/main"
cmd = "go build -o ./tmp/main"
full_bin = "APP_ENV=dev APP_USER=air ./tmp/main"
include_ext = ["go", "tpl", "tmpl", "html", "yaml", "yml"]
exclude_dir = ["assets", "tmp", "logs"]
exclude_file = ["air.conf"]
follow_symlink = true
kill_delay = 1000
delay = 1000
grace_time = 500
grace_time_slack = 0
cmd_args = ["-some_arg=some_value"]
run_args = ["-some_arg=some_value"]
build_args = ["-a", "-v"]

[log]
color = true
time = true
level = "info"

[watch]
exclude = ["assets", "tmp", "logs"]
include = ["*.go", "*.tpl", "*.tmpl", "*.html", "*.yaml", "*.yml"]
follow_symlink = true
delay = 1000

config.yaml

server:
port: 8080

main.go

package main

import (
"fmt"
"log"

"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)

func initConfig() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")

if err := viper.ReadInConfig(); err != nil {
log.Fatalf("Error reading config file, %s", err)
}
}

func main() {
initConfig()

r := gin.Default()
r.GET("/", func(c *gin.Context) {
c.String(200, "Hello, World!")
})

port := viper.GetString("server.port")
if port == "" {
port = "8080"
}
r.Run(fmt.Sprintf(":%s", port))
}

通过以上配置和代码,当你修改 config.yaml 文件时,air 会自动重启你的应用,使得配置更改生效。