-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
83 lines (69 loc) · 1.84 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
package main
import (
"context"
"embed"
"io"
"log"
"os"
"os/signal"
"syscall"
"github.com/iotexproject/iotex-analyser-api/apiservice"
"github.com/iotexproject/iotex-analyser-api/config"
"github.com/iotexproject/iotex-analyser-api/db"
"github.com/iotexproject/iotex-analyser-api/model"
)
//go:embed templates
var templates embed.FS
//go:embed docs-html/*
var docsHtml embed.FS
const (
ConfigPath = "ConfigPath"
)
func main() {
configPath := os.Getenv(ConfigPath)
//first load config from env
if configPath == "" {
configPath = config.FindDefaultConfigPath()
}
if configPath == "" {
log.Fatalf("cannot determine default configuration path. %v, %v",
config.DefaultConfigDirs,
config.DefaultConfigFiles)
}
log.Printf("currently config path: %s", configPath)
_, err := config.New(configPath)
if err != nil {
log.Fatalf("Failed to parse config: %v", err)
}
if config.Default.LogPath != "" {
f, err := os.OpenFile(config.Default.LogPath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatalf("Failed to open log file: %v", err)
}
log.SetOutput(io.MultiWriter(f, os.Stdout))
}
log.Printf("loaded config: %+v", config.Default)
db2, err := db.Connect()
if err != nil {
log.Fatalf("failed to connect DB, %v", err)
}
log.Printf("connected to DB")
if err := db2.AutoMigrate(&model.HermesDropRecords{}); err != nil {
log.Fatalf("failed to migrate DB, %v", err)
}
apiservice.DocsHTML = docsHtml
ctx := context.Background()
go func() {
if err := apiservice.StartGRPCService(ctx); err != nil {
log.Fatalf("failed to start GRPC API service, %v", err)
}
}()
go func() {
if err := apiservice.StartGRPCProxyService(templates); err != nil {
log.Fatalf("failed to start HTTP API service, %v", err)
}
}()
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
<-stop
}