forked from just1689/path-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pproxy.go
67 lines (59 loc) · 1.09 KB
/
pproxy.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
package main
import (
"encoding/json"
"fmt"
"github.com/just1689/path-proxy/pprox"
"github.com/sirupsen/logrus"
"io/ioutil"
"os"
)
func main() {
if generate() {
return
}
logrus.Println("Starting up...")
b := getConfigByEnv()
c := readConfigOrPanic(b)
pprox.Proxy(c)
}
func generate() bool {
if len(os.Args) == 2 && (os.Args[1] == "generate" || os.Args[1] == "g") {
c := pprox.Config{
Listen: ":8080",
Routes: make([]pprox.Route, 1),
}
c.Routes[0] = pprox.Route{
Target: "http://localhost:8080/static",
Prefix: "/static",
}
b, err := json.Marshal(c)
if err != nil {
panic(err)
}
fmt.Println(string(b))
return true
}
return false
}
func readConfigOrPanic(b []byte) *pprox.Config {
logrus.Println("Unmarshal config")
c := pprox.Config{}
err := json.Unmarshal(b, &c)
if err != nil {
panic(err)
}
return &c
}
func getConfigByEnv() []byte {
f := os.Getenv("config")
if f == "" {
f = "config.json"
logrus.Println("Defaulting to ", f)
}
logrus.Println("Loading from ", f)
dat, err := ioutil.ReadFile(f)
if err != nil {
panic(err)
}
return dat
}