-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
55 lines (48 loc) · 1.12 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
package main
import (
"flag"
)
type Opt struct {
Config string
Listen int
Prefix string
Root string
Upstream string
Version bool
}
var opt Opt
func init() {
flag.StringVar(&opt.Config, "c", "", "config file")
flag.IntVar(&opt.Listen, "l", 9000, "server listening port")
flag.StringVar(&opt.Prefix, "prefix", "/", "prefix of request path, example: /api/")
flag.StringVar(&opt.Root, "root", "", "root path of static files, example: dist")
flag.StringVar(&opt.Upstream, "upstream", "", "upstream server, example: http://localhost:3000")
flag.BoolVar(&opt.Version, "version", false, "show current version")
}
const VERSION = "0.2"
func main() {
flag.Parse()
if opt.Version {
println(VERSION)
return
}
if opt.Config != "" {
config, err := ParseConfig(opt.Config)
if err != nil {
panic(err)
}
Proxy(config)
} else {
var config ProxyConfig
var proxyItem ProxyItem
config.Listen = opt.Listen
proxyItem.Prefix = opt.Prefix
if opt.Root != "" {
proxyItem.Root = opt.Root
} else if opt.Upstream != "" {
proxyItem.Upstream = opt.Upstream
}
config.Proxy = []ProxyItem{proxyItem}
Proxy(config)
}
}