-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
87 lines (77 loc) · 2.17 KB
/
main.go
File metadata and controls
87 lines (77 loc) · 2.17 KB
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
package main
import (
"context"
"fmt"
"os"
"github.com/numaproj/numaflow-go/pkg/sourcer"
"github.com/numaproj-contrib/nats-source-go/pkg/config"
"github.com/numaproj-contrib/nats-source-go/pkg/nats"
"github.com/numaproj-contrib/nats-source-go/pkg/utils"
)
func main() {
logger := utils.NewLogger()
// Get the config file path and format from env vars
var format string
format, ok := os.LookupEnv("CONFIG_FORMAT")
if !ok {
logger.Info("CONFIG_FORMAT not set, defaulting to yaml")
format = "yaml"
}
var config *config.Config
var err error
config, err = getConfigFromEnvVars(format)
if err != nil {
config, err = getConfigFromFile(format)
if err != nil {
logger.Panic("Failed to parse config file : ", err)
} else {
logger.Info("Successfully parsed config file")
}
} else {
logger.Info("Successfully parsed config from env vars")
}
natsSrc, err := nats.New(config)
if err != nil {
logger.Panic("Failed to create nats source : ", err)
}
defer natsSrc.Close()
err = sourcer.NewServer(natsSrc).Start(context.Background())
if err != nil {
logger.Panic("Failed to start source server : ", err)
}
}
func getConfigFromFile(format string) (*config.Config, error) {
if format == "yaml" {
parser := &config.YAMLConfigParser{}
content, err := os.ReadFile(fmt.Sprintf("%s/nats-config.yaml", utils.ConfigVolumePath))
if err != nil {
return nil, err
}
return parser.Parse(string(content))
} else if format == "json" {
parser := &config.JSONConfigParser{}
content, err := os.ReadFile(fmt.Sprintf("%s/nats-config.json", utils.ConfigVolumePath))
if err != nil {
return nil, err
}
return parser.Parse(string(content))
} else {
return nil, fmt.Errorf("invalid config format %s", format)
}
}
func getConfigFromEnvVars(format string) (*config.Config, error) {
var c string
c, ok := os.LookupEnv("NATS_CONFIG")
if !ok {
return nil, fmt.Errorf("NATS_CONFIG environment variable is not set")
}
if format == "yaml" {
parser := &config.YAMLConfigParser{}
return parser.Parse(c)
} else if format == "json" {
parser := &config.JSONConfigParser{}
return parser.Parse(c)
} else {
return nil, fmt.Errorf("invalid config format %s", format)
}
}