This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 82
/
main.go
62 lines (54 loc) · 1.37 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
// Copyright 2022 <[email protected]>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"log"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/zehuamama/balancer/proxy"
)
func main() {
config, err := ReadConfig("config.yaml")
if err != nil {
log.Fatalf("read config error: %s", err)
}
err = config.Validation()
if err != nil {
log.Fatalf("verify config error: %s", err)
}
router := mux.NewRouter()
for _, l := range config.Location {
httpProxy, err := proxy.NewHTTPProxy(l.ProxyPass, l.BalanceMode)
if err != nil {
log.Fatalf("create proxy error: %s", err)
}
// start health check
if config.HealthCheck {
httpProxy.HealthCheck(config.HealthCheckInterval)
}
router.Handle(l.Pattern, httpProxy)
}
if config.MaxAllowed > 0 {
router.Use(maxAllowedMiddleware(config.MaxAllowed))
}
svr := http.Server{
Addr: ":" + strconv.Itoa(config.Port),
Handler: router,
}
// print config detail
config.Print()
// listen and serve
if config.Schema == "http" {
err := svr.ListenAndServe()
if err != nil {
log.Fatalf("listen and serve error: %s", err)
}
} else if config.Schema == "https" {
err := svr.ListenAndServeTLS(config.SSLCertificate, config.SSLCertificateKey)
if err != nil {
log.Fatalf("listen and serve error: %s", err)
}
}
}