-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
107 lines (92 loc) · 2.15 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
package main
import (
"context"
"fmt"
"os/signal"
"sync"
"syscall"
"time"
"github.com/iden3/reverse-hash-service/hashdb"
"github.com/iden3/reverse-hash-service/http"
"github.com/iden3/reverse-hash-service/log"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/pkg/errors"
"github.com/spf13/viper"
"go.uber.org/zap"
)
// config settings
const (
cfgDb = "db"
cfgListenAddr = "listen_addr"
)
func setupConfig() *viper.Viper {
v := viper.NewWithOptions()
v.SetEnvPrefix("rhs")
v.AutomaticEnv()
v.AddConfigPath(".")
v.SetDefault(cfgDb, "database=rhs")
v.SetDefault(cfgListenAddr, ":8080")
err := v.ReadInConfig()
if err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found; ignore error
} else {
panic(err)
}
}
return v
}
func main() {
v := setupConfig()
if err := log.Setup(); err != nil {
panic(fmt.Sprintf("%+v", err))
}
// Syncing of console causes error. Ignore any errors on Sync.
defer func() { _ = log.Sync() }()
pxpoolConfig, err := pgxpool.ParseConfig(v.GetString(cfgDb))
if err != nil {
panic(err)
}
pxpoolConfig.LazyConnect = true
conn, err := pgxpool.ConnectConfig(context.Background(), pxpoolConfig)
if err != nil {
panic(err)
}
defer conn.Close()
err = conn.Ping(context.Background())
if err != nil {
log.Warnf("database error, start without database connection: %+v",
errors.WithStack(err))
err = nil
}
storage := hashdb.New(conn)
httpSrv := http.New(v.GetString(cfgListenAddr), storage)
ctx, cancel := signal.NotifyContext(context.Background(),
syscall.SIGTERM, syscall.SIGINT)
defer cancel()
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
<-ctx.Done()
closeWithErrLog(httpSrv, 10*time.Second)
}()
log.Infof("Start listening on %v", v.GetString(cfgListenAddr))
err = httpSrv.Run()
if err != nil {
log.Errorw(err.Error(), zap.Error(err))
}
wg.Wait()
log.Infof("Bye")
}
type ctxCloser interface {
Close(context.Context) error
}
func closeWithErrLog(c ctxCloser, timeout time.Duration) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
err := c.Close(ctx)
if err != nil {
log.Errorf("%+v", err)
}
}