rest

package module
v0.0.0-...-3a8eb6e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 25, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Server     *echo.Echo // Global Echo server instance
	RestConfig *Config    // Configuration for REST server
	Logger     *zap.Logger
)

Functions

func CustomHTTPErrorHandler

func CustomHTTPErrorHandler(err error, c echo.Context)

func GenerateJwtToken

func GenerateJwtToken(userID any, app string) (string, error)

GenerateJwtToken creates a JWT token for any type of userID (int, string, etc).

func HTTPLogger

func HTTPLogger() echo.MiddlewareFunc

HTTPLogger returns a middleware that logs HTTP requests.

func IDFromContext

func IDFromContext(c echo.Context) int64

IDFromContext extracts an integer ID from the Echo context params.

func NewActionError

func NewActionError(msg string, e error) *echo.HTTPError

func NewServer

func NewServer(config *Config, logger *zap.Logger, registerRoutes func(e *echo.Echo))

NewServer initializes a new Echo server with middleware and routes.

func ParseUserIDFromToken

func ParseUserIDFromToken(c echo.Context) (any, error)

ParseUserIDFromToken extracts the user_id from JWT claims.

func RequestFailureDumper

func RequestFailureDumper(c echo.Context, reqBody, resBody []byte)

RequestFailureDumper logs failed HTTP requests and their payloads for easier debugging. It is triggered only when the response status is not 200 OK.

func Response

func Response(c echo.Context, rb interface{}, e error) error

func Restricted

func Restricted() echo.MiddlewareFunc

Restricted returns JWT middleware for Echo framework.

func Shutdown

func Shutdown(err error)

Shutdown gracefully shuts down the server.

func Start

func Start() error

Start runs the Echo server.

Types

type Config

type Config struct {
	ServiceName    string
	ServiceVersion string
	Address        string
	IsDev          bool
	AppName        string
	JwtSecret      string
}

Config holds configuration settings for the REST server.

type CustomBinder

type CustomBinder struct {
	// contains filtered or unexported fields
}

func (*CustomBinder) Bind

func (cb *CustomBinder) Bind(i interface{}, c echo.Context) (err error)

func (*CustomBinder) Validate

func (cb *CustomBinder) Validate(obj interface{}) (resp *validate.Response)

Validate the request when binding

type JwtCustomClaims

type JwtCustomClaims struct {
	UserID any    `json:"user_id"`
	App    string `json:"app"`
	jwt.RegisteredClaims
}

JwtCustomClaims defines the JWT payload structure.

type ResponseBody

type ResponseBody struct {
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
	Total   int64       `json:"total,omitempty"`
}

func NewResponseBody

func NewResponseBody() *ResponseBody

func (*ResponseBody) Body

func (r *ResponseBody) Body(data interface{}, total int64)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL