2021-12-27 23:52:22 +01:00
|
|
|
package routers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2022-02-12 00:33:58 +01:00
|
|
|
"strings"
|
2021-12-27 23:52:22 +01:00
|
|
|
|
|
|
|
"git.paulbsd.com/paulbsd/ipbl/src/config"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
func RunServer(ctx *context.Context, cfg *config.Config) (err error) {
|
|
|
|
e := echo.New()
|
|
|
|
e.HideBanner = true
|
|
|
|
|
|
|
|
RegisterRoutes(e, ctx, cfg)
|
|
|
|
|
|
|
|
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", cfg.Switchs.Port)))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-12 00:33:58 +01:00
|
|
|
func Result(c echo.Context, inputerr error, data interface{}) (err error) {
|
2021-12-27 23:52:22 +01:00
|
|
|
if inputerr != nil {
|
|
|
|
if inputerr.Error() == "Not Found" {
|
2022-02-12 00:33:58 +01:00
|
|
|
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")
|
2021-12-27 23:52:22 +01:00
|
|
|
}
|
|
|
|
if inputerr.Error() == "Error when parsing body" {
|
2022-02-12 00:33:58 +01:00
|
|
|
return c.String(http.StatusBadRequest, "Content not conform")
|
2021-12-27 23:52:22 +01:00
|
|
|
}
|
2022-02-12 00:33:58 +01:00
|
|
|
return c.String(http.StatusInternalServerError, inputerr.Error())
|
2021-12-27 23:52:22 +01:00
|
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ConfigAccess(cfg config.Config, ip string) (ret bool) {
|
|
|
|
switch ip {
|
|
|
|
case "127.0.0.1":
|
|
|
|
return true
|
|
|
|
case "::1":
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|