-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
116 lines (98 loc) · 2.62 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"errors"
"fmt"
"net/http"
"net/url"
"os"
"github.com/kajikentaro/flexy-proxy/models"
"github.com/kajikentaro/flexy-proxy/proxy"
"github.com/kajikentaro/flexy-proxy/utils"
"github.com/spf13/cobra"
)
// this will be specified like:
// go build -ldflags "-X 'main.version=1.0.0'"
var version string
func getVersion() string {
if version == "" {
return "unknown"
}
return version
}
func startProxy(customConfigPath string, portNum int) {
proxyConfig, err := utils.ParseConfig(customConfigPath)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
proxy := proxy.SetupProxy(proxyConfig)
addr := fmt.Sprintf(":%d", portNum)
proxyConfig.Logger.Info(fmt.Sprintf("Proxy started on %s", addr))
fmt.Fprintf(os.Stderr, "%v\n", http.ListenAndServe(addr, proxy))
}
func testRoute(customConfigPath string, testUrl string) error {
proxyConfig, err := utils.ParseConfig(customConfigPath)
if err != nil {
return err
}
parsedUrl, err := url.Parse(testUrl)
if err != nil {
return err
}
handler, matchedUrl, err := proxyConfig.Router.GetHandler(parsedUrl)
if errors.Is(err, models.ErrRouteNotFound) {
return fmt.Errorf("route not found")
}
if err != nil {
return err
}
fmt.Println("URL:", matchedUrl)
fmt.Println("Type:", handler.GetType())
fmt.Println("Info:")
for key, val := range handler.GetResponseInfo() {
fmt.Printf(" %s: %s\n", key, val)
}
return nil
}
func main() {
var (
customConfigPath string
portNum int
)
rootCmd := &cobra.Command{
Use: "main",
Short: "A YAML-based flexible proxy for software development",
Run: func(cmd *cobra.Command, args []string) {
startProxy(customConfigPath, portNum)
},
}
rootCmd.PersistentFlags().StringVarP(&customConfigPath, "config", "f", utils.DEFAULT_CONFIG_PATH, "Path to custom config file")
rootCmd.PersistentFlags().IntVarP(&portNum, "port", "p", 8888, "Port number")
versionCmd := &cobra.Command{
Use: "version",
Short: "Show version",
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(getVersion())
},
}
startCmd := &cobra.Command{
Use: "start",
Short: "Start the proxy server",
Run: func(cmd *cobra.Command, args []string) {
startProxy(customConfigPath, portNum)
},
}
testCmd := &cobra.Command{
Use: "test [URL]",
Short: "Print the configured routing information for the given URL",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
if err := testRoute(customConfigPath, args[0]); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
},
}
rootCmd.AddCommand(versionCmd, startCmd, testCmd)
rootCmd.Execute()
}