Add flags to specify server bind
This commit is contained in:
		
							
								
								
									
										11
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										11
									
								
								main.go
									
									
									
									
									
								
							@ -4,6 +4,7 @@ import (
 | 
				
			|||||||
	"encoding/json"
 | 
						"encoding/json"
 | 
				
			||||||
	"flag"
 | 
						"flag"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"log"
 | 
				
			||||||
	"net"
 | 
						"net"
 | 
				
			||||||
	"net/http"
 | 
						"net/http"
 | 
				
			||||||
	_ "net/http/pprof"
 | 
						_ "net/http/pprof"
 | 
				
			||||||
@ -23,6 +24,10 @@ type errorMessage struct {
 | 
				
			|||||||
	Error string `json:error`
 | 
						Error string `json:error`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var (
 | 
				
			||||||
 | 
						listen_spec = flag.String("bind", "127.0.0.1:8086", "http server bind spec")
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func responseWithError(resp http.ResponseWriter, statusCode int, message string) {
 | 
					func responseWithError(resp http.ResponseWriter, statusCode int, message string) {
 | 
				
			||||||
	resp.Header().Set("Content-Type", "text/plain")
 | 
						resp.Header().Set("Content-Type", "text/plain")
 | 
				
			||||||
	resp.WriteHeader(statusCode)
 | 
						resp.WriteHeader(statusCode)
 | 
				
			||||||
@ -34,11 +39,11 @@ func responseWithJsonError(resp http.ResponseWriter, statusCode int, message str
 | 
				
			|||||||
	body, err := json.Marshal(&errorMessage{
 | 
						body, err := json.Marshal(&errorMessage{
 | 
				
			||||||
		Error: message,
 | 
							Error: message,
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	resp.Header().Set("Content-Length", strconv.Itoa(len(body)))
 | 
					 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		// This should never happen
 | 
							// This should never happen
 | 
				
			||||||
		panic("json marshal failed, check code")
 | 
							panic("json marshal failed, check code")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						resp.Header().Set("Content-Length", strconv.Itoa(len(body)))
 | 
				
			||||||
	resp.WriteHeader(statusCode)
 | 
						resp.WriteHeader(statusCode)
 | 
				
			||||||
	resp.Write(body)
 | 
						resp.Write(body)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -132,8 +137,10 @@ func main() {
 | 
				
			|||||||
		resp.Header().Set("Content-Type", "text/plain")
 | 
							resp.Header().Set("Content-Type", "text/plain")
 | 
				
			||||||
		resp.WriteHeader(http.StatusOK)
 | 
							resp.WriteHeader(http.StatusOK)
 | 
				
			||||||
		server := mapping.Get(ip)
 | 
							server := mapping.Get(ip)
 | 
				
			||||||
 | 
							log.Printf("%s => %s\n", ip, server)
 | 
				
			||||||
		fmt.Fprint(resp, server)
 | 
							fmt.Fprint(resp, server)
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
	ipgeo.Init()
 | 
						ipgeo.Init()
 | 
				
			||||||
	http.ListenAndServe("0.0.0.0:8086", nil)
 | 
						log.Println("HTTP server is running on", *listen_spec)
 | 
				
			||||||
 | 
						http.ListenAndServe(*listen_spec, nil)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user