Skip to content

Commit

Permalink
更换web框架为Gin
Browse files Browse the repository at this point in the history
  • Loading branch information
fcwys committed May 26, 2022
1 parent 60aa9ab commit 6100676
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 46 deletions.
51 changes: 31 additions & 20 deletions cmd/server.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package cmd

import (
"net/http"
"log"
"strings"

"github.com/labstack/echo/v4"
"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
"github.com/zu1k/nali/pkg/entity"
)
Expand Down Expand Up @@ -40,19 +40,23 @@ var serverCmd = &cobra.Command{
port, _ := cmd.Flags().GetString("port")
port = ":" + port

//启动echo web服务
ec := echo.New()
log.Println(">>> Nali Server running on *"+port)
//关闭调试模式
gin.SetMode(gin.ReleaseMode)
//启动Gin服务
ec := gin.Default()
//参数方式查询
ec.GET("/", func(c echo.Context) error {
ip := c.QueryParam("ip")
ec.GET("/", func(c *gin.Context) {
ip := c.Query("ip")
if ip == "help" {
reinfo := &Errinfo{
Code: -1,
Msg: "Param: ip=223.5.5.5 or /223.5.5.5 (Default: visitor ip)",
Msg: "Param: /223.5.5.5 or ip=223.5.5.5 (Default: visitor ip)",
}
return c.JSON(http.StatusOK, reinfo)
c.JSON(200, reinfo)
return
} else if ip == "" {
ip = c.RealIP()
ip = c.ClientIP()
}
//fmt.Println(ip)
args = nil
Expand All @@ -64,52 +68,59 @@ var serverCmd = &cobra.Command{
Code: -1,
Msg: "No record, please try again",
}
return c.JSON(http.StatusOK, reinfo)
c.JSON(200, reinfo)
return
} else {
reinfo := &Ipinfo{
Code: 1,
Ip: res[0].Text,
Addr: strings.Replace(res[0].Info, "\t", " ", -1),
}
return c.JSON(http.StatusOK, reinfo)
c.JSON(200, reinfo)
return
}

})
//路径方式查询
ec.GET("/:ip", func(c echo.Context) error {

//路径查询方式
//参数方式查询
ec.GET("/:ip", func(c *gin.Context) {
ip := c.Param("ip")
if ip == "help" {
reinfo := &Errinfo{
Code: -1,
Msg: "Help: ?ip=223.5.5.5 or /223.5.5.5 (Default: your ip)",
Msg: "Param: /223.5.5.5 or ip=223.5.5.5 (Default: visitor ip)",
}
return c.JSON(http.StatusOK, reinfo)
c.JSON(200, reinfo)
return
} else if ip == "" {
ip = c.RealIP()
ip = c.ClientIP()
}
//fmt.Println(ip)
args = nil
args = append(args, ip)
res := entity.ParseLine(strings.Join(args, " "))
//addr := strings.Split(strings.Replace(res[0].Info, "\t", " ", -1), " ")
//判断是否有结果返回
if res[0].Info == "" {
reinfo := &Errinfo{
Code: -1,
Msg: "No record, please try again",
}
return c.JSON(http.StatusOK, reinfo)
c.JSON(200, reinfo)
return
} else {
reinfo := &Ipinfo{
Code: 1,
Ip: res[0].Text,
Addr: strings.Replace(res[0].Info, "\t", " ", -1),
}
return c.JSON(http.StatusOK, reinfo)
c.JSON(200, reinfo)
return
}

})
ec.Logger.Fatal(ec.Start(port))
//ec.Logger.Fatal(ec.Start(port))
ec.Run(port)
},
}

Expand Down
21 changes: 14 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ go 1.18

require (
github.com/fatih/color v1.13.0
github.com/gin-gonic/gin v1.7.7
github.com/ip2location/ip2location-go/v9 v9.2.0
github.com/ipipdotnet/ipdb-go v1.3.1
github.com/labstack/echo/v4 v4.7.2
github.com/lionsoul2014/ip2region v2.2.0-release+incompatible
github.com/oschwald/geoip2-golang v1.7.0
github.com/saracen/go7z v0.0.0-20191010121135-9c09b6bd7fda
Expand All @@ -18,13 +18,21 @@ require (

require (
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.11.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/labstack/gommon v0.3.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/oschwald/maxminddb-golang v1.9.0 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/pelletier/go-toml/v2 v2.0.0-beta.8 // indirect
Expand All @@ -35,13 +43,12 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.2.0 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 // indirect
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/tools v0.1.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
Loading

0 comments on commit 6100676

Please sign in to comment.