-
Notifications
You must be signed in to change notification settings - Fork 1
/
ms.go
134 lines (117 loc) · 2.94 KB
/
ms.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 ms
import (
"context"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/xq-libs/go-ms/config"
"github.com/xq-libs/go-ms/database"
"github.com/xq-libs/go-ms/server"
"gorm.io/gorm"
"log"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
// Listener listener interface
type Listener interface {
OnStart(s *http.Server)
OnShutdown()
}
var (
lcx = sync.Mutex{}
listeners = make([]Listener, 0)
)
// StartServer Start server with http handler
func StartServer(h http.Handler) {
// Listen and Server in 0.0.0.0:8080
log.Println("App server will create with config data.")
s := server.NewServer(h)
// Register start hook
s.RegisterOnShutdown(onShutdown)
// Invoke on start
onStart(s)
// Start Server
go func() {
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("App Server stop with error %v \n", err)
}
}()
log.Printf("App Server started at: %s \n", s.Addr)
waitExit(s)
}
func onStart(s *http.Server) {
for _, l := range listeners {
l.OnStart(s)
}
}
func onShutdown() {
for _, l := range listeners {
l.OnShutdown()
}
}
func waitExit(s *http.Server) {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, os.Kill, syscall.SIGHUP, syscall.SIGTERM, syscall.SIGQUIT)
sig := <-ch
log.Printf("App server got a exit signal: %v", sig)
// Create context
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Shutdown
if err := s.Shutdown(ctx); err != nil {
log.Printf("App server shutdown failure: %v", err)
}
log.Println("App exist success.")
}
// AddListener add listener
func AddListener(l Listener) {
lcx.Lock()
defer lcx.Unlock()
listeners = append(listeners, l)
}
// GetConfigData Get config data with section name
func GetConfigData(name string, data interface{}) {
config.GetSectionData(name, data)
}
// GetDecryptConfigData Get config data with section name and decrypt it by jasypt
func GetDecryptConfigData(name string, data interface{}) {
config.GetDecryptSectionData(name, data)
}
// GetDb get db instance
func GetDb() *gorm.DB {
return database.GetDb()
}
// WrapDbError wrap db error
func WrapDbError(err error) error {
if errors.Is(err, gorm.ErrRecordNotFound) {
return Error{
Cause: err,
Message: DBNotFoundError,
}
} else if err != nil {
return Error{
Cause: err,
Message: DBError,
}
} else {
return nil
}
}
func CustomerRecoveryFunc(sourceCtx *gin.Context, err any) {
ctx := NewContext(sourceCtx)
switch e := err.(type) {
case Error:
ctx.ResponseJson(http.StatusOK, ctx.GetErrorResponse(e))
case error:
ctx.ResponseJson(http.StatusInternalServerError, ctx.GetErrorResponse(NewError2(e, ServerError)))
case string:
ctx.ResponseJson(http.StatusInternalServerError, ctx.GetErrorResponse(NewError2(errors.New(e), ServerError)))
default:
str := fmt.Sprintf("%v", err)
ctx.ResponseJson(http.StatusInternalServerError, ctx.GetErrorResponse(NewError2(errors.New(str), UnknownError)))
}
}