middleware

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2025 License: MIT Imports: 9 Imported by: 0

README

Middleware

Perhaps the wiki is the most up-to-date. https://github.com/poteto-go/poteto/wiki/Middleware

middleware list

name summary
CORS CORS Policy
Camara Some Security Header
JWS Secure JWT
Timeout Timeout
RequestLogger Log config on Request

use middleware

package main

import (
  "net/http"

  "github.com/poteto-go/poteto"
  "github.com/poteto-go/poteto/middleware"
)

func main() {
  p := poteto.New()

  p.Middleware(middleware.CORSWithConfig(CORSConfig{
    AllowOrigins: []string{"*"},
    AllowMethods: []string{http.MethodGet, http.MethodPut, http.MethodPost, http.MethodDelete}}
  ))
  // handler
  ...
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultCORSConfig = CORSConfig{
	AllowOrigins: []string{"*"},
	AllowMethods: []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodDelete},
}
View Source
var DefaultCamaraConfig = CamaraConfig{
	ContentSecurityPolicy:   "default-src 'self';base-uri 'self';block-all-mixed-content;font-src 'self' https: data:;frame-ancestors 'self';img-src 'self' data:;object-src 'none';script-src 'self';script-src-attr 'none';style-src 'self' https: 'unsafe-inline';upgrade-insecure-requests",
	XFrameOption:            "SAMEORIGIN",
	StrictTransportSecurity: "max-age=15552000; includeSubDomains",
	XDownloadOption:         "noopen",
	XContentTypeOption:      "nosniff",
	ReferrerPolicy:          "no-referrer",
}
View Source
var DefaultJWSConfig = &potetoJWSConfig{
	AuthScheme: constant.AUTH_SCHEME,
	SignMethod: constant.ALGORITHM_HS256,
	ContextKey: "user",
	ClaimsFunc: func(c poteto.Context) jwt.Claims {
		return jwt.MapClaims{}
	},
}
View Source
var DefaultRequestLoggerConfig = RequestLoggerConfig{
	HasStatus:        true,
	HasMethod:        true,
	HasRoutePath:     true,
	HasRequestID:     true,
	HasUserAgent:     true,
	HasRemoteIP:      true,
	HasRealIP:        true,
	HasHost:          true,
	HasContentLength: true,
	OpenHeaders:      []string{},
	HasError:         true,
	HasStartTime:     true,
	HasEndTime:       true,
	HasDuration:      true,
}
View Source
var DefaultTimeoutConfig = TimeoutConfig{
	Limit:           time.Second * 10,
	TimeoutResponse: DefaultTimeoutResponse,
}
View Source
var DefaultTimeoutResponse = TimeoutResponseEx{
	Message: "Gateway Time Out",
}

Functions

func CORSWithConfig

func CORSWithConfig(config CORSConfig) poteto.MiddlewareFunc

func CamaraWithConfig

func CamaraWithConfig(config CamaraConfig) poteto.MiddlewareFunc

Provide Some Security Header

func JWSWithConfig

func JWSWithConfig(cfg PotetoJWSConfig) poteto.MiddlewareFunc

func RequestLoggerWithConfig

func RequestLoggerWithConfig(config RequestLoggerConfig) poteto.MiddlewareFunc

func TimeoutWithConfig

func TimeoutWithConfig(config TimeoutConfig) poteto.MiddlewareFunc

Types

type CORSConfig

type CORSConfig struct {
	AllowOrigins []string `yaml:"allow_origins"`
	AllowMethods []string `yaml:"allow_methods"`
}

type CamaraConfig

type CamaraConfig struct {
	ContentSecurityPolicy   string `yaml:"content_security_policy"`
	XFrameOption            string `yaml:"x_frame_option"`
	StrictTransportSecurity string `yaml:"strict_transport_security"`
	XDownloadOption         string `yaml:"x_download_option"`
	XContentTypeOption      string `yaml:"x_content_type_option"`
	ReferrerPolicy          string `yaml:"referrer_policy"`
}

type LogHandlerFunc

type LogHandlerFunc func(ctx poteto.Context, rlv RequestLoggerValues) error

type PotetoJWSConfig

type PotetoJWSConfig interface {
	KeyFunc(token *jwt.Token) (any, error)
	ParseToken(ctx poteto.Context, auth string) (any, error)
}

func NewPotetoJWSConfig

func NewPotetoJWSConfig(contextKey string, signKey any) PotetoJWSConfig

type RequestLoggerConfig

type RequestLoggerConfig struct {
	HasStatus        bool
	HasMethod        bool
	HasRoutePath     bool
	HasRequestID     bool
	HasUserAgent     bool
	HasRemoteIP      bool
	HasRealIP        bool
	HasHost          bool
	HasContentLength bool
	OpenHeaders      []string
	HasError         bool
	HasStartTime     bool
	HasEndTime       bool
	HasDuration      bool
	LogHandleFunc    LogHandlerFunc
}

type RequestLoggerValues

type RequestLoggerValues struct {
	Status        int
	Method        string
	RoutePath     string
	RequestId     string
	UserAgent     string
	RemoteIP      string
	RealIP        string
	Host          string
	ContentLength string
	Headers       map[string][]string
	Error         error
	StartTime     time.Time
	EndTime       time.Time
	Duration      time.Duration
}

type TimeoutConfig

type TimeoutConfig struct {
	Limit           time.Duration `yaml:"limit"`
	TimeoutResponse any
}

type TimeoutResponseEx

type TimeoutResponseEx struct {
	Message string `json:"message"`
}

Jump to

Keyboard shortcuts

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