app

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: May 13, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LogMidddle = func(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		start := time.Now()
		re := &StatusRecorder{ResponseWriter: w}
		h.ServeHTTP(re, r)

		slog.InfoContext(
			r.Context(),
			"http request",
			slog.Int("status", re.Status),
			slog.String("duration", time.Since(start).String()),
			slog.String("method", r.Method),
			slog.String("path", r.URL.Path),
		)
	})
}
View Source
var RecoveryMiddle = func(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		defer func() {
			if err := recover(); err != nil {
				w.WriteHeader(http.StatusInternalServerError)
				stack := make([]byte, 1024*8)
				stack = stack[:runtime.Stack(stack, false)]
				slog.ErrorContext(r.Context(), "panic",
					slog.String("path", r.URL.Path),
					slog.Any("error", err),
					slog.Any("stack", stack),
				)
			}
		}()

		h.ServeHTTP(w, r)

	})
}

Functions

This section is empty.

Types

type Addr

type Addr struct {
	IP   string
	Port int
}

func (*Addr) String

func (a *Addr) String() string

type App

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

func New

func New(options ...AppOption) *App

func (*App) DELETE

func (a *App) DELETE(path string, h http.Handler)

func (*App) GET

func (a *App) GET(path string, h http.Handler)

func (*App) HEAD

func (a *App) HEAD(path string, h http.Handler)

func (*App) POST

func (a *App) POST(path string, h http.Handler)

func (*App) PUT

func (a *App) PUT(path string, h http.Handler)

func (*App) RegisteGrpcService

func (a *App) RegisteGrpcService(desc *grpc.ServiceDesc, s any)

func (*App) Run

func (a *App) Run()

type AppOption

type AppOption func(aos *AppOptions)

func WihtGrpcWeb

func WihtGrpcWeb(open bool) AppOption

func WithAddr

func WithAddr(ip string, p int) AppOption

func WithCorsOptions

func WithCorsOptions(opt *cors.Options) AppOption

func WithPromAddr

func WithPromAddr(ip string, p int) AppOption

func WithSlogConfig added in v0.0.3

func WithSlogConfig(l *SLogConfig) AppOption

func WithTLSConfig

func WithTLSConfig(c *TLSConfig) AppOption

type AppOptions

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

type Middleware added in v0.0.3

type Middleware func(h http.Handler) http.Handler

type PromMiddleWare added in v0.0.3

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

func MetricMiddle added in v0.0.3

func MetricMiddle(name string, buckets ...float64) *PromMiddleWare

NewMiddleware returns a new prometheus Middleware handler.

func (*PromMiddleWare) Hander added in v0.0.3

func (m *PromMiddleWare) Hander(h http.Handler) http.Handler

type SLogConfig added in v0.0.3

type SLogConfig struct {
	Level      slog.Level
	JSONOutPut bool
	AddSource  bool
}

type StatusRecorder added in v0.0.3

type StatusRecorder struct {
	http.ResponseWriter
	Status int
}

func (*StatusRecorder) WriteHeader added in v0.0.3

func (r *StatusRecorder) WriteHeader(status int)

type TLSConfig

type TLSConfig struct {
	Addr
	KeyPath  string
	CertPath string
}

Jump to

Keyboard shortcuts

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