-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
88 lines (68 loc) · 1.55 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
package main
import (
"context"
"fmt"
"log"
"net"
"os"
"os/signal"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/spf13/viper"
"github.com/rizkix/wired/config"
"google.golang.org/grpc"
"github.com/labstack/echo"
)
func init() {
viper.SetConfigFile(`config.json`)
err := viper.ReadInConfig()
if err != nil {
panic(err)
}
if viper.GetBool(`debug`) {
fmt.Println("Service RUN on DEBUG mode")
}
}
func main() {
app, err := InitializeApp()
if err != nil {
fmt.Printf("Cannot start app: %+v\n", err)
os.Exit(1)
}
app.Start()
}
type App struct {
Config config.Config
GRPC *grpc.Server
HTTP *echo.Echo
}
func NewApp(c config.Config, h *echo.Echo, g *grpc.Server) App {
return App{Config: c, HTTP: h, GRPC: g}
}
func (e App) Start() {
go func() {
e.HTTP.Start(viper.GetString(`server.address`))
}()
go func() {
lis, err := net.Listen("tcp", viper.GetString(`grpc.address`))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
if err := e.GRPC.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}()
quit := make(chan os.Signal)
signal.Notify(quit, os.Interrupt)
<-quit
fmt.Println("Shutting down server")
// give n seconds for server to shutdown gracefully
duration := time.Duration(viper.GetInt(`context.timeout`)) * time.Second
ctx, cancel := context.WithTimeout(context.Background(), duration)
defer cancel()
if err := e.HTTP.Shutdown(ctx); err != nil {
fmt.Printf("Failed to shut down server gracefully: %s", err)
}
e.GRPC.GracefulStop()
fmt.Printf("Server shutted down")
}