-
Notifications
You must be signed in to change notification settings - Fork 12
/
init.go
118 lines (100 loc) · 2.87 KB
/
init.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
package main
import (
"context"
"fmt"
"io/fs"
"time"
"github.com/zerodha/logf"
"os"
"strings"
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
goyesqlx "github.com/knadh/goyesql/v2/sqlx"
"github.com/knadh/goyesql/v2"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
flag "github.com/spf13/pflag"
)
// initConfig loads config to `ko` object.
func initConfig() (*koanf.Koanf, error) {
var (
ko = koanf.New(".")
f = flag.NewFlagSet("front", flag.ContinueOnError)
)
// Configure Flags.
f.Usage = func() {
fmt.Println(f.FlagUsages())
os.Exit(0)
}
// Register `--config` flag.
cfgPath := f.String("config", "config.sample.toml", "Path to a config file to load.")
// Parse and Load Flags.
err := f.Parse(os.Args[1:])
if err != nil {
return nil, err
}
err = ko.Load(file.Provider(*cfgPath), toml.Parser())
if err != nil {
return nil, err
}
err = ko.Load(env.Provider("MONKEYBEAT_", ".", func(s string) string {
return strings.Replace(strings.ToLower(
strings.TrimPrefix(s, "MONKEYBEAT_")), "__", ".", -1)
}), nil)
if err != nil {
return nil, err
}
return ko, nil
}
// initClickhouse initialises clickhouse connection using the native interface.
func initClickhouse(ko *koanf.Koanf) (driver.Conn, error) {
conn, err := clickhouse.Open(&clickhouse.Options{
Addr: []string{fmt.Sprintf("%s:%d", ko.String("db.host"), ko.Int("db.port"))},
DialTimeout: time.Second * 5,
ReadTimeout: time.Minute * 1,
Debug: ko.Bool("db.debug"),
Auth: clickhouse.Auth{
Database: ko.String("db.name"),
Username: ko.String("db.user"),
Password: ko.String("db.password"),
},
})
if err != nil {
return nil, err
}
if err := conn.Ping(context.Background()); err != nil {
return nil, err
}
return conn, nil
}
// initQueries loads named SQL queries from the queries file.
func initQueries(f fs.FS) (*queries, error) {
// Load SQL queries.
qFile, err := fs.ReadFile(f, "queries.sql")
if err != nil {
return nil, fmt.Errorf("error reading db queries: %v", err)
}
qMap, err := goyesql.ParseBytes(qFile)
if err != nil {
return nil, fmt.Errorf("error parsing SQL queries: %w", err)
}
// Load them to a struct.
// It doesn't prepare them as it's not needed here and additionally
// Clickhouse requires all prepared queries to be sent to DB so it's
// not straightforward to do with goyesql for CH driver.
var q queries
if err := goyesqlx.ScanToStruct(&q, qMap, nil); err != nil {
return nil, fmt.Errorf("error preparing SQL queries: %w", err)
}
return &q, nil
}
// initLogger initializes logger instance.
func initLogger(ko *koanf.Koanf) logf.Logger {
opts := logf.Opts{EnableCaller: true, EnableColor: true}
if ko.String("app.log") == "debug" {
opts.Level = logf.DebugLevel
}
return logf.New(opts)
}