ipbl/src/ipblws/server.go

58 lines
1.3 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")
})
2021-12-11 12:59:43 +01:00
e.POST("/ip", func(c echo.Context) (err error) {
return c.String(http.StatusNotImplemented, "")
})
2021-11-07 20:44:48 +01:00
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)
})
2021-12-11 12:59:43 +01:00
e.POST("/ips", func(c echo.Context) (err error) {
var ips = []ipbl.IP{}
return c.JSON(http.StatusOK, ips)
})
2021-11-07 20:44:48 +01:00
e.Logger.Fatal(
e.Start(
fmt.Sprintf(":%d",
cfg.Switchs.Port)))
return
}