-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
131 lines (105 loc) · 3.22 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
package main
import (
"errors"
"expvar"
"fmt"
"net"
"net/smtp"
"os"
"github.com/ardanlabs/conf"
es "github.com/dudakovict/social-network/business/core/email"
"github.com/dudakovict/social-network/business/data/email"
"github.com/dudakovict/social-network/foundation/logger"
_ "go.uber.org/automaxprocs/maxprocs"
"go.uber.org/zap"
"google.golang.org/grpc"
)
var build = "develop"
func main() {
// Construct the application logger.
log, err := logger.New("email-api")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer log.Sync()
// Perform the startup and shutdown sequence.
if err = run(log); err != nil {
log.Errorw("startup", "ERROR", err)
log.Sync()
os.Exit(1)
}
}
func run(log *zap.SugaredLogger) error {
// =========================================================================
// GOMAXPROCS
// Want to see what maxprocs reports.
//opt := maxprocs.Logger(log.Infof)
// Set the correct number of threads for the service
// based on what is available either by the machine or quotas.
/*
if _, err := maxprocs.Set(opt); err != nil {
return fmt.Errorf("maxprocs: %w", err)
}
log.Infow("startup", "GOMAXPROCS", runtime.GOMAXPROCS(0))
*/
// =========================================================================
// Configuration
cfg := struct {
conf.Version
Auth struct {
KeysFolder string `conf:"default:zarf/keys/"`
ActiveKID string `conf:"default:54bb2165-71e1-41a6-af3e-7da4a0e1e2c1"`
}
GRPC struct {
Network string `conf:"default:tcp"`
Address string `conf:"default:0.0.0.0:50084"`
}
SMTP struct {
Username string `conf:"default:example@gmail.com,env:SMTP_USERNAME,mask"`
Password string `conf:"default:jusesendsmtpauth,env:SMTP_PASSWORD,mask"`
Host string `conf:"default:smtp.gmail.com"`
Port string `conf:"default:587"`
Address string `conf:"default:smtp.gmail.com:587"`
}
}{
Version: conf.Version{
SVN: build,
Desc: "copyright information here",
},
}
const prefix = "EMAIL"
help, err := conf.ParseOSArgs(prefix, &cfg)
if err != nil {
if errors.Is(err, conf.ErrHelpWanted) {
fmt.Println(help)
return nil
}
return fmt.Errorf("parsing config: %w", err)
}
// =========================================================================
// App Starting
log.Infow("starting service", "version", build)
defer log.Infow("shutdown complete")
out, err := conf.String(&cfg)
if err != nil {
return fmt.Errorf("generating config for output: %w", err)
}
log.Infow("startup", "config", out)
expvar.NewString("build").Set(build)
// =========================================================================
// Start gRPC Service
log.Infow("startup", "status", "gRPC service started", "host", cfg.GRPC.Address)
conn, err := net.Listen(cfg.GRPC.Network, cfg.GRPC.Address)
if err != nil {
return fmt.Errorf("announcing on the local network: %w", err)
}
grpc := grpc.NewServer()
auth := smtp.PlainAuth("", cfg.SMTP.Username, cfg.SMTP.Password, cfg.SMTP.Host)
es := es.NewEmailServer(log, cfg.SMTP.Address, cfg.SMTP.Username, auth)
email.RegisterEmailServer(grpc, &es)
if err := grpc.Serve(conn); err != nil {
log.Errorw("shutdown", "status", "gRPC v1 server closed", "host", cfg.GRPC.Address, "ERROR", err)
}
return nil
}