maryread

package module
v0.0.20 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2023 License: GPL-3.0 Imports: 7 Imported by: 8

README

Mary Read

Provides a echo router wrapper with extra functionality like database initialization and util working middleware to be shared between microservices.

Also helps to fight global warming acting as Mary Read.

App

Default

Provides an app with default tools:

    import "github.com/orov-io/maryread
    // ...

    app := maryread.Default()
    // You can access to the new echo router by app.Router()
    // app.Router().Use()
    // app.Router().GET()...
    
Custom App

Returns an app with tools configured via options: Usage:

    import "github.com/orov-io/maryread
    // ...

    e := echo.New()
    // e.GET()....
    options := maryread.AppOptions{
        Router: RouterOptions{
            Router: router
        }
    }
    

Available Middleware

    import "github.com/orov-io/maryread/middleware"
Body Dump

Adds the default Bodydump echo middleware for request with the header X-Bodydump not empty. Usage:

e := echo.New()
e.Use(DefaultLogger(zerolog.DebugLevel))
e.Use(BodyDumpOnHeader())

It relies in the logger echo-midlleware (see below) After this, all request with the X-Bodydump will use the zero logger functionality in this package to log both request & response bodies.

Logger

Usage:

e := echo.New()
e.Use(middleware.JWTWithConfig(middleware.JWTConfig{
    SigningKey: []byte(testJWTSecret),
}))
e.Use(DefaultLogger(zerolog.DebugLevel))

Adds the zero logger to the echo context. It generates a child that:

  • Adds the requestID if the requestID (the one in this package or the one prodived by echo) as a string param.
  • Adds the userID if you uses the JWT echo middleware a a string params. At now, it spects the ID to be in the ID claim.
  • Uses a pretty zerolog parser.
  • Changes the log level for a single request if you send the X-Loglevel header set to a valid zerolog level value (trace, debug, info ...)
RequestID

Usage:

e := echo.New()
e.Use(middleware.RequestID())
// Inside a handler or middleware...
RequestID(c) # It returns the generated requestID

Returns the generated requestID if you use the echo requestID middleware

Request Logger

Deprecated. Use echo.middleware.Logger() Instead.

TODO list

[] Migrate current middleware to fit the echo provided middleware (a default initializer and an initializer with options... perhaps other initializer with default config overrides by env vars...) [] Decouple auth middleware from firebase. Use the echo middleware with custom (parse) functions. [] Use the echo.NewHTTPError to return errors that echo will understand, so return c.JSON(...) will not be used and code become clearest. [] Add auth and sqlx middleware to readme. Remember to say to people must import desired driver in the file that they use to load the middleare, Examples:

  • _ "github.com/mattn/go-sqlite3"
  • _ "github.com/lib/pq"
  • or another driver supported by sql (and goose if you want automigrations)

[] Add Must<shortcut> to app, in order to panic if can obtain required object, as in dbx. [] Add test to middleware shortcuts. [] WARNING: If you want´t to use the datadog/sqlmock db mock in your test, please, deactivate the automigration feature or use the in memory sqlite3 driver (see the automigration test). We couldn't infer the Execs and transactions that goose made to the database in an Up command, so it will panic with no acction expecteds. [] Test if the default injected logger by the default echo logger middleware generates childs or insulate loggers in each context. If true, se the inner echo logger provided by the logger middleware instead of zero logger, accepting another logger that fit the error interface. Do this will result in delete the requestID Middleware. If not, check https://github.com/labstack/echo/issues/2310 to come back to use the custom request logger by default. Also, force the auth middleware to set a header in the request (X-LoggedUserID) and use the header string template mechanism in the logger to expose it.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bindlidate added in v0.0.19

func Bindlidate[T any](c echo.Context, data *T) error

Bindlidate uses provided context to both Bind and Validate data.

func GetDBX added in v0.0.14

func GetDBX(c echo.Context) (*sqlx.DB, error)

GetDBX is a shortcut to middleware.GetDBX()

func GetIDToken added in v0.0.10

func GetIDToken(c echo.Context) (*auth.Token, error)

GetIDToken is a shortcut to middleware.GetIDToken()

func LoggedUserIs added in v0.0.13

func LoggedUserIs(c echo.Context, rol string) bool

LoggedUserIs is a shortcut to middleware.LoggedUserIs()

func LoggedUserIsAny added in v0.0.13

func LoggedUserIsAny(c echo.Context, roles []string) bool

LoggedUserIsAny is a shortcut to middleware.LoggedUserIsAny()

func MustGetDBX added in v0.0.14

func MustGetDBX(c echo.Context) *sqlx.DB

MustGetDBX is like GetDBX but panics on error

func RequestID

func RequestID(c echo.Context) string

RequestID is a shortcut to middleware.RequestID()

Types

type App

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

func Default

func Default() *App

Returns a new app with default values.

func New

func New(options AppOptions) *App

NewApp generates a new app with tools expecified in provided options.

func (*App) Router

func (app *App) Router() *echo.Echo

Router returns the inner echo router.

type AppOptions

type AppOptions struct {
	Router RouterOptions
}

AppOptions models the app tools.

type RouterOptions

type RouterOptions struct {
	Router    *echo.Echo
	Validator echo.Validator
}

RouterOptions model the echo router options. If provided, app will use the expecified echo router.

type Validator added in v0.0.17

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

Validator is the default validator. It uses the go-playground validator and wraps the error in a echo.HTTPError with a status 400 error. For more control over the status code, please use the ValidatorRawError

func NewValidator added in v0.0.17

func NewValidator() *Validator

func (*Validator) Validate added in v0.0.17

func (v *Validator) Validate(i interface{}) error

type ValidatorRawError added in v0.0.17

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

ValidatorRawError uses the go-playground validator and return the raw error directly. No handling this error will become in a InternalServerError (500) if validation fails and you return the error to echo.

func NewValidatorRawError added in v0.0.17

func NewValidatorRawError() *ValidatorRawError

func (*ValidatorRawError) Validate added in v0.0.17

func (v *ValidatorRawError) Validate(i interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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