package ipblws import ( "fmt" "net/http" "strconv" "git.paulbsd.com/paulbsd/ipbl/src/config" "git.paulbsd.com/paulbsd/ipbl/src/ipbl" "github.com/labstack/echo/v4" ) // RunServer runs the main echo HTTP server func RunServer(cfg *config.Config) (err error) { e := echo.New() e.HideBanner = cfg.Options.HideBanner e.GET("/", func(c echo.Context) error { return c.HTML(http.StatusOK, `

Welcome to ipbl software (https://git.paulbsd.com/paulbsd/ipbl)

`) }) e.GET("/ip/:ip", func(c echo.Context) (err error) { var ip = ipbl.IP{IP: c.Param("ip")} res, _ := cfg.Db.Get(&ip) if res { return c.JSON(http.StatusOK, ip) } return c.JSON(http.StatusNotFound, "IP not found") }) e.POST("/ip", func(c echo.Context) (err error) { var ip = new(ipbl.IP) err = c.Bind(ip) if err != nil { return c.JSON(http.StatusInternalServerError, "Error when parsing body") } num, err := ip.InsertIP(cfg) if err != nil { return c.JSON(http.StatusInternalServerError, "Error inserting data") } return c.JSON(http.StatusOK, fmt.Sprintf("Inserted %d IP", num)) }) e.GET("/ips", func(c echo.Context) (err error) { var ips = []ipbl.IP{} var limit int = 50 if c.QueryParam("limit") != "" { limit, err = strconv.Atoi(c.QueryParam("limit")) if err != nil { limit = 50 } } cfg.Db.Limit(limit).Desc("created").Find(&ips) return c.JSON(http.StatusOK, ips) }) e.POST("/ips", func(c echo.Context) (err error) { var ips = []ipbl.IP{} err = c.Bind(&ips) if err != nil { return c.JSON(http.StatusInternalServerError, "Error when parsing body") } numinsert, numfail, _ := ipbl.InsertIPBulk(cfg, &ips) return c.JSON(http.StatusOK, fmt.Sprintf("Inserted %d IP, %d errors", numinsert, numfail)) }) e.Logger.Fatal( e.Start( fmt.Sprintf(":%d", cfg.Switchs.Port))) return }