ipbl/src/ipblws/server.go

51 lines
1.1 KiB
Go
Raw Normal View History

2021-11-07 20:44:48 +01:00
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.String(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.String(http.StatusNotFound, "IP not found")
})
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.Logger.Fatal(
e.Start(
fmt.Sprintf(":%d",
cfg.Switchs.Port)))
return
}