@@ -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
4959func 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 }
0 commit comments