mirror of
https://github.com/hazemKrimi/crimson-vault.git
synced 2026-05-02 02:30:28 +00:00
wip: custom logger and error handler
This commit is contained in:
@@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/gorilla/sessions"
|
||||
@@ -11,12 +12,14 @@ import (
|
||||
|
||||
"github.com/hazemKrimi/crimson-vault/internal/lib"
|
||||
"github.com/hazemKrimi/crimson-vault/internal/models"
|
||||
"github.com/hazemKrimi/crimson-vault/internal/types"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
ConfigDirectory string
|
||||
instance *echo.Echo
|
||||
db *models.DB
|
||||
Logger *slog.Logger
|
||||
}
|
||||
|
||||
func (api *API) Initialize() {
|
||||
@@ -26,6 +29,19 @@ func (api *API) Initialize() {
|
||||
db := &models.DB{}
|
||||
ech := echo.New()
|
||||
ech.Validator = &CustomValidator{validator: validator}
|
||||
ech.HTTPErrorHandler = func(err error, context echo.Context) {
|
||||
if context.Response().Committed {
|
||||
return
|
||||
}
|
||||
|
||||
custom, ok := err.(types.Error)
|
||||
|
||||
if ok {
|
||||
context.JSONPretty(custom.Code, map[string][]string{"errors": custom.Messages}, " ")
|
||||
}
|
||||
|
||||
ech.DefaultHTTPErrorHandler(err, context)
|
||||
}
|
||||
|
||||
db.Connect(api.ConfigDirectory)
|
||||
db.MigrateClients()
|
||||
@@ -34,6 +50,7 @@ func (api *API) Initialize() {
|
||||
api.instance = ech
|
||||
api.db = db
|
||||
|
||||
api.instance.Use(api.LoggerMiddleware())
|
||||
// TODO: Update with appropriate origins when finishing v1
|
||||
api.instance.Use(middleware.CORSWithConfig(middleware.CORSConfig{
|
||||
AllowOrigins: []string{"*"},
|
||||
|
||||
Reference in New Issue
Block a user