lifecycle

package
v0.0.24 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2026 License: MIT Imports: 2 Imported by: 0

README

Lifecycle Middleware

What it does

Provides hooks around request start, before handler, after handler, error handling, and request end. It centralizes cross-cutting lifecycle behavior.

How to implement

package main

import (
	"github.com/oarkflow/fh"
	"github.com/oarkflow/fh/mw/lifecycle"
)

func main() {
	app := fh.New()
	app.Use(lifecycle.New(lifecycle.Hooks{}))

	app.Get("/", func(c fh.Ctx) error {
		return c.Status(fh.StatusOK).SendString("ok")
	})
}

Impact

Hook overhead depends on configured callbacks. Useful for instrumentation, cleanup, and compensation.

Ordering guidance

Run near the outside of the middleware chain so it can observe most behavior.

Production considerations

Hooks must be fast and safe. Do not panic in hooks. Avoid blocking external calls in lifecycle callbacks.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(h Hooks) fh.HandlerFunc

func NewWithConfig

func NewWithConfig(cfg Config) fh.HandlerFunc

Types

type Config

type Config struct {
	Hooks Hooks

	// RunAfterOnError controls whether OnAfterHandler runs after c.Next()
	// even when c.Next() returned an error.
	RunAfterOnError bool

	// SwallowErrorOnHandled controls whether OnError returning nil means
	// the original handler error is considered handled.
	SwallowErrorOnHandled bool
}

type Hooks

type Hooks struct {
	OnRequestStart  func(fh.Ctx) error
	OnBeforeHandler func(fh.Ctx) error
	OnAfterHandler  func(fh.Ctx) error
	OnError         func(fh.Ctx, error) error
	OnRequestEnd    func(fh.Ctx, error) error
}

Jump to

Keyboard shortcuts

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