-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
122 lines (98 loc) · 3.42 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
package main
import (
"fmt"
"log"
"net"
"net/http"
"runtime"
"time"
"github.com/antihax/goesi"
"google.golang.org/grpc"
"github.com/EVE-Tools/element43/go/lib/transport"
"github.com/EVE-Tools/static-data/lib/locations"
"github.com/EVE-Tools/static-data/lib/server"
pb "github.com/EVE-Tools/static-data/lib/staticData"
"github.com/EVE-Tools/static-data/lib/types"
"github.com/boltdb/bolt"
"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus"
"github.com/grpc-ecosystem/go-grpc-middleware/tags"
"github.com/kelseyhightower/envconfig"
"github.com/sirupsen/logrus"
)
// Config holds the application's configuration info from the environment.
type Config struct {
DBPath string `default:"static-data.db" envconfig:"db_path"`
LogLevel string `default:"info" envconfig:"log_level"`
Port string `default:"43000" envconfig:"port"`
ESIHost string `default:"esi.tech.ccp.is" envconfig:"esi_host"`
StructureHuntHost string `default:"stop.hammerti.me.uk" envconfig:"structure_hunt_host"`
DisableTLS bool `default:"false" envconfig:"disable_tls"`
}
func main() {
config := loadConfig()
startEndpoint(config)
// Terminate this goroutine, crash if all other goroutines exited
runtime.Goexit()
}
// Load configuration from environment
func loadConfig() Config {
config := Config{}
envconfig.MustProcess("STATIC_DATA", &config)
logLevel, err := logrus.ParseLevel(config.LogLevel)
if err != nil {
panic(err)
}
logrus.SetLevel(logLevel)
logrus.Debugf("Config: %q", config)
return config
}
// getClients generates API clients and base URLs
func getClients(config Config) (*goesi.APIClient, *http.Client, string) {
const userAgent string = "Element43/static-data (element-43.com)"
const timeout time.Duration = time.Duration(time.Second * 30)
var structureHuntURL string
// Initialize clients
genericClient := &http.Client{
Timeout: time.Minute,
Transport: transport.NewTransport(userAgent),
}
httpClientESI := &http.Client{
Timeout: timeout,
Transport: transport.NewESITransport(userAgent, timeout),
}
esiClient := goesi.NewAPIClient(httpClientESI, userAgent)
if config.DisableTLS {
esiClient.ChangeBasePath(fmt.Sprintf("http://%s:443", config.ESIHost))
structureHuntURL = fmt.Sprintf("http://%s:443/api/structure/all", config.StructureHuntHost)
} else {
esiClient.ChangeBasePath(fmt.Sprintf("https://%s", config.ESIHost))
structureHuntURL = fmt.Sprintf("https://%s/api/structure/all", config.StructureHuntHost)
}
return esiClient, genericClient, structureHuntURL
}
// Init DB and start gRPC endpoint.
func startEndpoint(config Config) {
db, err := bolt.Open(config.DBPath, 0600, &bolt.Options{Timeout: 1 * time.Second})
if err != nil {
panic(err)
}
esiClient, genericClient, url := getClients(config)
locations.Initialize(esiClient,
genericClient,
url,
db)
types.Initialize(esiClient, db)
var opts []grpc.ServerOption
var logOpts []grpc_logrus.Option
opts = append(opts, grpc_middleware.WithUnaryServerChain(
grpc_ctxtags.UnaryServerInterceptor(),
grpc_logrus.UnaryServerInterceptor(logrus.NewEntry(logrus.New()), logOpts...)))
listener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%s", config.Port))
if err != nil {
log.Fatalf("could not listen: %v", err)
}
grpcServer := grpc.NewServer(opts...)
pb.RegisterStaticDataServer(grpcServer, &server.Server{})
grpcServer.Serve(listener)
}