Update to support changes to the gin API. More logic on when not to do DNS resolution

This commit is contained in:
George Shammas 2015-10-26 20:30:36 +00:00
parent 6d248b5cf0
commit b46780971c
1 changed files with 7 additions and 5 deletions

12
main.go
View File

@ -73,8 +73,10 @@ func mainHandler(c *gin.Context) {
c.Set("forwarded", c.Request.Header.Get("X-Forwarded-For")) c.Set("forwarded", c.Request.Header.Get("X-Forwarded-For"))
c.Set("country_code", c.Request.Header.Get("CF-IPCountry")) c.Set("country_code", c.Request.Header.Get("CF-IPCountry"))
ua := strings.Split(c.Request.UserAgent(), "/")
// Only lookup hostname if the results are going to need it. // Only lookup hostname if the results are going to need it.
if stringInSlice(fields[0], []string{"", "all", "host"}) { if stringInSlice(fields[0], []string{"all", "host"}) || (fields[0] == "" && ua[0] != "curl") {
hostnames, err := net.LookupAddr(ip.IP.String()) hostnames, err := net.LookupAddr(ip.IP.String())
if err != nil { if err != nil {
c.Set("host", "") c.Set("host", "")
@ -88,7 +90,6 @@ func mainHandler(c *gin.Context) {
wantsJSON = true wantsJSON = true
} }
ua := strings.Split(c.Request.UserAgent(), "/")
switch fields[0] { switch fields[0] {
case "": case "":
//If the user is using curl, then we should just return the IP, else we show the home page. //If the user is using curl, then we should just return the IP, else we show the home page.
@ -110,9 +111,10 @@ func mainHandler(c *gin.Context) {
return return
} }
fieldResult, err := c.Get(fields[0]) fieldResult, exists := c.Get(fields[0])
if err != nil { if !exists {
c.String(404, "Not Found") c.String(404, "Not Found")
return
} }
c.String(200, fmt.Sprintln(fieldResult)) c.String(200, fmt.Sprintln(fieldResult))
@ -134,7 +136,7 @@ func main() {
r := gin.New() r := gin.New()
r.Use(gin.Recovery()) r.Use(gin.Recovery())
r.Use(Logger()) r.Use(Logger())
r.LoadHTMLTemplates("templates/*") r.LoadHTMLGlob("templates/*")
r.GET("/:field", mainHandler) r.GET("/:field", mainHandler)
r.GET("/", mainHandler) r.GET("/", mainHandler)