query other ip
This commit is contained in:
@@ -5,6 +5,6 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func HandleIp(c *gin.Context) {
|
||||
func HandleMyIp(c *gin.Context) {
|
||||
c.String(http.StatusOK, c.ClientIP())
|
||||
}
|
||||
|
||||
@@ -3,12 +3,26 @@ package api
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/rs/zerolog/log"
|
||||
"net"
|
||||
"net/http"
|
||||
"thuanle.me/ip-info/internal/data"
|
||||
)
|
||||
|
||||
func HandleJson(c *gin.Context) {
|
||||
ip := c.ClientIP()
|
||||
HandleIpInfo(c, ip)
|
||||
}
|
||||
|
||||
func HandleOtherIp(c *gin.Context) {
|
||||
ip := c.Param("ip")
|
||||
if net.ParseIP(ip) == nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid ip"})
|
||||
return
|
||||
}
|
||||
HandleIpInfo(c, ip)
|
||||
}
|
||||
|
||||
func HandleIpInfo(c *gin.Context, ip string) {
|
||||
if !data.Ins().IsLoaded() {
|
||||
log.Error().Msg("DB is not loaded")
|
||||
c.String(http.StatusInternalServerError, "Try again later")
|
||||
@@ -26,5 +40,4 @@ func HandleJson(c *gin.Context) {
|
||||
|
||||
ipData["ip"] = ip
|
||||
c.JSON(http.StatusOK, ipData)
|
||||
|
||||
}
|
||||
|
||||
@@ -22,9 +22,10 @@ func StartApiService() {
|
||||
_ = engine.SetTrustedProxies([]string{trustedProxyIP})
|
||||
}
|
||||
|
||||
engine.GET("/", HandleIp)
|
||||
engine.GET("/", HandleMyIp)
|
||||
engine.GET("/json", HandleJson)
|
||||
engine.GET("/metrics", HandleMetrics)
|
||||
engine.GET("/:ip", HandleOtherIp)
|
||||
|
||||
port := os.Getenv(key.EnvApiPort)
|
||||
if len(port) == 0 {
|
||||
|
||||
Reference in New Issue
Block a user