updated functions
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Paul 2022-02-12 00:33:58 +01:00
parent ac71103475
commit c759df10d0
4 changed files with 29 additions and 26 deletions

View File

@ -20,7 +20,7 @@ func GetWhitelists(cfg config.Config) (res []string, err error) {
return return
} }
func (wl Whitelist) Insert(cfg config.Config) (err error) { func (wl Whitelist) InsertOrUpdate(cfg config.Config) (err error) {
var w = Cfg{Key: "whitelist"} var w = Cfg{Key: "whitelist"}
exists, _ := cfg.Db.Get(&w) exists, _ := cfg.Db.Get(&w)
if exists { if exists {

View File

@ -68,7 +68,6 @@ func (i *IP) InsertIP(cfg *config.Config) (num int64, err error) {
// InsertIPBulk ... // InsertIPBulk ...
func InsertIPBulk(cfg *config.Config, ips *[]IP) (numinserts int64, numupdates int64, numfail int64, err error) { func InsertIPBulk(cfg *config.Config, ips *[]IP) (numinserts int64, numupdates int64, numfail int64, err error) {
var iplist []string var iplist []string
for _, ip := range *ips { for _, ip := range *ips {
iplist = append(iplist, ip.IP) iplist = append(iplist, ip.IP)

View File

@ -23,26 +23,26 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
</html>`) </html>`)
}) })
// IPs
e.GET("/ip/:ip", func(c echo.Context) (err error) { e.GET("/ip/:ip", func(c echo.Context) (err error) {
ret, err := models.GetIP(ctx, cfg, c.Param("ip")) 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) { e.POST("/ip", func(c echo.Context) (err error) {
var ip = new(models.IP) var ip = new(models.IP)
var msg string var msg string
err = c.Bind(ip) err = c.Bind(ip)
if err != nil { 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) num, err := ip.InsertIP(cfg)
fmt.Println(err)
if err != nil { if err != nil {
return JSONResult(c, fmt.Errorf("error inserting data"), "") return Result(c, err, "")
} }
if num > 0 { if num > 0 {
msg = fmt.Sprintf("Inserted %d IP", num) 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) { e.GET("/ips", func(c echo.Context) (err error) {
limit := 50 limit := 50
@ -50,25 +50,25 @@ func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
limit, _ = strconv.Atoi(c.QueryParam("limit")) limit, _ = strconv.Atoi(c.QueryParam("limit"))
} }
ret, err := models.GetIPs(ctx, cfg, limit) ret, err := models.GetIPs(ctx, cfg, limit)
return JSONResult(c, err, ret) return Result(c, err, ret)
}) })
// retro-compat // retro-compat
e.GET("/ips/lastday", func(c echo.Context) (err error) { e.GET("/ips/lastday", func(c echo.Context) (err error) {
interval := "1 day" interval := "1 day"
ret, err := models.GetIPsLast(ctx, cfg, interval) 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) { e.GET("/ips/last", func(c echo.Context) (err error) {
interval := "10 minutes" interval := "10 minutes"
ret, err := models.GetIPsLast(ctx, cfg, interval) 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) { e.POST("/ips", func(c echo.Context) (err error) {
var ips = []models.IP{} var ips = []models.IP{}
var msg string var msg string
err = c.Bind(&ips) err = c.Bind(&ips)
if err != nil { if err != nil {
return c.JSON(http.StatusInternalServerError, "Error when parsing body") return Result(c, err, ips)
} }
numinsert, numupdate, _, _ := models.InsertIPBulk(cfg, &ips) numinsert, numupdate, _, _ := models.InsertIPBulk(cfg, &ips)
if numinsert > 0 { 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) msg = fmt.Sprintf("Updated %d IP", numupdate)
log.Println(msg) log.Println(msg)
} }
return c.JSON(http.StatusOK, msg) return Result(c, err, msg)
}) })
e.GET("/ips/whitelist", func(c echo.Context) (err error) { e.GET("/ips/whitelist", func(c echo.Context) (err error) {
whitelists, _ := models.GetWhitelists(*cfg) whitelists, err := models.GetWhitelists(*cfg)
if len(whitelists) > 0 { return Result(c, err, whitelists)
return c.JSON(http.StatusOK, whitelists)
}
return c.JSON(http.StatusInternalServerError, "")
}) })
e.PUT("/ips/whitelist", func(c echo.Context) (err error) { e.POST("/ips/whitelist", func(c echo.Context) (err error) {
var cidr models.Whitelist var cidr models.Whitelist
err = c.Bind(&cidr) err = c.Bind(&cidr)
if err == nil && cidr.Verify() { if err == nil && cidr.Verify() {
cidr.Insert(*cfg) err = cidr.InsertOrUpdate(*cfg)
return c.JSON(http.StatusOK, "Inserted new CIDR") 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) { e.DELETE("/ips/whitelist/:ip", func(c echo.Context) (err error) {
var ip = c.Param("ip") var ip = c.Param("ip")

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"net/http" "net/http"
"strings"
"git.paulbsd.com/paulbsd/ipbl/src/config" "git.paulbsd.com/paulbsd/ipbl/src/config"
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
@ -20,16 +21,22 @@ func RunServer(ctx *context.Context, cfg *config.Config) (err error) {
return return
} }
// JSONResult handles returns and error management on backend api // Result handles returns and error management on backend api
func JSONResult(c echo.Context, inputerr error, data interface{}) (err error) { func Result(c echo.Context, inputerr error, data interface{}) (err error) {
if inputerr != nil { if inputerr != nil {
if inputerr.Error() == "Not Found" { 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" { 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) return c.JSON(http.StatusOK, data)
} }