-
Notifications
You must be signed in to change notification settings - Fork 120
/
main.go
165 lines (142 loc) · 4.04 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
159
160
161
162
163
164
165
package main
import (
"crypto/tls"
"embed"
"flag"
"github.com/valyala/fasthttp"
"golang.org/x/crypto/acme"
"golang.org/x/crypto/acme/autocert"
"goyoubbs/controller"
"goyoubbs/cronjob"
mdw "goyoubbs/middleware"
"goyoubbs/model"
"log"
"net"
"os"
"os/signal"
"syscall"
"time"
)
//go:embed static
var staticFs embed.FS
var (
addr = flag.String("addr", ":8080", "TCP address to listen to")
sdbDir = flag.String("sdbDir", "localdb", "Directory to serve sdb from")
autoTLS = flag.String("autoTLS", "", "user Let's Encrypt. Leave empty for disabling")
domain = flag.String("domain", "", "set domain when user Let's Encrypt")
certFile = flag.String("certFile", "", "Path to TLS certificate file")
keyFile = flag.String("keyFile", "", "Path to TLS key file")
)
func main() {
flag.Parse()
myApp := &model.Application{}
myApp.Init(*addr, *sdbDir, &staticFs)
// cron job
cr := cronjob.BaseHandler{App: myApp}
go cr.MainCronJob()
// 挂载路由
controller.RouterReload(myApp)
mux := myApp.Mux
var hd fasthttp.RequestHandler
if myApp.Cf.Site.IsDevMod {
hd = mdw.RspNoCache(mux.Handler)
} else {
hd = mux.Handler
}
log.Printf("Serving sdb from directory %q", *sdbDir)
srv := &fasthttp.Server{
Handler: hd,
Name: "gyb Service",
ReadBufferSize: 4096,
WriteBufferSize: 4096,
// MaxConnsPerIP:
// MaxRequestsPerConn:
DisableKeepalive: true,
DisableHeaderNamesNormalizing: true,
ReadTimeout: 200 * time.Second, // important
WriteTimeout: 300 * time.Second,
IdleTimeout: time.Minute,
MaxRequestBodySize: 2000 << 20, // 100MB,上传文件最大值
}
// server model
if len(*autoTLS) > 0 && len(*domain) > 0 {
// Let's Encrypt, auto cert
go func() {
log.Printf("TCP address to listen to %q", *addr)
if err := fasthttp.ListenAndServe(*addr, redirectHandler); err != nil {
log.Fatalf("HTTP server ListenAndServe: %v", err)
}
}()
m := &autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(*domain), // Replace with your domain.
Cache: autocert.DirCache("./certs"),
}
cfg := &tls.Config{
GetCertificate: m.GetCertificate,
NextProtos: []string{
"http/1.1", acme.ALPNProto,
},
}
// Let's Encrypt tls-alpn-01 only works on port 443.
ln, err := net.Listen("tcp4", "0.0.0.0:443") /* #nosec G102 */
if err != nil {
log.Fatalf("net Listen: %v", err)
}
lnTls := tls.NewListener(ln, cfg)
go func() {
if err = srv.Serve(lnTls); err != nil {
log.Fatalf("HTTPS server: %v", err)
}
}()
} else if len(*certFile) > 0 && len(*keyFile) > 0 {
// TLS with ertFile & keyFile
//go func() {
// log.Printf("TCP address to listen to %q", *addr)
// if err := fasthttp.ListenAndServe(*addr, redirectHandler); err != nil {
// log.Fatalf("HTTP server ListenAndServe: %v", err)
// }
//}()
go func() {
if err := srv.ListenAndServeTLS("0.0.0.0"+*addr, *certFile, *keyFile); err != nil {
log.Fatalf("HTTPS ListenAndServeTLS: %v", err)
}
}()
} else {
// only http
go func() {
log.Printf("TCP address to listen to %q", *addr)
if err := srv.ListenAndServe(*addr); err != nil {
log.Fatalf("HTTP server ListenAndServe: %v", err)
}
}()
}
// graceful stop
// subscribe to SIGINT signals
signalChan := make(chan os.Signal, 1)
signal.Notify(
signalChan,
syscall.SIGHUP, // kill -SIGHUP XXXX
syscall.SIGINT, // kill -SIGINT XXXX or Ctrl+c
syscall.SIGTERM, // kill -SIGTERM XXXX
syscall.SIGQUIT, // kill -SIGQUIT XXXX
)
<-signalChan
log.Printf("os.Interrupt - shutting down...\n")
if err := srv.Shutdown(); err != nil {
log.Println("Shutdown err", err)
defer os.Exit(1)
} else {
myApp.Close() // !important 留意上下文位置
log.Println("gracefully stopped")
}
//go func() {
// <-signalChan
// log.Fatal("os.Kill - terminating...\n")
//}()
defer os.Exit(0)
return
}
func redirectHandler(ctx *fasthttp.RequestCtx) {
ctx.Redirect("https://"+*domain+string(ctx.RequestURI()), fasthttp.StatusFound)
}