-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (89 loc) · 2.36 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
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/exec"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/hammer-code/moonlight/app/certificates/controller"
"github.com/hammer-code/moonlight/app/certificates/repository"
"github.com/hammer-code/moonlight/app/certificates/usecase"
"github.com/hammer-code/moonlight/app/route"
"github.com/hammer-code/moonlight/config"
"github.com/hammer-code/moonlight/pkg/logging"
)
func init() {
logging.InitLogging()
}
func main() {
ctx := context.Background()
cfg, err := config.InitConfig()
if err != nil {
logging.Error(ctx, err, "failed to init config")
}
defer func() {
if r := recover(); r != nil {
panicCause := fmt.Sprintf("Recovered panic, %v", r)
logging.Info(ctx, panicCause)
}
}()
logging.Info(ctx, "success load config", logging.Fields{
"api": cfg.API,
"database_postgres": cfg.DBPostgres,
})
db, err := config.NewDatabase(cfg.DBPostgres)
if err != nil {
logging.Error(ctx, err, "failed to init config")
}
repo := repository.NewRepository(db)
usecase := usecase.Newusecase(repo)
ctrl := controller.NewController(usecase)
handler := route.NewRoute(ctrl)
server := &http.Server{
Addr: fmt.Sprintf("%s:%s", cfg.API.Host, strconv.Itoa(cfg.API.Port)),
Handler: handler,
}
cls := make(chan struct{})
go func() {
for {
time.Sleep(1 * time.Second)
resp, err := http.Get("https://hammercode.org")
if err != nil {
logging.Error(ctx, err, "failed to init config")
return
}
if resp.StatusCode >= 500 {
// auto recover hammercodeweb
cmd := exec.CommandContext(ctx, "pm2", "restart", "hammercodewebsite")
if err = cmd.Run(); err != nil {
return
}
// auto recover moonligt
cmd = exec.CommandContext(ctx, "sudo", "systemctl", "restart", "hmc-cert-go.service")
if err = cmd.Run(); err != nil {
return
}
}
}
}()
go grafulShutdonw(ctx, server, cls)
logging.Info(ctx, "server running", logging.Fields{
"host": cfg.API.Host,
"port": cfg.API.Port,
})
server.ListenAndServe()
<-cls
}
func grafulShutdonw(ctx context.Context, server *http.Server, cls chan struct{}) {
done := make(chan os.Signal, 1)
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM, syscall.SIGKILL)
<-done
if err := server.Shutdown(ctx); err == context.DeadlineExceeded {
logging.Error(ctx, err, "server cannot shutdown")
}
close(cls)
}