Skip to content

Commit 9ef0a40

Browse files
committed
move rest of echo setup into handlers setup
1 parent 5f85097 commit 9ef0a40

File tree

2 files changed

+35
-41
lines changed

2 files changed

+35
-41
lines changed

backend/cli/serve.go

+4-37
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,12 @@
11
package cli
22

33
import (
4-
"errors"
54
"github.com/enchant97/note-mark/backend/config"
6-
"github.com/enchant97/note-mark/backend/core"
75
"github.com/enchant97/note-mark/backend/db"
86
"github.com/enchant97/note-mark/backend/handlers"
97
"github.com/enchant97/note-mark/backend/storage"
10-
"github.com/labstack/echo/v4"
11-
"github.com/labstack/echo/v4/middleware"
12-
"gorm.io/gorm"
13-
"net/http"
148
)
159

16-
// HTTP error handler, to handle unexpected errors
17-
func httpErrorHandler(err error, ctx echo.Context) {
18-
if e, ok := err.(*echo.HTTPError); ok {
19-
// normal HTTP error
20-
ctx.JSON(e.Code, e.Message)
21-
return
22-
}
23-
ctx.Logger().Error(err)
24-
if errors.Is(err, gorm.ErrRecordNotFound) {
25-
ctx.NoContent(http.StatusNotFound)
26-
} else if errors.Is(err, gorm.ErrDuplicatedKey) {
27-
ctx.NoContent(http.StatusConflict)
28-
} else if errors.Is(err, core.ErrBind) || errors.Is(err, core.ErrValidation) {
29-
ctx.NoContent(http.StatusUnprocessableEntity)
30-
} else {
31-
ctx.NoContent(http.StatusInternalServerError)
32-
}
33-
}
34-
3510
func commandServe(appConfig config.AppConfig) error {
3611
// Connect to storage backend
3712
storage_backend := storage.DiskController{}.New(appConfig.DataPath)
@@ -43,18 +18,10 @@ func commandServe(appConfig config.AppConfig) error {
4318
if err := db.InitDB(appConfig.DB); err != nil {
4419
return err
4520
}
46-
// Create server
47-
e := echo.New()
48-
e.HTTPErrorHandler = httpErrorHandler
49-
// Register root middleware
50-
e.Use(middleware.Recover())
51-
e.Use(middleware.Logger())
52-
v := core.Validator{}.New()
53-
e.Validator = &v
54-
// Init routes
55-
if err := handlers.SetupHandlers(e, appConfig, storage_backend); err != nil {
21+
if e, err := handlers.SetupHandlers(appConfig, storage_backend); err != nil {
5622
return err
23+
} else {
24+
// Start server
25+
return e.Start(appConfig.Bind.AsAddress())
5726
}
58-
// Start server
59-
return e.Start(appConfig.Bind.AsAddress())
6027
}

backend/handlers/utils.go

+31-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
echojwt "github.com/labstack/echo-jwt/v4"
1515
"github.com/labstack/echo/v4"
1616
"github.com/labstack/echo/v4/middleware"
17+
"gorm.io/gorm"
1718
)
1819

1920
const (
@@ -94,10 +95,36 @@ func getAuthDetails(ctx echo.Context) *core.AuthenticationDetails {
9495
return ctx.Get(AuthDetailsKey).(*core.AuthenticationDetails)
9596
}
9697

98+
// HTTP error handler, to handle unexpected errors
99+
func httpErrorHandler(err error, ctx echo.Context) {
100+
if e, ok := err.(*echo.HTTPError); ok {
101+
// normal HTTP error
102+
ctx.JSON(e.Code, e.Message)
103+
return
104+
}
105+
ctx.Logger().Error(err)
106+
if errors.Is(err, gorm.ErrRecordNotFound) {
107+
ctx.NoContent(http.StatusNotFound)
108+
} else if errors.Is(err, gorm.ErrDuplicatedKey) {
109+
ctx.NoContent(http.StatusConflict)
110+
} else if errors.Is(err, core.ErrBind) || errors.Is(err, core.ErrValidation) {
111+
ctx.NoContent(http.StatusUnprocessableEntity)
112+
} else {
113+
ctx.NoContent(http.StatusInternalServerError)
114+
}
115+
}
116+
97117
func SetupHandlers(
98-
e *echo.Echo,
99118
appConfig config.AppConfig,
100-
storage_backend storage.StorageController) error {
119+
storage_backend storage.StorageController) (*echo.Echo, error) {
120+
// Create server
121+
e := echo.New()
122+
e.HTTPErrorHandler = httpErrorHandler
123+
// Register root middleware
124+
e.Use(middleware.Recover())
125+
e.Use(middleware.Logger())
126+
v := core.Validator{}.New()
127+
e.Validator = &v
101128
corsConfig := middleware.DefaultCORSConfig
102129
{
103130
corsConfig.AllowOrigins = appConfig.CORSOrigins
@@ -112,7 +139,7 @@ func SetupHandlers(
112139
)
113140
if len(appConfig.StaticPath) != 0 {
114141
if _, err := os.Stat(appConfig.StaticPath); errors.Is(err, os.ErrNotExist) {
115-
return err
142+
return nil, err
116143
}
117144
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
118145
Root: appConfig.StaticPath,
@@ -134,5 +161,5 @@ func SetupHandlers(
134161
SetupBooksHandler(apiRoutes)
135162
SetupNotesHandler(apiRoutes, appConfig, storage_backend)
136163
SetupAssetsHandler(apiRoutes, appConfig, storage_backend)
137-
return nil
164+
return e, nil
138165
}

0 commit comments

Comments
 (0)