ipbl/src/routers/funcs.go

83 lines
2.0 KiB
Go
Raw Normal View History

2021-12-27 23:52:22 +01:00
package routers
2021-11-07 20:44:48 +01:00
import (
2021-12-27 23:52:22 +01:00
"context"
2021-11-07 20:44:48 +01:00
"fmt"
2021-12-12 19:02:35 +01:00
"log"
2021-11-07 20:44:48 +01:00
"net/http"
"strconv"
"git.paulbsd.com/paulbsd/ipbl/src/config"
2021-12-27 23:52:22 +01:00
"git.paulbsd.com/paulbsd/ipbl/src/models"
2021-11-07 20:44:48 +01:00
"github.com/labstack/echo/v4"
)
2021-12-27 23:52:22 +01:00
// RegisterRoutes runs the main echo HTTP server
func RegisterRoutes(e *echo.Echo, ctx *context.Context, cfg *config.Config) {
2021-11-07 20:44:48 +01:00
e.GET("/", func(c echo.Context) error {
2021-12-12 17:33:40 +01:00
return c.HTML(http.StatusOK, `<html>
<body style="background-color: black">
<p style="color:white">Welcome to ipbl software (https://git.paulbsd.com/paulbsd/ipbl)</p>
</body>
</html>`)
2021-11-07 20:44:48 +01:00
})
2021-12-27 23:52:22 +01:00
// IPs
2021-11-07 20:44:48 +01:00
e.GET("/ip/:ip", func(c echo.Context) (err error) {
2021-12-27 23:52:22 +01:00
ret, err := models.GetIp(ctx, cfg, c.Param("ip"))
return JSONResult(c, err, ret)
2021-11-07 20:44:48 +01:00
})
2021-12-11 12:59:43 +01:00
e.POST("/ip", func(c echo.Context) (err error) {
2021-12-27 23:52:22 +01:00
var ip = new(models.IP)
2021-12-29 14:18:27 +01:00
var msg string
2021-12-12 17:33:40 +01:00
err = c.Bind(ip)
if err != nil {
2021-12-27 23:52:22 +01:00
return JSONResult(c, fmt.Errorf("Error when parsing body"), "")
2021-12-12 17:33:40 +01:00
}
num, err := ip.InsertIP(cfg)
if err != nil {
2021-12-27 23:52:22 +01:00
return JSONResult(c, fmt.Errorf("Error inserting data"), "")
2021-12-12 17:33:40 +01:00
}
2021-12-29 14:18:27 +01:00
if num > 0 {
msg = fmt.Sprintf("Inserted %d IP", num)
}
2021-12-12 19:02:35 +01:00
return c.JSON(http.StatusOK, msg)
2021-12-11 12:59:43 +01:00
})
2021-11-07 20:44:48 +01:00
e.GET("/ips", func(c echo.Context) (err error) {
2021-12-27 23:52:22 +01:00
limit := 50
2021-11-07 20:44:48 +01:00
if c.QueryParam("limit") != "" {
2021-12-27 23:52:22 +01:00
limit, _ = strconv.Atoi(c.QueryParam("limit"))
2021-11-07 20:44:48 +01:00
}
2021-12-27 23:52:22 +01:00
ret, err := models.GetIps(ctx, cfg, limit)
return JSONResult(c, err, ret)
2021-11-07 20:44:48 +01:00
})
2021-12-11 12:59:43 +01:00
e.POST("/ips", func(c echo.Context) (err error) {
2021-12-27 23:52:22 +01:00
var ips = []models.IP{}
2021-12-29 14:18:27 +01:00
var msg string
2021-12-12 17:33:40 +01:00
err = c.Bind(&ips)
if err != nil {
return c.JSON(http.StatusInternalServerError, "Error when parsing body")
}
2021-12-29 14:18:27 +01:00
numinsert, _, _ := models.InsertIPBulk(cfg, &ips)
if numinsert > 0 {
msg = fmt.Sprintf("Inserted %d IP", numinsert)
}
2021-12-12 19:02:35 +01:00
log.Println(msg)
return c.JSON(http.StatusOK, msg)
2021-12-11 12:59:43 +01:00
})
2021-12-27 23:52:22 +01:00
e.GET("/ips/whitelist", func(c echo.Context) (err error) {
var whitelists = models.GetWhitelists(*cfg)
if len(whitelists) > 0 {
return c.JSON(http.StatusOK, whitelists)
}
return c.JSON(http.StatusInternalServerError, "")
})
2021-11-07 20:44:48 +01:00
e.Logger.Fatal(
e.Start(
fmt.Sprintf(":%d",
cfg.Switchs.Port)))
}