Skip to content

Commit 41a4118

Browse files
committed
Show an error if port is busy. Improvement errors informativity.
1 parent 34815b0 commit 41a4118

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ The reverse proxy server for the local environment that can help to run several
55
- [x] Configure routing via `json`.
66
- [x] Implement reverse-proxy functionality.
77
- [ ] Add documentation how to use it.
8-
- [ ] Show error if port is busy.
8+
- [x] Show error if port is busy.
99
- [ ] Add supporting url paths.
1010
- [ ] Add supporting https.

app.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ func main() {
1414
proxyHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1515
var proxyUrl string
1616

17-
hosts := getMapped()
17+
services := getMapped()
1818

19-
for domainPattern, pUrl := range hosts {
20-
pattern := fmt.Sprintf(`^%s$`, domainPattern)
19+
for servicePattern, pUrl := range services {
20+
pattern := fmt.Sprintf(`^%s$`, servicePattern)
2121
match, err := regexp.MatchString(pattern, r.Host)
2222
if err != nil {
23-
//TODO:: add logging
23+
fmt.Println(err)
2424
continue
2525
}
2626

@@ -30,24 +30,34 @@ func main() {
3030
}
3131
}
3232

33-
// reverseProxy url
34-
//fmt.Println(proxyUrl)
33+
if proxyUrl == "" {
34+
http.Error(w, "No match of service was found in services.json", http.StatusInternalServerError)
35+
return
36+
}
3537

3638
parsedProxyUrl, err := url.Parse(proxyUrl)
3739
if err != nil {
38-
http.Error(w, "Error parsing backend URL", http.StatusInternalServerError)
40+
http.Error(
41+
w,
42+
fmt.Sprintf("Error parsing of service URL %s", proxyUrl),
43+
http.StatusInternalServerError,
44+
)
3945
return
4046
}
4147

4248
reverseProxy := httputil.NewSingleHostReverseProxy(parsedProxyUrl)
4349
reverseProxy.ServeHTTP(w, r)
4450
})
4551

46-
http.ListenAndServe(":80", proxyHandler)
52+
fmt.Println("Reverse proxy is started.")
53+
err := http.ListenAndServe(":80", proxyHandler)
54+
if err != nil {
55+
panic(err)
56+
}
4757
}
4858

4959
func getMapped() map[string]string {
50-
data, err := os.ReadFile("hosts.json") // For read access.
60+
data, err := os.ReadFile("services.json") // For read access.
5161
if err != nil {
5262
panic(err)
5363
}
File renamed without changes.

0 commit comments

Comments
 (0)