-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve indexer rest api error code and supports to query file info i…
…n batch (#79) * Extract common code for rest api * refactor local gateway * refine code for api framework * simplify indexer gateway server code * rename file * Add error code for rest api * refactor errors * refine code * Supports to get file info from indexer in batch * update readme * limit batch size to get file info
- Loading branch information
Showing
13 changed files
with
307 additions
and
212 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/go-playground/validator/v10" | ||
) | ||
|
||
const httpStatusCodeInternalError = 600 | ||
|
||
var ErrHandled = new(BusinessError) | ||
|
||
func Wrap(controller func(c *gin.Context) (interface{}, error)) gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
result, err := controller(c) | ||
if err == ErrHandled { | ||
return | ||
} | ||
|
||
if err != nil { | ||
switch e := err.(type) { | ||
case *BusinessError: | ||
// custom business error | ||
if e != ErrHandled { | ||
c.JSON(http.StatusOK, e) | ||
} | ||
case validator.ValidationErrors: | ||
// binding error | ||
c.JSON(http.StatusOK, ErrValidation.WithData(e)) | ||
default: | ||
// internal server error | ||
c.JSON(httpStatusCodeInternalError, ErrInternal.WithData(e)) | ||
} | ||
} else if result == nil { | ||
c.JSON(http.StatusOK, ErrNil) | ||
} else { | ||
c.JSON(http.StatusOK, ErrNil.WithData(result)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package api | ||
|
||
// General errors | ||
var ( | ||
ErrNil = NewBusinessError(0, "Success") | ||
ErrValidation = NewBusinessError(1, "Invalid parameter") | ||
ErrInternal = NewBusinessError(2, "Internal server error") | ||
) | ||
|
||
type BusinessError struct { | ||
Code int `json:"code"` | ||
Message string `json:"message"` | ||
Data interface{} `json:"data"` | ||
} | ||
|
||
func NewBusinessError(code int, message string) *BusinessError { | ||
return &BusinessError{code, message, nil} | ||
} | ||
|
||
func NewBusinessErrorWithData(code int, message string, data interface{}) *BusinessError { | ||
return &BusinessError{code, message, data} | ||
} | ||
|
||
func (err *BusinessError) Error() string { | ||
return err.Message | ||
} | ||
|
||
func (be *BusinessError) WithData(data interface{}) *BusinessError { | ||
return NewBusinessErrorWithData(be.Code, be.Message, data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-contrib/cors" | ||
"github.com/gin-gonic/gin" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type RouteFactory func(router *gin.Engine) | ||
|
||
type RouterOption struct { | ||
RecoveryDisabled bool | ||
LoggerForced bool | ||
OriginsAllowed []string | ||
} | ||
|
||
func MustServe(endpoint string, factory RouteFactory, option ...RouterOption) { | ||
if err := Serve(endpoint, factory, option...); err != http.ErrServerClosed { | ||
logrus.WithError(err).Fatal("Failed to serve API") | ||
} | ||
} | ||
|
||
func Serve(endpoint string, factory RouteFactory, option ...RouterOption) error { | ||
router := newRouter(factory, option...) | ||
|
||
server := http.Server{ | ||
Addr: endpoint, | ||
Handler: router, | ||
} | ||
|
||
return server.ListenAndServe() | ||
} | ||
|
||
func newRouter(factory RouteFactory, option ...RouterOption) *gin.Engine { | ||
var opt RouterOption | ||
if len(option) > 0 { | ||
opt = option[0] | ||
} | ||
|
||
router := gin.New() | ||
|
||
if !opt.RecoveryDisabled { | ||
router.Use(gin.Recovery()) | ||
} | ||
|
||
router.Use(newCorsMiddleware(opt.OriginsAllowed)) | ||
|
||
if opt.LoggerForced || logrus.IsLevelEnabled(logrus.DebugLevel) { | ||
router.Use(gin.Logger()) | ||
} | ||
|
||
factory(router) | ||
|
||
return router | ||
} | ||
|
||
func newCorsMiddleware(origins []string) gin.HandlerFunc { | ||
conf := cors.DefaultConfig() | ||
conf.AllowMethods = append(conf.AllowMethods, "OPTIONS") | ||
conf.AllowHeaders = append(conf.AllowHeaders, "*") | ||
|
||
if len(origins) == 0 { | ||
conf.AllowAllOrigins = true | ||
} else { | ||
conf.AllowOrigins = origins | ||
} | ||
|
||
return cors.New(conf) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.