github.com/hy3/cuto@v0.9.8-0.20160830082821-aa6652f877b7/servant/main.go (about)

     1  // Copyright 2015 unirita Inc.
     2  // Created 2015/04/10 shanxia
     3  
     4  package main
     5  
     6  import (
     7  	"flag"
     8  	"fmt"
     9  	"os"
    10  	"strconv"
    11  
    12  	"github.com/unirita/cuto/console"
    13  	"github.com/unirita/cuto/log"
    14  	"github.com/unirita/cuto/message"
    15  	"github.com/unirita/cuto/servant/config"
    16  	"github.com/unirita/cuto/util"
    17  )
    18  
    19  type arguments struct {
    20  	v          bool
    21  	configPath string // 設定ファイルのパス
    22  }
    23  
    24  const (
    25  	rc_OK    = 0
    26  	rc_error = 1
    27  )
    28  
    29  // エントリポイント
    30  func main() {
    31  	args := fetchArgs()
    32  	// 作業フォルダは実行モジュールと同じ場所にする
    33  	os.Chdir(util.GetCurrentPath())
    34  
    35  	os.Exit(realMain(args))
    36  }
    37  
    38  func realMain(args *arguments) int {
    39  	if args.v {
    40  		showVersion()
    41  		return rc_OK
    42  	}
    43  	message.ServantVersion = Version
    44  
    45  	// システム変数のセット
    46  	message.AddSysValue("ROOT", "", util.GetRootPath())
    47  
    48  	config.ReadConfig(args.configPath)
    49  	if err := config.Servant.DetectError(); err != nil {
    50  		console.Display("CTS005E", err)
    51  		return rc_error
    52  	}
    53  
    54  	// ログ出力開始
    55  	if err := log.Init(config.Servant.Dir.LogDir,
    56  		"servant",
    57  		strconv.Itoa(config.Servant.Sys.BindPort),
    58  		config.Servant.Log.OutputLevel,
    59  		config.Servant.Log.MaxSizeKB,
    60  		config.Servant.Log.MaxGeneration,
    61  		config.Servant.Log.TimeoutSec); err != nil {
    62  		console.Display("CTS023E", err)
    63  		return rc_error
    64  	}
    65  	defer log.Term()
    66  	console.Display("CTS001I", os.Getpid(), Version)
    67  
    68  	// メイン処理開始
    69  	exitCode, err := Run()
    70  
    71  	if err != nil {
    72  		log.Error(err)
    73  		exitCode = rc_error
    74  	}
    75  	console.Display("CTS002I", exitCode)
    76  	return exitCode
    77  }
    78  
    79  func fetchArgs() *arguments {
    80  	args := new(arguments)
    81  	flag.BoolVar(&args.v, "v", false, "version option")
    82  	flag.StringVar(&args.configPath, "c", "", "config file option")
    83  	flag.Parse()
    84  	return args
    85  }
    86  
    87  func showVersion() {
    88  	fmt.Printf("cuto servant version %s\n", Version)
    89  }