-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
101 lines (75 loc) · 3.4 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
package main
import (
"flag"
"log"
"os"
"strconv"
"strings"
"github.com/gocql/gocql"
businessService "github.com/micro-business/AddressService/business/service"
"github.com/micro-business/AddressService/config"
dataService "github.com/micro-business/AddressService/data/service"
"github.com/micro-business/AddressService/endpoint"
"github.com/micro-business/Micro-Business-Core/common/diagnostics"
"github.com/micro-business/Micro-Business-Core/system"
)
var consulAddress string
var consulScheme string
var listeningPort int
var cassandraHosts string
var cassandraKeyspace string
var cassandraProtoclVersion int
func main() {
flag.StringVar(&consulAddress, "consul-address", "", "The consul address in form of host:port. The default value is empty string.")
flag.StringVar(&consulScheme, "consul-scheme", "", "The consul scheme. The default value is empty string.")
flag.IntVar(&listeningPort, "listening-port", 0, "The port the application is serving HTTP request on. The default is zero.")
flag.StringVar(&cassandraHosts, "cassandra-hosts", "", "The list of cassandra hosts to connect to. The default value is empty string.")
flag.StringVar(&cassandraKeyspace, "cassandra-keyspace", "", "The cassandra keyspace. The default value is empty string.")
flag.IntVar(&cassandraProtoclVersion, "cassandra-protocl-version", 0, "The cassandra protocl version. The default value is zero.")
flag.Parse()
consulConfigurationReader := config.ConsulConfigurationReader{ConsulAddress: consulAddress, ConsulScheme: consulScheme}
setConsulConfigurationValuesRequireToBeOverriden(&consulConfigurationReader)
endpoint := endpoint.Endpoint{ConfigurationReader: consulConfigurationReader}
cassandraHosts, err := consulConfigurationReader.GetCassandraHosts()
if err != nil {
log.Fatal(err.Error())
return
}
cassandraKeyspace, err := consulConfigurationReader.GetCassandraKeyspace()
if err != nil {
log.Fatal(err.Error())
return
}
cassandraProtocolVersion, err := consulConfigurationReader.GetCassandraProtocolVersion()
if err != nil {
log.Fatal(err.Error())
return
}
uuidGeneratorService := system.UUIDGeneratorServiceImpl{}
cluster := gocql.NewCluster()
cluster.Hosts = cassandraHosts
cluster.ProtoVersion = cassandraProtocolVersion
cluster.Keyspace = cassandraKeyspace
cluster.Consistency = gocql.Quorum
addressDataService := dataService.AddressDataService{UUIDGeneratorService: &uuidGeneratorService, ClusterConfig: cluster}
addressService := businessService.AddressService{AddressDataService: &addressDataService}
endpoint.AddressService = addressService
endpoint.StartServer()
}
func setConsulConfigurationValuesRequireToBeOverriden(consulConfigurationReader *config.ConsulConfigurationReader) {
diagnostics.IsNotNil(consulConfigurationReader, "consulConfigurationReader", "consulConfigurationReader is nil.")
if listeningPort != 0 {
consulConfigurationReader.ListeningPortToOverride = listeningPort
} else if port, err := strconv.Atoi(os.Getenv("PORT")); err == nil && port != 0 {
consulConfigurationReader.ListeningPortToOverride = port
}
if len(cassandraHosts) != 0 {
consulConfigurationReader.CassandraHostsToOverride = strings.Split(cassandraHosts, ",")
}
if len(cassandraKeyspace) != 0 {
consulConfigurationReader.CassandraKeyspaceToOverride = cassandraKeyspace
}
if cassandraProtoclVersion != 0 {
consulConfigurationReader.CassandraProtocolVersionToOverride = cassandraProtoclVersion
}
}