-
Notifications
You must be signed in to change notification settings - Fork 0
/
media_service.go
134 lines (112 loc) · 2.93 KB
/
media_service.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
package main
import (
"context"
"fmt"
"github.com/gin-gonic/gin"
amqp "github.com/rabbitmq/amqp091-go"
"net/http"
"os"
"os/signal"
"pc-ziegert.de/media_service/common/config"
"pc-ziegert.de/media_service/common/constant"
e "pc-ziegert.de/media_service/common/error"
l "pc-ziegert.de/media_service/common/log"
s "pc-ziegert.de/media_service/service"
"pc-ziegert.de/media_service/service/mq"
"pc-ziegert.de/media_service/service/utils"
"syscall"
"time"
)
func main() {
// Load config
conf := config.LoadConfig()
// Set logging level
l.SetLevel(conf.Log.Level)
l.Infof("Start Media Service %s.", constant.AppVersion)
// Create initializer
i := s.NewInitializer(conf)
// Open RabbitMQ connection
// create Exchange and routing keys
mq := i.GetRabbitMQ()
mq.OpenRabbitmq()
defer mq.CloseRabbitmq()
mq.Configure()
// Open and create/update database
redis := i.GetRedis()
redis.NewClient()
defer redis.CloseClient()
// Schedule jobs
i.GetJobService().ScheduleJobs()
// Create router
router := gin.New()
// Set release mode for router
if i.GetConfig().Log.Level != "debug" {
gin.SetMode(gin.ReleaseMode)
}
// configure router
s.ConfigureRouter(i, router)
// Add Root handlers
s.AddRootHandlers(router)
// Add probe handlers
s.AddProbeHandlers(i, router)
// Add probe handlers
s.AddMetricsHandlers(i, router)
// Add Swagger UI handlers
s.AddSwaggerUiHandlers(router)
// Configure API routing
s.ConfigureApiRouting(i, router)
// Create server
server := &http.Server{
Addr: fmt.Sprintf(":%d", i.GetConfig().HTTP.Port),
Handler: router,
ReadTimeout: time.Second * 10,
WriteTimeout: time.Second * 10,
MaxHeaderBytes: 1 << 20,
}
// Start HTTP server
go func() {
// s connections
l.Infof("start server: :%d", i.GetConfig().HTTP.Port)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
l.Fatalf("listen: %s", err)
}
}()
msgs, err := consume(mq)
if err != nil {
l.Error("Can not consume queue")
}
rabServ := i.GetRabbitMqService()
go rabServ.Consumer(msgs)
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal, 1)
// kill (no param) default send syscanll.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall. SIGKILL but can"t be catch, so don't need add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
l.Info("Shutdown Server ...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
l.Fatalf("Server Shutdown:", err)
}
// catching ctx.Done(). timeout of 5 seconds.
select {
case <-ctx.Done():
l.Info("timeout of 5 seconds.")
}
l.Info("Server exiting")
}
func consume(r *mq.RabbitMQ) (<-chan amqp.Delivery, *e.Error) {
h := utils.GetHostname()
return r.Consume(
constant.RabbitMQQueueMediaServiceName,
*h,
false,
false,
false,
false,
nil,
)
}