ipbl/src/routers/main.go

48 lines
1.1 KiB
Go
Raw Normal View History

2021-12-27 23:52:22 +01:00
package routers
import (
"context"
"fmt"
"net/http"
"git.paulbsd.com/paulbsd/ipbl/src/config"
"github.com/labstack/echo/v4"
)
// RunServer runs the main echo server
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
}
// JSONResult handles returns and error management on backend api
func JSONResult(c echo.Context, inputerr error, data interface{}) (err error) {
if inputerr != nil {
if inputerr.Error() == "Not Found" {
return c.JSON(http.StatusNotFound, inputerr.Error())
}
if inputerr.Error() == "Error when parsing body" {
return c.JSON(http.StatusBadRequest, inputerr.Error())
}
return c.JSON(http.StatusInternalServerError, inputerr.Error())
}
return c.JSON(http.StatusOK, data)
}
// ConfigAccess make ip authorization to configuration
func ConfigAccess(cfg config.Config, ip string) (ret bool) {
switch ip {
case "127.0.0.1":
return true
case "::1":
return true
default:
return
}
}