This commit is contained in:
parent
ac71103475
commit
c759df10d0
@ -20,7 +20,7 @@ func GetWhitelists(cfg config.Config) (res []string, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (wl Whitelist) Insert(cfg config.Config) (err error) {
|
||||
func (wl Whitelist) InsertOrUpdate(cfg config.Config) (err error) {
|
||||
var w = Cfg{Key: "whitelist"}
|
||||
exists, _ := cfg.Db.Get(&w)
|
||||
if exists {
|
||||
|
@ -68,7 +68,6 @@ func (i *IP) InsertIP(cfg *config.Config) (num int64, err error) {
|
||||
|
||||
// InsertIPBulk ...
|
||||
func InsertIPBulk(cfg *config.Config, ips *[]IP) (numinserts int64, numupdates int64, numfail int64, err error) {
|
||||
|
||||
var iplist []string
|
||||
for _, ip := range *ips {
|
||||
iplist = append(iplist, ip.IP)
|
||||
|
@ -23,26 +23,26 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
|
||||
</html>`)
|
||||
})
|
||||
|
||||
// IPs
|
||||
e.GET("/ip/:ip", func(c echo.Context) (err error) {
|
||||
ret, err := models.GetIP(ctx, cfg, c.Param("ip"))
|
||||
return JSONResult(c, err, ret)
|
||||
return Result(c, err, ret)
|
||||
})
|
||||
e.POST("/ip", func(c echo.Context) (err error) {
|
||||
var ip = new(models.IP)
|
||||
var msg string
|
||||
err = c.Bind(ip)
|
||||
if err != nil {
|
||||
return JSONResult(c, fmt.Errorf("error when parsing body"), "")
|
||||
return Result(c, fmt.Errorf("error when parsing body"), "")
|
||||
}
|
||||
num, err := ip.InsertIP(cfg)
|
||||
fmt.Println(err)
|
||||
if err != nil {
|
||||
return JSONResult(c, fmt.Errorf("error inserting data"), "")
|
||||
return Result(c, err, "")
|
||||
}
|
||||
if num > 0 {
|
||||
msg = fmt.Sprintf("Inserted %d IP", num)
|
||||
}
|
||||
return c.JSON(http.StatusOK, msg)
|
||||
return Result(c, err, msg)
|
||||
})
|
||||
e.GET("/ips", func(c echo.Context) (err error) {
|
||||
limit := 50
|
||||
@ -50,25 +50,25 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
|
||||
limit, _ = strconv.Atoi(c.QueryParam("limit"))
|
||||
}
|
||||
ret, err := models.GetIPs(ctx, cfg, limit)
|
||||
return JSONResult(c, err, ret)
|
||||
return Result(c, err, ret)
|
||||
})
|
||||
// retro-compat
|
||||
e.GET("/ips/lastday", func(c echo.Context) (err error) {
|
||||
interval := "1 day"
|
||||
ret, err := models.GetIPsLast(ctx, cfg, interval)
|
||||
return JSONResult(c, err, ret)
|
||||
return Result(c, err, ret)
|
||||
})
|
||||
e.GET("/ips/last", func(c echo.Context) (err error) {
|
||||
interval := "10 minutes"
|
||||
ret, err := models.GetIPsLast(ctx, cfg, interval)
|
||||
return JSONResult(c, err, ret)
|
||||
return Result(c, err, ret)
|
||||
})
|
||||
e.POST("/ips", func(c echo.Context) (err error) {
|
||||
var ips = []models.IP{}
|
||||
var msg string
|
||||
err = c.Bind(&ips)
|
||||
if err != nil {
|
||||
return c.JSON(http.StatusInternalServerError, "Error when parsing body")
|
||||
return Result(c, err, ips)
|
||||
}
|
||||
numinsert, numupdate, _, _ := models.InsertIPBulk(cfg, &ips)
|
||||
if numinsert > 0 {
|
||||
@ -79,23 +79,20 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
|
||||
msg = fmt.Sprintf("Updated %d IP", numupdate)
|
||||
log.Println(msg)
|
||||
}
|
||||
return c.JSON(http.StatusOK, msg)
|
||||
return Result(c, err, msg)
|
||||
})
|
||||
e.GET("/ips/whitelist", func(c echo.Context) (err error) {
|
||||
whitelists, _ := models.GetWhitelists(*cfg)
|
||||
if len(whitelists) > 0 {
|
||||
return c.JSON(http.StatusOK, whitelists)
|
||||
}
|
||||
return c.JSON(http.StatusInternalServerError, "")
|
||||
whitelists, err := models.GetWhitelists(*cfg)
|
||||
return Result(c, err, whitelists)
|
||||
})
|
||||
e.PUT("/ips/whitelist", func(c echo.Context) (err error) {
|
||||
e.POST("/ips/whitelist", func(c echo.Context) (err error) {
|
||||
var cidr models.Whitelist
|
||||
err = c.Bind(&cidr)
|
||||
if err == nil && cidr.Verify() {
|
||||
cidr.Insert(*cfg)
|
||||
return c.JSON(http.StatusOK, "Inserted new CIDR")
|
||||
err = cidr.InsertOrUpdate(*cfg)
|
||||
return Result(c, err, cidr)
|
||||
}
|
||||
return c.JSON(http.StatusInternalServerError, "Invalid CIDR")
|
||||
return Result(c, err, nil)
|
||||
})
|
||||
e.DELETE("/ips/whitelist/:ip", func(c echo.Context) (err error) {
|
||||
var ip = c.Param("ip")
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.paulbsd.com/paulbsd/ipbl/src/config"
|
||||
"github.com/labstack/echo/v4"
|
||||
@ -20,16 +21,22 @@ func RunServer(ctx *context.Context, cfg *config.Config) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// JSONResult handles returns and error management on backend api
|
||||
func JSONResult(c echo.Context, inputerr error, data interface{}) (err error) {
|
||||
// Result handles returns and error management on backend api
|
||||
func Result(c echo.Context, inputerr error, data interface{}) (err error) {
|
||||
if inputerr != nil {
|
||||
if inputerr.Error() == "Not Found" {
|
||||
return c.JSON(http.StatusNotFound, inputerr.Error())
|
||||
return c.String(http.StatusNotFound, "Content not found")
|
||||
}
|
||||
if strings.Contains(inputerr.Error(), "duplicate key value violates unique constraint") {
|
||||
return c.String(http.StatusConflict, "Content already exists")
|
||||
}
|
||||
if strings.Contains(inputerr.Error(), "already exists") {
|
||||
return c.String(http.StatusConflict, "Content already exists")
|
||||
}
|
||||
if inputerr.Error() == "Error when parsing body" {
|
||||
return c.JSON(http.StatusBadRequest, inputerr.Error())
|
||||
return c.String(http.StatusBadRequest, "Content not conform")
|
||||
}
|
||||
return c.JSON(http.StatusInternalServerError, inputerr.Error())
|
||||
return c.String(http.StatusInternalServerError, inputerr.Error())
|
||||
}
|
||||
return c.JSON(http.StatusOK, data)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user