-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
59 lines (50 loc) · 1.29 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
package main
import (
"context"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os/signal"
"syscall"
"time"
)
func start(srv *http.Server) {
env := GetEnv()
innerUrl := "http://" + env.InnerHost
url, err := url.Parse(innerUrl)
if err != nil {
log.Fatalf("[HRP] Invalid inner url: %s\n", innerUrl)
}
proxy := httputil.NewSingleHostReverseProxy(url)
log.Printf("[HRP] Proxy URL: %s\n", innerUrl)
srv.Addr = env.ServrAddr()
srv.Handler = proxy
log.Printf("[HRP] Serve on %s\n", srv.Addr)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Printf("[HRP] Serve is end: %v\n", err)
} else {
log.Println("[HRP] Serve is end gracefully")
}
}
func stop(srv *http.Server) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Printf("[HRP] Server stopped: %v\n", err)
} else {
log.Println("[HRP] Server stopped")
}
}
func main() {
srv := &http.Server{}
go start(srv)
// Wait for interrupt signal to gracefully shutdown the server
ctx, reset := signal.NotifyContext(context.Background(),
syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
log.Println("[HRP] Press Ctrl+C to shutdown server...")
<-ctx.Done()
log.Println("[HRP] Shutdown server...")
reset()
stop(srv)
}