-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
94 lines (80 loc) · 2.62 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
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"time"
"github.com/eduardohoraciosanto/users/internal/config"
"github.com/eduardohoraciosanto/users/internal/db"
"github.com/eduardohoraciosanto/users/internal/logger"
"github.com/eduardohoraciosanto/users/pkg/health"
"github.com/eduardohoraciosanto/users/pkg/users"
transport "github.com/eduardohoraciosanto/users/transport/http"
)
func main() {
conf := config.New()
l := logger.NewLogger("users api", config.GetVersion(), conf.TracingEnabled)
/*
cfg, err := awsConfig.LoadDefaultConfig(context.TODO(),
// Hard coded credentials.
awsConfig.WithCredentialsProvider(credentials.StaticCredentialsProvider{
Value: aws.Credentials{
AccessKeyID: conf.AWSAccessKey, SecretAccessKey: conf.AWSSecretKey, SessionToken: "",
Source: "environment",
},
}))
if err != nil {
l.WithError(err).Error(context.TODO(), "Unable to load AWS Config")
return
}
db := db.NewDynamoDB("users", dynamodb.NewFromConfig(cfg), l.WithField("svc", "dynamo DB"))
*/
db := db.NewMemDB(l.WithField("svc", "db"))
hsvc := health.NewService(
l.WithField("svc", "health"),
db,
)
userSvc := users.NewService(db, l.WithField("svc", "users"))
httpTransportRouter := transport.NewHTTPRouter(hsvc, userSvc)
srv := &http.Server{
Addr: fmt.Sprintf("0.0.0.0:%s", conf.Port),
// Good practice to set timeouts to avoid Slowloris attacks.
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: httpTransportRouter,
}
l.WithField(
"transport", "http").
WithField(
"port", conf.Port).
Info(context.Background(), "Transport Start")
// Run our server in a goroutine so that it doesn't block.
go func() {
if err := srv.ListenAndServe(); err != nil {
l.WithField(
"transport", "http").
WithError(err).
Info(context.Background(), "Transport Stopped")
}
}()
c := make(chan os.Signal, 1)
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)
// SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught.
signal.Notify(c, os.Interrupt)
// Block until we receive our signal.
<-c
// Create a deadline to wait for.
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
// Doesn't block if no connections, but will otherwise wait
// until the timeout deadline.
srv.Shutdown(ctx)
// Optionally, you could run srv.Shutdown in a goroutine and block on
// <-ctx.Done() if your application should wait for other services
// to finalize based on context cancellation.
l.Info(context.Background(), "Service gracefully shut down")
os.Exit(0)
}