github.com/XiaoMi/Gaea@v1.2.5/cmd/gaea-cc/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/cc"
    26  	"github.com/XiaoMi/Gaea/core"
    27  
    28  	"github.com/XiaoMi/Gaea/log"
    29  	"github.com/XiaoMi/Gaea/log/xlog"
    30  	"github.com/XiaoMi/Gaea/models"
    31  )
    32  
    33  var ccConfigFile = flag.String("c", "./etc/gaea_cc.ini", "gaea cc配置")
    34  var info = flag.Bool("info", false, "show info of gaea-cc")
    35  
    36  func initXLog(ccConfig *models.CCConfig) error {
    37  	cfg := make(map[string]string, 4)
    38  	cfg["path"] = ccConfig.LogPath
    39  	cfg["filename"] = ccConfig.LogFileName
    40  	cfg["level"] = ccConfig.LogLevel
    41  	cfg["service"] = "gaea-cc"
    42  	cfg["skip"] = "5"
    43  
    44  	logger, err := xlog.CreateLogManager(ccConfig.LogOutput, cfg)
    45  	if err != nil {
    46  		return err
    47  	}
    48  	log.SetGlobalLogger(logger)
    49  	return nil
    50  }
    51  
    52  func main() {
    53  	flag.Parse()
    54  	if *info {
    55  		fmt.Printf("Build Version Information:%s\n", core.Info.LongForm())
    56  		return
    57  	}
    58  
    59  	fmt.Printf("Build Version Information:%s\n", core.Info.LongForm())
    60  
    61  	// 初始化配置
    62  	ccConfig, err := models.ParseCCConfig(*ccConfigFile)
    63  	if err != nil {
    64  		fmt.Printf("parse cc config failed, %v\n", err)
    65  	}
    66  
    67  	// 初始化日志
    68  	err = initXLog(ccConfig)
    69  	if err != nil {
    70  		fmt.Printf("init xlog failed, %v\n", err)
    71  		return
    72  	}
    73  
    74  	// 构造服务实例
    75  	s, err := cc.NewServer(ccConfig.Addr, ccConfig)
    76  	if err != nil {
    77  		log.Fatal("create server failed, %v", err)
    78  		return
    79  	}
    80  
    81  	c := make(chan os.Signal, 1)
    82  	signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGPIPE, syscall.SIGUSR1)
    83  	wg := new(sync.WaitGroup)
    84  	wg.Add(1)
    85  	go func() {
    86  		defer wg.Done()
    87  		for {
    88  			sig := <-c
    89  			if sig == syscall.SIGINT || sig == syscall.SIGTERM || sig == syscall.SIGQUIT {
    90  				log.Notice("got signal %d, quit", sig)
    91  				s.Close()
    92  				return
    93  			}
    94  			log.Notice("ignore signal %d", sig)
    95  		}
    96  	}()
    97  
    98  	s.Run()
    99  	wg.Wait()
   100  	log.Close()
   101  }