-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
113 lines (99 loc) · 2.58 KB
/
main.go
File metadata and controls
113 lines (99 loc) · 2.58 KB
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
package main
import (
"context"
"embed"
"fmt"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
"github.com/Lord-Y/versions/commons"
customLogger "github.com/Lord-Y/versions/logger"
"github.com/Lord-Y/versions/mysql"
"github.com/Lord-Y/versions/postgres"
"github.com/Lord-Y/versions/routers"
"github.com/rs/zerolog/log"
)
//go:embed sql/postgres
var sqlpostgres embed.FS
//go:embed sql/mysql
var sqlmysql embed.FS
func init() {
customLogger.SetLoggerLogLevel()
if commons.RedisEnabled() {
if commons.GetRedisURI() == "" {
msg := "REDIS_URI environment variable must be set"
log.Fatal().Err(fmt.Errorf(msg)).Msg(msg)
return
}
}
if os.Getenv("SLEEP") != "" {
sleep, _ := strconv.Atoi(os.Getenv("SLEEP"))
log.Info().Msgf("Sleeping %d", sleep)
time.Sleep(time.Duration(sleep) * time.Second)
}
if commons.SqlDriver == "" {
msg := "SQL_DRIVER environment variable can only be mysql or postgres"
log.Fatal().Err(fmt.Errorf(msg)).Msg(msg)
return
}
if strings.TrimSpace(os.Getenv("DB_URI")) == "" {
msg := "DB_URI environment variable must be set"
log.Fatal().Err(fmt.Errorf(msg)).Msg(msg)
return
}
switch commons.SqlDriver {
case "mysql":
mysql.InitDB(sqlmysql)
case "postgres":
postgres.InitDB(sqlpostgres)
default:
msg := "SQL_DRIVER environment variable can only be mysql or postgres"
log.Fatal().Err(fmt.Errorf(msg)).Msg(msg)
return
}
}
func main() {
var appPort string
router := routers.SetupRouter()
port := strings.TrimSpace(os.Getenv("APP_PORT"))
switch strings.HasPrefix(port, ":") {
case true:
appPort = port
case false:
if port == "" {
appPort = ":8080"
} else {
appPort = fmt.Sprintf(":%s", port)
}
}
srv := &http.Server{
Addr: appPort,
Handler: router,
}
log.Info().Msgf("Starting server on port %s", appPort)
go func() {
// service connections
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal().Err(err).Msg("Startup failed")
}
}()
// 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
log.Info().Msg("Shutting down server")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatal().Err(err).Msg("Server shutted down abruptly")
}
log.Info().Msg("Server exited successfully")
}