-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
140 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,12 +23,15 @@ | |
|
||
- `config` 配置保存地址,可以指定为etcd或者本地文件,如:`etcd://user:[email protected]:2379/pike`,本地文件:`/opt/pike/config.yml` | ||
- `admin` 配置管理后台的访问地址,如:`--admin=:9013` | ||
- `log` 日志文件目录,支持单文件与lumberjack形式,如`/var/pike.log`或`lumberjack:///tmp/pike.log?maxSize=100&maxAge=1&compress=true`,lumberjack会根据文件内容大小与时间将文件分割 | ||
|
||
### 使用文件保存配置 | ||
|
||
```bash | ||
# 命令行 | ||
# linux etcd,管理后台使用9013端口访问 | ||
./pike --config=etcd://127.0.0.1:2379/pike --admin=:9013 | ||
# linux file,配置文件保存在/opt/pike.yml,管理后台使用9013端口访问 | ||
./pike --config=/opt/pike.yml --admin=:9013 | ||
|
||
# docker | ||
docker run -it --rm \ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,16 @@ var alarmURL string | |
var alarmTemplate string | ||
|
||
func init() { | ||
|
||
err := runCMD() | ||
if err != nil { | ||
panic(err) | ||
} | ||
// 如果是help cmd,则 | ||
if isHelpCmd() { | ||
os.Exit(0) | ||
return | ||
} | ||
_, _ = maxprocs.Set(maxprocs.Logger(func(format string, args ...interface{}) { | ||
value := fmt.Sprintf(format, args...) | ||
log.Default().Info(value) | ||
|
@@ -113,45 +123,19 @@ func startAdminServer(addr string) error { | |
}) | ||
} | ||
|
||
func run() { | ||
logger := log.Default() | ||
|
||
go config.Watch(func() { | ||
err := update() | ||
if err != nil { | ||
logger.Error("update config fail", | ||
zap.Error(err), | ||
) | ||
go doAlarm("config", err.Error()) | ||
} else { | ||
logger.Info("update config success") | ||
} | ||
}) | ||
|
||
err := update() | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func isHelpCmd() bool { | ||
for _, arg := range os.Args { | ||
if arg == "-h" || arg == "--help" { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
func main() { | ||
logger := log.Default() | ||
defer config.Close() | ||
// runCMD 解析各命令参数 | ||
func runCMD() error { | ||
configURL := "" | ||
adminAddr := "" | ||
logOutputPath := "" | ||
|
||
var rootCmd = &cobra.Command{ | ||
Use: "pike", | ||
Short: "Pike is a http cache server", | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
if logOutputPath != "" { | ||
log.SetOutputPath(logOutputPath) | ||
} | ||
// 初始化配置 | ||
err := config.InitDefaultClient(configURL) | ||
if err != nil { | ||
|
@@ -164,7 +148,7 @@ func main() { | |
go func() { | ||
err := startAdminServer(adminAddr) | ||
if err != nil { | ||
logger.Error("start admin server fail", | ||
log.Default().Error("start admin server fail", | ||
zap.String("addr", adminAddr), | ||
zap.Error(err), | ||
) | ||
|
@@ -175,19 +159,51 @@ func main() { | |
run() | ||
}, | ||
} | ||
rootCmd.Flags().StringVar(&configURL, "config", "pike.yml", "The config of pike, support etcd or file, etcd://user:[email protected]:2379,192.168.1.3:2379/pike or /opt/pike") | ||
// 配置文件地址 | ||
rootCmd.Flags().StringVar(&configURL, "config", "pike.yml", "The config of pike, support etcd or file, etcd://user:[email protected]:2379,192.168.1.3:2379/pike or /opt/pike.yml") | ||
// 管理后台地址 | ||
rootCmd.Flags().StringVar(&adminAddr, "admin", "", "The address of admin web page, e.g.: :9013") | ||
// 告警发送地址 | ||
rootCmd.Flags().StringVar(&alarmURL, "alarm", "", "The alarm request url, alarm will post to the url, e.g.: http://192.168.1.2:3000/alarms") | ||
// 日志文件 | ||
rootCmd.Flags().StringVar(&logOutputPath, "log", "", "The log path, e.g.: /var/pike.log or lumberjack:///tmp/pike.log?maxSize=100&maxAge=1&compress=true") | ||
|
||
return rootCmd.Execute() | ||
} | ||
|
||
func run() { | ||
logger := log.Default() | ||
|
||
go config.Watch(func() { | ||
err := update() | ||
if err != nil { | ||
logger.Error("update config fail", | ||
zap.Error(err), | ||
) | ||
go doAlarm("config", err.Error()) | ||
} else { | ||
logger.Info("update config success") | ||
} | ||
}) | ||
|
||
err := rootCmd.Execute() | ||
err := update() | ||
if err != nil { | ||
panic(err) | ||
} | ||
if isHelpCmd() { | ||
return | ||
} | ||
|
||
func isHelpCmd() bool { | ||
for _, arg := range os.Args { | ||
if arg == "-h" || arg == "--help" { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
func main() { | ||
defer config.Close() | ||
|
||
logger.Info("pike is running") | ||
log.Default().Info("pike is running") | ||
c := make(chan os.Signal, 1) | ||
signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) | ||
for range c { | ||
|