mixed

package module
v1.2.4 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2023 License: MIT Imports: 2 Imported by: 0

README

go-echo-mixed-middleware

Test

A composition middleware for Echo that will succeed if either of the middleware succeeds.

Guide

Installation
go get github.com/kumojin/go-echo-mixed-middleware
Example
package main

import (
  "errors"
  "net/http"
  "github.com/labstack/echo/v4"
  "github.com/labstack/echo/v4/middleware"
  mixed "github.com/kumojin/go-echo-mixed-middleware"
)

func main() {
  // Echo instance
  e := echo.New()

  // Middleware
  e.Use(middleware.Logger())
  e.Use(middleware.Recover())

  // Routes
  e.GET("/", hello, mixed.Mixed(failingMiddleware, succeedingMiddleware))

  // Start server
  e.Logger.Fatal(e.Start(":1323"))
}

// Handler
func hello(c echo.Context) error {
  return c.String(http.StatusOK, "Hello, World!")
}

func succeedingMiddleware() echo.MiddlewareFunc {
	return func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) error {
			return nil
		}
	}
}

func failingMiddleware() echo.MiddlewareFunc {
	return func(next echo.HandlerFunc) echo.HandlerFunc {
		return func(c echo.Context) error {
            ctx.String(http.StatusUnauthorized, "Unauthorized")
			return errors.New("Unauthorized")
		}
	}
}

Contribute

Use issues for everything

  • For a small change, just send a PR.
  • For bigger changes, please open an issue for discussion before sending a PR.
  • PR should have:
    • Test case
    • Documentation
    • Example (If it makes sense)
  • You can also contribute by:
    • Reporting issues
    • Suggesting new features or enhancements
    • Improve/fix documentation

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Mixed

func Mixed(preserveKeys []string) func(handlers ...echo.MiddlewareFunc) echo.MiddlewareFunc

Mixed returns a single middleware function composed of the middlewares. Upon being called, it is internally going to call the first middleware, and if there is no error, return immediately with the answer.

Otherwise, it is going to call the next middlewares, and return their response.

Types

This section is empty.

Jump to

Keyboard shortcuts

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