router

package
v0.0.0-...-24fabdb Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2024 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const SESSION_COOKIE_NAME = "session_token"

Variables

View Source
var ErrExpiredCookie = fmt.Errorf("cookie has expired")

Functions

func ClientIP

func ClientIP(r *http.Request) string

func HandleMetrics

func HandleMetrics(logger *core.Logger) http.Handler

func ReadJSON

func ReadJSON[T any](req *http.Request) (T, error)

data, err := readJSON[MyStructType](r)

func RecordRequest

func RecordRequest(code int, d time.Duration)

func RenderHTML

func RenderHTML(w http.ResponseWriter, status int, s string) error

func RenderJSON

func RenderJSON[T any](w http.ResponseWriter, status int, obj T) error

func renderJSON[T any](w http.ResponseWriter, r *http.Request, status int, obj T) error {

Types

type ContextKey

type ContextKey string
const ClaimsKey ContextKey = "claims"

type HTTPServer

type HTTPServer struct {
	Logger *core.Logger
	Config *core.Config
	db.CuttleDB
	db.AuthDB
	Handler http.Handler
	// Mux saves the http.ServeMux instance. This provides easier access to the
	// mux without having to enforce a ref type on HTTPServer.Handler everytime.
	// We can now use HTTPServer.Mux.Handle() instead of HTTPServer.Handler.(*http.ServeMux).Handle().
	Mux *http.ServeMux
}

func NewHTTPServer

func NewHTTPServer(logger *core.Logger, config *core.Config) HTTPServer

func (*HTTPServer) Start

func (s *HTTPServer) Start(ctx context.Context, timeoutSec int) error

type Metrics

type Metrics struct {
	Start    time.Time
	Requests int
	Errors   int
	Duration []time.Duration
}

func NewMetrics

func NewMetrics() *Metrics

type MetricsReport

type MetricsReport struct {
	Start       time.Time `json:"start"`        // Time metrics started being gathered
	End         time.Time `json:"end"`          // Time metrics were gathered
	DurationSec float64   `json:"duration_sec"` // Duration metrics where gathered in Seconds
	Requests    int       `json:"requests"`     // Total Requests
	Errors      int       `json:"errors"`       // Total Errors
	RequestsPS  float64   `json:"requests_ps"`  // Requests Per Second
	ErrorsPS    float64   `json:"errors_ps"`    // Errors Per Second
	MinDuration float64   `json:"min_duration"` // Minimum Duration
	AvgDuration float64   `json:"avg_duration"` // Average Duration
	MaxDuration float64   `json:"max_duration"` // Maximum Duration
}

func Report

func Report() MetricsReport

type Middleware

type Middleware func(http.Handler) http.Handler

func APIAuthMiddleware

func APIAuthMiddleware(logger *core.Logger, users db.CuttleDB) Middleware

func LoggerMiddleware

func LoggerMiddleware(logger *core.Logger) Middleware

func WebAuthMiddleware

func WebAuthMiddleware(logger *core.Logger, authDB db.AuthDB, secret string) Middleware

type ReqMetrics

type ReqMetrics struct {
	ClientIP     string        `json:"client_ip"`
	RequestTime  time.Time     `json:"request_time"`
	Method       string        `json:"method"`
	URI          string        `json:"uri"`
	ResponseCode int           `json:"response_code"`
	ResponseSize int64         `json:"response_size"`
	Referer      string        `json:"referer"`
	UserAgent    string        `json:"user_agent"`
	Duration     time.Duration `json:"duration"`
}

func NewReqMetrics

func NewReqMetrics(r *http.Request) ReqMetrics

type RouterGroup

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

func NewRouterGroup

func NewRouterGroup(mux *http.ServeMux, path string, middleware ...Middleware) (*RouterGroup, error)

func (*RouterGroup) ANY

func (group *RouterGroup) ANY(path string, handler http.Handler, middleware ...Middleware)

func (*RouterGroup) GET

func (group *RouterGroup) GET(path string, handler http.Handler, middleware ...Middleware)

func (*RouterGroup) Group

func (group *RouterGroup) Group(path string, middleware ...Middleware) *RouterGroup

type SessionCookie

type SessionCookie struct {
	Value   string // Bearer token used to lookup JWT from AuthCache.
	Path    string
	Expires time.Time // Cookie expiration time.
}

func GetSessionCookie

func GetSessionCookie(r *http.Request) (SessionCookie, error)

func NewSessionCookie

func NewSessionCookie(bearer string) (SessionCookie, error)

func NewSessionCookieFromCookie

func NewSessionCookieFromCookie(cookie *http.Cookie) (SessionCookie, error)

func (SessionCookie) Delete

func (s SessionCookie) Delete(w http.ResponseWriter)

func (SessionCookie) Validate

func (s SessionCookie) Validate() error

func (SessionCookie) Write

Jump to

Keyboard shortcuts

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