-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
137 lines (113 loc) · 2.88 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"errors"
"flag"
"fmt"
"net/http"
"net/url"
"os"
)
var version = "0.0.0"
func main() {
var notifyFlag, serveFlag, quietFlag, versionFlag bool
var originUrl, proxyUrl, notifUrl string
flag.BoolVar(¬ifyFlag, "notify", false,
"Notify the proxy server that it should reload.")
flag.BoolVar(¬ifyFlag, "n", false, "")
flag.BoolVar(&serveFlag, "serve", false,
"Start the proxy server.")
flag.StringVar(&originUrl, "origin",
"http://127.0.0.1:8080",
"The url of the origin server, where requests are sent.\n"+
"This should be the address of your application.\n")
flag.StringVar(&proxyUrl, "addr",
"http://127.0.0.1:3030",
"The url the proxy server will listen to.\n"+
"This is what you open in the browser.\n")
flag.StringVar(¬ifUrl, "notif-addr",
"http://127.0.0.1:3031",
"The url the notification server will listen to.\n")
flag.BoolVar(&quietFlag, "quiet", false,
"Do not output anything to stdout.")
flag.BoolVar(&versionFlag, "version", false,
"Print the herl version and exit.")
flag.Parse()
if versionFlag {
fmt.Println(version)
return
}
err := run(
notifyFlag, serveFlag, quietFlag,
originUrl, proxyUrl, notifUrl)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func run(
notifyFlag, serveFlag, quietFlag bool,
originUrl, proxyUrl, notifUrl string,
) error {
if serveFlag && notifyFlag {
return errors.New(
"-serve and -notify (-n) are mutually exclusive" +
"see -help for details")
}
urls := URLs{}
err := error(nil)
urls.Origin, err = url.Parse(originUrl)
if err != nil {
return fmt.Errorf(
"origin server url is not valid:\n%w", err)
}
urls.Proxy, err = url.Parse(proxyUrl)
if err != nil {
return fmt.Errorf(
"proxy server url is not valid:\n%w", err)
}
urls.Notification, err = url.Parse(notifUrl)
if err != nil {
return fmt.Errorf(
"notification server url is not valid:\n%w", err)
}
switch {
case notifyFlag:
return notify(urls.Notification)
case serveFlag:
return serve(quietFlag, urls)
default:
return errors.New(
"one of -serve or -notify (-n) must be specified, " +
"see -help for details")
}
}
func notify(url *url.URL) error {
resp, err := http.Post(url.String(), "", nil)
if err != nil {
return errors.Join(
errors.New("failed to reach the notification server, "+
"if it is running on a non-standard\n"+
"address you can set it here with the -notif-addr flag"),
err)
}
if resp.StatusCode != 200 {
return fmt.Errorf(
"something went wrong with the notification server: %s",
resp.Status)
}
return nil
}
func serve(quiet bool, urls URLs) (err error) {
wait := make(chan struct{}, 2)
// If either of these go routines fail we should return
go func() {
err = startProxyServer(quiet, urls)
wait <- struct{}{}
}()
go func() {
err = startNotifServer(urls.Notification)
wait <- struct{}{}
}()
<-wait
return err
}