forked from solana-labs/solana-ping-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
127 lines (109 loc) · 3.13 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
125
126
127
package main
import (
"flag"
"log"
"time"
_ "github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/dialers/postgres"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
var config Config
// Cluster enum
type Cluster string
var database *gorm.DB
const useGCloudDB = true
type ClustersToRun string
// Cluster enum
const (
MainnetBeta Cluster = "MainnetBeta"
Testnet = "Testnet"
Devnet = "Devnet"
Atlasnet = "Atlasnet"
)
var influxdb *InfluxdbClient
var userInputClusterMode string
var atlasnetFailover RPCFailover
const (
RunMainnetBeta ClustersToRun = "mainnet"
RunTestnet = "testnet"
RunDevnet = "devnet"
RunAtlasnet = "atlasnet"
RunAllClusters = "all"
)
func init() {
config = loadConfig()
log.Println(" *** Config Start *** ")
log.Println("--- //// Database Config --- ")
log.Println(config.Database)
log.Println("--- //// Influxdb Config --- ")
log.Println(config.InfluxdbConfig)
log.Println("--- //// Retension --- ")
log.Println(config.Retension)
log.Println("--- //// ClusterCLIConfig--- ")
log.Println("ClusterCLIConfig Atlasnet", config.ClusterCLIConfig.ConfigAtlasnet)
log.Println("--- Atlasnet Ping --- ")
log.Println("Atlasnet.ClusterPing.APIServer", config.Atlasnet.ClusterPing.APIServer)
log.Println("Atlasnet.ClusterPing.PingServiceEnabled", config.Atlasnet.ClusterPing.PingServiceEnabled)
log.Println("Atlasnet.ClusterPing.AlternativeEnpoint.HostList", config.Atlasnet.ClusterPing.AlternativeEnpoint.HostList)
log.Println("Atlasnet.ClusterPing.PingConfig", config.Atlasnet.ClusterPing.PingConfig)
log.Println("Atlasnet.ClusterPing.Report", config.Atlasnet.ClusterPing.Report)
log.Println(" *** Config End *** ")
ResponseErrIdentifierInit()
StatisticErrExpectionInit()
AlertErrExpectionInit()
ReportErrExpectionInit()
PingTakeTimeErrExpectionInit()
if config.Database.UseGoogleCloud {
gormDB, err := gorm.Open(postgres.New(postgres.Config{
DriverName: "cloudsqlpostgres",
DSN: config.DBConn,
}))
if err != nil {
log.Panic(err)
}
database = gormDB
} else {
gormDB, err := gorm.Open(postgres.Open(config.DBConn), &gorm.Config{})
if err != nil {
log.Panic(err)
}
database = gormDB
}
err := database.AutoMigrate(&PingResult{})
if err != nil {
log.Printf("Failed to auto migrate: %v", err)
}
log.Println("database connected")
if config.InfluxdbConfig.Enabled {
influxdb = NewInfluxdbClient(config.InfluxdbConfig)
}
/// ---- Start RPC Failover ---
log.Println("RPC Endpoint Failover Setting ---")
if len(config.Atlasnet.AlternativeEnpoint.HostList) <= 0 {
atlasnetFailover = NewRPCFailover([]RPCEndpoint{{
Endpoint: "https://api.atlasnet.staratlas.cloud",
Piority: 1,
MaxRetry: 30}})
} else {
atlasnetFailover = NewRPCFailover(config.Atlasnet.AlternativeEnpoint.HostList)
}
}
func main() {
defer func() {
if influxdb != nil {
influxdb.ClientClose()
}
if database != nil {
sqldb, err := database.DB()
if err == nil {
sqldb.Close()
}
}
}()
flag.Parse()
go launchWorkers(RunAtlasnet)
go APIService(RunAtlasnet)
for {
time.Sleep(10 * time.Second)
}
}