-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
158 lines (133 loc) · 3.84 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"context"
_ "embed"
"flag"
"io/fs"
"net"
"net/http"
"os"
"os/signal"
"path/filepath"
"syscall"
"time"
"github.com/stefandanzl/cloudr/bootstrap"
model "github.com/stefandanzl/cloudr/models"
"github.com/stefandanzl/cloudr/pkg/cache"
"github.com/stefandanzl/cloudr/pkg/conf"
"github.com/stefandanzl/cloudr/pkg/util"
"github.com/stefandanzl/cloudr/routers"
)
var (
isEject bool
confPath string
scriptName string
)
//go:embed assets.zip
var staticZip string
var staticFS fs.FS
func init() {
flag.StringVar(&confPath, "c", util.RelativePath("conf.ini"), "Path to the config file.")
flag.BoolVar(&isEject, "eject", false, "Eject all embedded static files.")
flag.StringVar(&scriptName, "database-script", "", "Name of database util script.")
flag.Parse()
staticFS = bootstrap.NewFS(staticZip)
bootstrap.Init(confPath, staticFS)
}
func main() {
// Close database connection
defer func() {
if model.DB != nil {
model.DB.Close()
}
}()
if isEject {
// Start exporting built-in static resource files
bootstrap.Eject(staticFS)
return
}
if scriptName != "" {
// Start running the helper database script
bootstrap.RunScript(scriptName)
return
}
api := routers.InitRouter()
api.TrustedPlatform = conf.SystemConfig.ProxyHeader
server := &http.Server{Handler: api}
// Shut down the server after receiving the signal
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT)
go shutdown(sigChan, server)
defer func() {
<-sigChan
}()
// If SSL is enabled
if conf.SSLConfig.CertPath != "" {
util.Log().Info("Listening to %q", conf.SSLConfig.Listen)
server.Addr = conf.SSLConfig.Listen
if err := server.ListenAndServeTLS(conf.SSLConfig.CertPath, conf.SSLConfig.KeyPath); err != nil {
util.Log().Error("Failed to listen to %q: %s", conf.SSLConfig.Listen, err)
return
}
}
// If Unix is enabled
if conf.UnixConfig.Listen != "" {
// delete socket file before listening
if _, err := os.Stat(conf.UnixConfig.Listen); err == nil {
if err = os.Remove(conf.UnixConfig.Listen); err != nil {
util.Log().Error("Failed to delete socket file: %s", err)
return
}
}
util.Log().Info("Listening to %q", conf.UnixConfig.Listen)
if err := RunUnix(server); err != nil {
util.Log().Error("Failed to listen to %q: %s", conf.UnixConfig.Listen, err)
}
return
}
util.Log().Info("Listening to %q", conf.SystemConfig.Listen)
server.Addr = conf.SystemConfig.Listen
if err := server.ListenAndServe(); err != nil {
util.Log().Error("Failed to listen to %q: %s", conf.SystemConfig.Listen, err)
}
}
func RunUnix(server *http.Server) error {
listener, err := net.Listen("unix", conf.UnixConfig.Listen)
if err != nil {
return err
}
defer listener.Close()
defer os.Remove(conf.UnixConfig.Listen)
if conf.UnixConfig.Perm > 0 {
err = os.Chmod(conf.UnixConfig.Listen, os.FileMode(conf.UnixConfig.Perm))
if err != nil {
util.Log().Warning(
"Failed to set permission to %q for socket file %q: %s",
conf.UnixConfig.Perm,
conf.UnixConfig.Listen,
err,
)
}
}
return server.Serve(listener)
}
func shutdown(sigChan chan os.Signal, server *http.Server) {
sig := <-sigChan
util.Log().Info("Signal %s received, shutting down server...", sig)
ctx := context.Background()
if conf.SystemConfig.GracePeriod != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, time.Duration(conf.SystemConfig.GracePeriod)*time.Second)
defer cancel()
}
// Shutdown http server
err := server.Shutdown(ctx)
if err != nil {
util.Log().Error("Failed to shutdown server: %s", err)
}
// Persist in-memory cache
if err := cache.Store.Persist(filepath.Join(model.GetSettingByName("temp_path"), cache.DefaultCacheFile)); err != nil {
util.Log().Warning("Failed to persist cache: %s", err)
}
close(sigChan)
}