http

package
v0.7.12 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2024 License: MIT Imports: 56 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrorMiddleware = func(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		ctx := context.WithValue(r.Context(), "error-handler", func(err error) {
			slog.Error(err.Error(), "request_id", r.Header.Get("X-Request-Id"))

			httputil.HandleError(w, httputil.NewError("", err, http.StatusInternalServerError))
		})

		next.ServeHTTP(w, r.WithContext(ctx))
	})
}
View Source
var PostMiddleware = func(_ http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		w.WriteHeader(http.StatusNoContent)
		_, _ = w.Write(nil)
	})
}
View Source
var PreMiddleware = func(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		turna := &tcontext.Turna{
			Vars: make(map[string]interface{}),
		}

		ctx := context.WithValue(r.Context(), tcontext.TurnaKey, turna)
		r = r.WithContext(ctx)

		echoContext := echo.New().NewContext(r, w)
		echoContext.Set("turna", turna)
		turna.EchoContext = echoContext

		next.ServeHTTP(w, r)
	})
}
View Source
var ReadHeaderTimeout = 10 * time.Second
View Source
var RecoverMiddleware = func(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		defer func() {
			if r := recover(); r != nil {
				if r == http.ErrAbortHandler {
					panic(r)
				}
				err, ok := r.(error)
				if !ok {
					err = fmt.Errorf("%v", r)
				}

				slog.Error(fmt.Sprintf("panic: %s", err.Error()))
				debug.PrintStack()

				w.WriteHeader(http.StatusInternalServerError)
				_, _ = w.Write([]byte(fmt.Sprintf("panic: %s", err.Error())))

				return
			}
		}()

		next.ServeHTTP(w, r)
	})
}
View Source
var ServerInfo = "turna"
View Source
var ServerInfoMiddleware = func(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Server", ServerInfo)

		next.ServeHTTP(w, r)
	})
}

Functions

This section is empty.

Types

type Certificate

type Certificate struct {
	CertFile string `cfg:"cert_file"`
	KeyFile  string `cfg:"key_file"`
}

type HTTP

type HTTP struct {
	Routers     map[string]Router         `cfg:"routers"`
	Middlewares map[string]HTTPMiddleware `cfg:"middlewares"`
	TLS         TLS                       `cfg:"tls"`
}

func (*HTTP) Set

func (h *HTTP) Set(ctx context.Context, wg *sync.WaitGroup) error

type HTTPMiddleware

type HTTPMiddleware struct {
	AddPrefixMiddleware        *addprefix.AddPrefix                  `cfg:"add_prefix"`
	AuthMiddleware             *auth.Auth                            `cfg:"auth"`
	InjectMiddleware           *inject.Inject                        `cfg:"inject"`
	HelloMiddleware            *hello.Hello                          `cfg:"hello"`
	TemplateMiddleware         *template.Template                    `cfg:"template"`
	InfoMiddleware             *info.Info                            `cfg:"info"`
	SetMiddleware              *set.Set                              `cfg:"set"`
	StripPrefixMiddleware      *stripprefix.StripPrefix              `cfg:"strip_prefix"`
	RoleMiddleware             *role.Role                            `cfg:"role"`
	ScopeMiddleware            *scope.Scope                          `cfg:"scope"`
	ServiceMiddleware          *service.Service                      `cfg:"service"`
	FolderMiddleware           *folder.Folder                        `cfg:"folder"`
	BasicAuthMiddleware        *basicauth.BasicAuth                  `cfg:"basic_auth"`
	CorsMiddleware             *cors.Cors                            `cfg:"cors"`
	HeadersMiddleware          *headers.Headers                      `cfg:"headers"`
	BlockMiddleware            *block.Block                          `cfg:"block"`
	RegexPathMiddleware        *regexpath.RegexPath                  `cfg:"regex_path"`
	GzipMiddleware             *gzip.Gzip                            `cfg:"gzip"`
	DecompressMiddleware       *decompress.Decompress                `cfg:"decompress"`
	LogMiddleware              *log.Log                              `cfg:"log"`
	PrintMiddleware            *print.Print                          `cfg:"print"`
	LoginMiddleware            *login.Login                          `cfg:"login"`
	SessionMiddleware          *session.Session                      `cfg:"session"`
	ViewMiddleware             *view.View                            `cfg:"view"`
	RequestMiddleware          *request.Request                      `cfg:"request"`
	RedirectionMiddleware      *redirection.Redirection              `cfg:"redirection"`
	TryMiddleware              *try.Try                              `cfg:"try"`
	SessionInfoMiddleware      *sessioninfo.Info                     `cfg:"session_info"`
	IamMiddleware              *iam.Iam                              `cfg:"iam"`
	IamCheckMiddleware         *iamcheck.IamCheck                    `cfg:"iam_check"`
	RoleCheckMiddleware        *rolecheck.RoleCheck                  `cfg:"role_check"`
	RoleDataMiddleware         *roledata.RoleData                    `cfg:"role_data"`
	TokenPassMiddleware        *tokenpass.TokenPass                  `cfg:"token_pass"`
	RedirectContinueMiddleware *redirectcontinue.RedirectionContinue `cfg:"redirect_continue"`
	ForwardMiddleware          *forward.Forward                      `cfg:"forward"`
	GrpcUIMiddleware           *grpcui.GrpcUI                        `cfg:"grpcui"`
	DNSPathMiddleware          *dnspath.DNSPath                      `cfg:"dns_path"`
	SplitterMiddleware         *splitter.Splitter                    `cfg:"splitter"`
	PathMiddleware             *path.Path                            `cfg:"path"`
	RequestIDMiddleware        *requestid.RequestID                  `cfg:"request_id"`
}

func (*HTTPMiddleware) Set

func (h *HTTPMiddleware) Set(ctx context.Context, name string) error

type MiddlewareFunc added in v0.7.0

type MiddlewareFunc = func(http.Handler) http.Handler

type PreMiddlewares added in v0.7.8

type PreMiddlewares struct {
	RequestID  *bool `cfg:"request_id"`  // default is true
	ServerInfo *bool `cfg:"server_info"` // default is true
}

type Router

type Router struct {
	Host        string    `cfg:"host"`
	Path        []string  `cfg:"path"`
	Middlewares []string  `cfg:"middlewares"`
	TLS         *struct{} `cfg:"tls"`
	EntryPoints []string  `cfg:"entrypoints"`

	PreMiddlewares PreMiddlewares `cfg:"pre_middlewares"`
}

func (*Router) Set

func (r *Router) Set(_ string, ruleRouter *RuleRouter) error

type RouterHandler added in v0.7.1

type RouterHandler interface {
	Handle(pattern string, handler http.Handler)
	ServeHTTP(w http.ResponseWriter, r *http.Request)
}

type RuleRouter

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

func NewRuleRouter

func NewRuleRouter() *RuleRouter

func (*RuleRouter) GetMux added in v0.7.0

func (s *RuleRouter) GetMux(r RuleSelection) RouterHandler

func (RuleRouter) Serve

func (s RuleRouter) Serve(entrypoint string) http.Handler

Serve implements the http.Handler interface with changing entrypoint selection.

func (*RuleRouter) ServeHTTP

func (s *RuleRouter) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*RuleRouter) SetRule

func (s *RuleRouter) SetRule(selection RuleSelection)

type RuleSelection

type RuleSelection struct {
	Host       string
	Entrypoint string
}

type TLS

type TLS struct {
	Store map[string][]Certificate `cfg:"store"`
}

Jump to

Keyboard shortcuts

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