github.com/XiaoMi/Gaea@v1.2.5/cmd/gaea/main.go (about) 1 // Copyright 2019 The Gaea Authors. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "flag" 19 "fmt" 20 "os" 21 "os/signal" 22 "sync" 23 "syscall" 24 25 "github.com/XiaoMi/Gaea/core" 26 "github.com/XiaoMi/Gaea/log" 27 "github.com/XiaoMi/Gaea/log/xlog" 28 "github.com/XiaoMi/Gaea/models" 29 "github.com/XiaoMi/Gaea/proxy/server" 30 ) 31 32 var configFile = flag.String("config", "etc/gaea.ini", "gaea config file") 33 var info = flag.Bool("info", false, "show info of gaea") 34 35 func main() { 36 flag.Parse() 37 if *info { 38 fmt.Printf("Build Version Information:%s\n", core.Info.LongForm()) 39 return 40 } 41 42 fmt.Printf("Build Version Information:%s\n", core.Info.LongForm()) 43 44 // init config of gaea proxy 45 cfg, err := models.ParseProxyConfigFromFile(*configFile) 46 if err != nil { 47 fmt.Printf("parse config file error:%v\n", err.Error()) 48 return 49 } 50 51 if err = initXLog(cfg.LogOutput, cfg.LogPath, cfg.LogFileName, cfg.LogLevel, cfg.Service); err != nil { 52 fmt.Printf("init xlog error: %v\n", err.Error()) 53 return 54 } 55 defer log.Close() 56 57 // init manager 58 mgr, err := server.LoadAndCreateManager(cfg) 59 if err != nil { 60 log.Fatal("init manager failed, error: %v", err) 61 return 62 } 63 64 svr, err := server.NewServer(cfg, mgr) 65 if err != nil { 66 log.Fatal("NewServer error, quit. error: %s", err.Error()) 67 return 68 } 69 70 sc := make(chan os.Signal, 1) 71 signal.Notify(sc, 72 syscall.SIGINT, 73 syscall.SIGTERM, 74 syscall.SIGQUIT, 75 syscall.SIGPIPE, 76 syscall.SIGUSR1, 77 ) 78 79 var wg sync.WaitGroup 80 wg.Add(1) 81 go func() { 82 defer wg.Done() 83 for { 84 sig := <-sc 85 if sig == syscall.SIGINT || sig == syscall.SIGTERM || sig == syscall.SIGQUIT { 86 log.Notice("Got signal %d, quit", sig) 87 svr.Close() 88 break 89 } else if sig == syscall.SIGPIPE { 90 log.Notice("Ignore broken pipe signal") 91 } else if sig == syscall.SIGUSR1 { 92 log.Notice("Got update config signal") 93 } 94 } 95 }() 96 svr.Run() 97 wg.Wait() 98 } 99 100 func initXLog(output, path, filename, level, service string) error { 101 cfg := make(map[string]string) 102 cfg["path"] = path 103 cfg["filename"] = filename 104 cfg["level"] = level 105 cfg["service"] = service 106 cfg["skip"] = "5" // 设置xlog打印方法堆栈需要跳过的层数, 5目前为调用log.Debug()等方法的方法名, 比xlog默认值多一层. 107 108 logger, err := xlog.CreateLogManager(output, cfg) 109 if err != nil { 110 return err 111 } 112 113 log.SetGlobalLogger(logger) 114 return nil 115 }