config.go 2.6 KB
Newer Older
H
hongming 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Copyright 2018 The OpenPitrix Authors. All rights reserved.
// Use of this source code is governed by a Apache license
// that can be found in the LICENSE file.

package config

import (
	"flag"
	"fmt"
	"os"
	"time"

	"github.com/koding/multiconfig"

	"openpitrix.io/openpitrix/pkg/logger"
)

type Config struct {
	Log         LogConfig
	Grpc        GrpcConfig
	Mysql       MysqlConfig
	Etcd        EtcdConfig
	IAM         IAMConfig
P
pengcong06 已提交
24
	Attachment  AttachmentConfig
H
hongming 已提交
25 26 27 28 29 30 31 32 33
	DisableGops bool `default:"false"`
}

type IAMConfig struct {
	SecretKey              string        `default:"OpenPitrix-lC4LipAXPYsuqw5F"`
	ExpireTime             time.Duration `default:"2h"`
	RefreshTokenExpireTime time.Duration `default:"336h"` // default is 2 week
}

P
pengcong06 已提交
34 35 36 37 38 39 40
type AttachmentConfig struct {
	AccessKey  string `default:"openpitrixminioaccesskey"`
	SecretKey  string `default:"openpitrixminiosecretkey"`
	Endpoint   string `default:"http://openpitrix-minio:9000"`
	BucketName string `default:"openpitrix-attachment"`
}

H
hongming 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
type LogConfig struct {
	Level string `default:"info"` // debug, info, warn, error, fatal
}

type GrpcConfig struct {
	ShowErrorCause bool `default:"false"` // show grpc error cause to frontend
}

type EtcdConfig struct {
	Endpoints string `default:"openpitrix-etcd:2379"` // Example: "localhost:2379,localhost:22379,localhost:32379"
}

type MysqlConfig struct {
	Host     string `default:"openpitrix-db"`
	Port     string `default:"3306"`
	User     string `default:"root"`
	Password string `default:"password"`
	Database string `default:"openpitrix"`
	Disable  bool   `default:"false"`
}

func (m *MysqlConfig) GetUrl() string {
	return fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", m.User, m.Password, m.Host, m.Port, m.Database)
}

func PrintUsage() {
	fmt.Fprintf(os.Stdout, "Usage of %s:\n", os.Args[0])
	flag.PrintDefaults()
	fmt.Fprint(os.Stdout, "\nSupported environment variables:\n")
	e := newLoader("openpitrix")
	e.PrintEnvs(new(Config))
	fmt.Println("")
}

func GetFlagSet() *flag.FlagSet {
	flag.CommandLine.Usage = PrintUsage
	return flag.CommandLine
}

func ParseFlag() {
	GetFlagSet().Parse(os.Args[1:])
}

P
pengcong06 已提交
84
var conf Config
H
hongming 已提交
85

P
pengcong06 已提交
86
func loadConf() *Config {
H
hongming 已提交
87 88 89 90 91 92 93 94 95 96 97 98
	config := new(Config)
	m := &multiconfig.DefaultLoader{}
	m.Loader = multiconfig.MultiLoader(newLoader("openpitrix"))
	m.Validator = multiconfig.MultiValidator(
		&multiconfig.RequiredValidator{},
	)
	err := m.Load(config)
	if err != nil {
		logger.Critical(nil, "Failed to load config: %+v", err)
		panic(err)
	}
	logger.SetLevelByString(config.Log.Level)
P
pengcong06 已提交
99
	logger.Debug(nil, "GetConf: %+v", config)
H
hongming 已提交
100 101 102

	return config
}
P
pengcong06 已提交
103 104 105 106 107 108 109 110 111 112 113

func init() {
	conf = *loadConf()
}

func GetConf() *Config {
	ParseFlag()
	var c = new(Config)
	*c = conf
	return c
}