-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
81 lines (70 loc) · 1.79 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
package main
import (
"fmt"
"github.com/NYTimes/gziphandler"
"github.com/briandowns/spinner"
"github.com/fatih/color"
"github.com/fsnotify/fsnotify"
"github.com/gorilla/mux"
"github.com/s1ntaxe770r/pawxi/proxy"
"github.com/s1ntaxe770r/pawxi/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"log"
"net/http"
"time"
)
func handle(err error) {
if err != nil {
log.Fatal(err)
}
}
var (
router = mux.NewRouter()
routes []proxy.Route
)
func main() {
viper.AddConfigPath(".")
viper.SetConfigType("toml")
viper.SetConfigName("pawxi")
readerr := viper.ReadInConfig()
port := fmt.Sprintf(":%s", viper.GetString("proxy.binds"))
server := utils.NewServer(router, port)
viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
logrus.Warn("Config file changed:", e.Name)
logrus.Warn("Restarting Proxy")
s := spinner.New(spinner.CharSets[2], 100*time.Millisecond)
s.Start()
s.Color("yellow")
// Semi Graceful shutdown ?
time.Sleep(5 * time.Second)
s.Stop()
utils.Exec()
})
if readerr != nil {
panic(fmt.Errorf("fatal error config file: %s", readerr))
}
err := viper.UnmarshalKey("proxy.routes", &routes)
if err != nil {
logrus.Error("unable to unmarshal into struct REASON : %v", err.Error())
}
usegzip := viper.GetBool("proxy.usegzip")
for _, route := range routes {
// individually proxy each request
proxy := proxy.Tunnel(route)
if usegzip != true {
go router.HandleFunc(route.Path, func(w http.ResponseWriter, r *http.Request) {
proxy.ServeHTTP(w, r)
})
} else {
zipped := gziphandler.GzipHandler(proxy)
router.Handle(route.Path, zipped)
}
}
utils.Vizualize(routes)
logrus.Infof(color.HiGreenString("proxy started on %s"), port)
if err := server.ListenAndServe(); err != nil {
log.Fatalf("ListenAndServe error: %v", err)
}
}