-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.go
124 lines (104 loc) · 3.47 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"flag"
"fmt"
"github.com/VineethReddy02/cortex-mysql-store/grpc"
"github.com/VineethReddy02/cortex-mysql-store/mysql-store"
"go.uber.org/zap"
g "google.golang.org/grpc"
"io/ioutil"
"log"
"net"
"os"
"strconv"
"strings"
"github.com/cortexproject/cortex/pkg/chunk"
"github.com/cortexproject/cortex/pkg/util/flagext"
"github.com/lib/pq"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
type server struct {
Cfg mysql_store.Config `yaml:"cfg,omitempty"`
SchemaCfg chunk.SchemaConfig `yaml:"schema_cfg,omitempty"`
Session *pq.Dialer `yaml:"-"`
Logger *zap.Logger
}
func (c *server) RegisterFlags(f *flag.FlagSet) {
c.Cfg.RegisterFlags(f)
}
const (
configFileOption = "config.file"
configExpandENV = "config.expand-env"
)
func main() {
var cfg server
s := g.NewServer()
flagext.RegisterFlags(&cfg)
configFile, expandENV := parseConfigFileParameter(os.Args[1:])
if configFile != "" {
if err := LoadConfig(configFile, expandENV, &cfg); err != nil {
fmt.Fprintf(os.Stderr, "error loading config from %s: %v\n", configFile, err)
os.Exit(1)
}
}
s1, err := mysql_store.NewStorageClient(cfg.Cfg, cfg.SchemaCfg)
if err != nil {
log.Fatalf("Failed to created new storage client")
}
gRPCServerAddress := "localhost:"+strconv.Itoa(cfg.Cfg.GrpcServerPort)
lis, err := net.Listen("tcp", gRPCServerAddress)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
grpc.RegisterGrpcStoreServer(s, s1)
if err := s.Serve(lis); err != nil {
log.Fatalf("Failed to serve: %v", err)
}
}
// Parse -config.file and -config.expand-env option via separate flag set, to avoid polluting default one and calling flag.Parse on it twice.
func parseConfigFileParameter(args []string) (configFile string, expandEnv bool) {
// ignore errors and any output here. Any flag errors will be reported by main flag.Parse() call.
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.SetOutput(ioutil.Discard)
// usage not used in these functions.
fs.StringVar(&configFile, configFileOption, "", "")
fs.BoolVar(&expandEnv, configExpandENV, false, "")
// Try to find -config.file and -config.expand-env option in the flags. As Parsing stops on the first error, eg. unknown flag, we simply
// try remaining parameters until we find config flag, or there are no params left.
// (ContinueOnError just means that flag.Parse doesn't call panic or os.Exit, but it returns error, which we ignore)
for len(args) > 0 {
_ = fs.Parse(args)
args = args[1:]
}
return
}
// LoadConfig read YAML-formatted config from filename into cfg.
func LoadConfig(filename string, expandENV bool, cfg *server) error {
buf, err := ioutil.ReadFile(filename)
if err != nil {
return errors.Wrap(err, "Error reading config file")
}
if expandENV {
buf = expandEnv(buf)
}
err = yaml.UnmarshalStrict(buf, cfg)
if err != nil {
return errors.Wrap(err, "Error parsing config file")
}
return nil
}
// expandEnv replaces ${var} or $var in config according to the values of the current environment variables.
// The replacement is case-sensitive. References to undefined variables are replaced by the empty string.
// A default value can be given by using the form ${var:default value}.
func expandEnv(config []byte) []byte {
return []byte(os.Expand(string(config), func(key string) string {
keyAndDefault := strings.SplitN(key, ":", 2)
key = keyAndDefault[0]
v := os.Getenv(key)
if v == "" && len(keyAndDefault) == 2 {
v = keyAndDefault[1] // Set value to the default.
}
return v
}))
}