This repository has been archived by the owner on Jun 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
135 lines (116 loc) · 3.2 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
128
129
130
131
132
133
134
135
package main
import (
"context"
"crypto/tls"
"flag"
"log"
"net"
"net/url"
"path/filepath"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres"
_ "github.com/golang-migrate/migrate/v4/source/file"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/horizon/pkg/grpc/lz4"
grpctoken "github.com/hashicorp/horizon/pkg/grpc/token"
hznpb "github.com/hashicorp/horizon/pkg/pb"
"github.com/jinzhu/gorm"
"github.com/sethvargo/go-envconfig"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"github.com/hashicorp/waypoint-hzn/pkg/server"
)
var (
flagDev = flag.Bool("dev", false, "dev mode to run locally")
)
func main() {
flag.Parse()
L := hclog.New(&hclog.LoggerOptions{
Name: "waypoint-hzn",
Level: hclog.Trace,
})
ctx := context.Background()
L.Info("reading configuration from env vars")
var cfg config
if err := envconfig.Process(ctx, &cfg); err != nil {
log.Fatalf("error reading configuration: %s", err)
return
}
// Apply migrations
if cfg.MigrationsApply || *flagDev {
if !filepath.IsAbs(cfg.MigrationsPath) {
path, err := filepath.Abs(cfg.MigrationsPath)
if err != nil {
log.Fatalf("error determining migration path: %s", err)
}
cfg.MigrationsPath = path
}
L.Info("applying migrations", "path", cfg.MigrationsPath)
m, err := migrate.New("file://"+cfg.MigrationsPath, cfg.DatabaseUrl)
if err != nil {
log.Fatalf("error creating migrater: %s", err)
}
err = m.Up()
if err != nil {
if err != migrate.ErrNoChange {
log.Fatalf("error running migrations: %s", err)
}
}
m.Close()
}
L.Info("connecting to database")
u, err := url.Parse(cfg.DatabaseUrl)
if err != nil {
log.Fatal(err)
return
}
db, err := gorm.Open("postgres", migrate.FilterCustomQuery(u).String())
if err != nil {
log.Fatal(err)
return
}
opts := []grpc.DialOption{
grpc.WithPerRPCCredentials(grpctoken.Token(cfg.ControlToken)),
grpc.WithDefaultCallOptions(grpc.UseCompressor(lz4.Name)),
}
if cfg.ControlInsecure {
opts = append(opts, grpc.WithInsecure())
} else {
creds := credentials.NewTLS(&tls.Config{})
opts = append(opts, grpc.WithTransportCredentials(creds))
}
L.Info("dialing horizon control plane", "addr", cfg.ControlAddr)
gcc, err := grpc.Dial(cfg.ControlAddr, opts...)
if err != nil {
log.Fatal(err)
return
}
ln, err := net.Listen("tcp", cfg.ListenAddr)
if err != nil {
log.Fatal(err)
return
}
defer ln.Close()
L.Info("starting server")
err = server.Run(
server.WithContext(ctx),
server.WithLogger(L),
server.WithGRPC(ln),
server.WithDB(db),
server.WithHznControl(hznpb.NewControlManagementClient(gcc)),
server.WithDomain(cfg.Domain),
)
if err != nil {
log.Fatal(err)
}
}
type config struct {
ListenAddr string `env:"LISTEN_ADDR,default=:24030"`
ControlAddr string `env:"CONTROL_ADDR,default=127.0.0.1:24401"`
ControlInsecure bool `env:"CONTROL_INSECURE,default=1"`
ControlToken string `env:"CONTROL_TOKEN,default=aabbcc"`
DatabaseUrl string `env:"DATABASE_URL"`
Domain string `env:"DOMAIN,default=waypoint.localdomain"`
MigrationsApply bool `env:"MIGRATIONS_APPLY"`
MigrationsPath string `env:"MIGRATIONS_PATH,default=./migrations"`
}