Last active
February 22, 2016 04:34
-
-
Save sadhasivam/258c210870ba304b1e87 to your computer and use it in GitHub Desktop.
simple_gin.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net" | |
"time" | |
"github.com/gin-gonic/gin" | |
) | |
func rootHandler(context *gin.Context) { | |
current_time := time.Now() | |
current_time.Format("20060102150405") | |
context.JSON(200, gin.H{ | |
"current_time": current_time, | |
"ip": GetLocalIP(), | |
}) | |
} | |
func main() { | |
router := gin.Default() | |
router.GET("/", rootHandler) | |
router.Run() | |
} | |
func GetLocalIP() string { | |
addrs, err := net.InterfaceAddrs() | |
if err != nil { | |
return "" | |
} | |
for _, address := range addrs { | |
// check the address type and if it is not a loopback the display it | |
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { | |
if ipnet.IP.To4() != nil { | |
return ipnet.IP.String() | |
} | |
} | |
} | |
return "" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment