mux

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

Functions

func InfraEncoderOpt

func InfraEncoderOpt(middles ...any) transport.ServiceOption

func Wraps

func Wraps(h http.Handler, middles ...Middle) http.Handler

Types

type Config

type Config struct {
	Addr string `json:"addr" yaml:"addr"`
}

type InfraEncodeMiddle

type InfraEncodeMiddle func(w http.ResponseWriter)
var InfraCORS InfraEncodeMiddle = func(w http.ResponseWriter) {
	w.Header().Set("Access-Control-Allow-Origin", "*")
}

type InfraResponseEncoder

type InfraResponseEncoder struct {
	Middles []InfraEncodeMiddle
}

func (*InfraResponseEncoder) Encoder

type Middle

type Middle func(http.Handler) http.Handler
var CORS Middle = func(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if r.Method != http.MethodOptions {
			h.ServeHTTP(w, r)
			w.Header().Set(echo.HeaderVary, echo.HeaderOrigin)
			w.Header().Set(echo.HeaderAccessControlAllowOrigin, "*")
			return
		}

		w.Header().Set(echo.HeaderVary, echo.HeaderOrigin)
		w.Header().Add(echo.HeaderVary, echo.HeaderAccessControlRequestMethod)
		w.Header().Add(echo.HeaderVary, echo.HeaderAccessControlRequestHeaders)
		w.Header().Set(echo.HeaderAccessControlAllowOrigin, "*")
		w.Header().Set(echo.HeaderAccessControlAllowMethods, strings.Join(HTTPMethods, ","))
		if v := r.Header.Get(echo.HeaderAccessControlRequestHeaders); v != "" {
			w.Header().Set(echo.HeaderAccessControlAllowHeaders, v)
		}
	})
}
var SetXRequestId Middle = func(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if id := r.Header.Get("X-Request-Id"); id == "" {
			r.Header.Set("X-Request-Id", strings.ReplaceAll(uuid.NewString(), "-", ""))
		}
		h.ServeHTTP(w, r)
	})
}

func (Middle) MiddleFunc

func (middle Middle) MiddleFunc() func(http.HandlerFunc) http.HandlerFunc

type Mux

type Mux interface {
	Handle(path, method string, h http.Handler, middles ...Middle)
	HandlePrefix(prefix, method string, h http.Handler, middles ...Middle)
	HandleMatch(match func(r *http.Request) bool, h http.Handler, middles ...Middle)
}

Jump to

Keyboard shortcuts

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