Skip to content

Instantly share code, notes, and snippets.

@sadhasivam
Last active February 22, 2016 04:34
Show Gist options
  • Save sadhasivam/258c210870ba304b1e87 to your computer and use it in GitHub Desktop.
Save sadhasivam/258c210870ba304b1e87 to your computer and use it in GitHub Desktop.
simple_gin.go
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