maryread

package module
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2022 License: GPL-3.0 Imports: 6 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

Usage

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

It relies in the provided logger functionality, like the body dump middleware. Use the zerologger and the echo requestLogger middleware to log request info.

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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 GetLogger

func GetLogger(c echo.Context) zerolog.Logger

GetLogger is a shortcut to middleware.GetLogger()

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
}

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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