srvhttp

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2024 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var DefaultCorsConfig = CorsConfig{
	AllowAllOrigins: true,
	AllowMethods: []string{
		http.MethodGet, http.MethodPost,
		http.MethodPut, http.MethodPatch,
		http.MethodDelete, http.MethodHead, http.MethodOptions,
	},
	AllowHeaders: []string{"Origin", "Content-Length", "Content-Type"},
	MaxAge:       12 * time.Hour,
}

Functions

func ClientIP

func ClientIP(r *http.Request) string

Types

type Context

type Context struct {
	context.Context
	Request *http.Request
	// contains filtered or unexported fields
}

func NewContext

func NewContext(rsp http.ResponseWriter, req *http.Request) *Context

NewContext returns a new Context instance.

func (*Context) Buffer added in v0.0.2

func (c *Context) Buffer() *bytes.Buffer

Buffer returns the Buffer associated with the Context.

func (*Context) ClientIP

func (c *Context) ClientIP() string

ClientIP implements a best effort algorithm to return the real client IP

func (*Context) File added in v0.0.2

func (c *Context) File(filepath string)

File sends a response with the content of the file.

func (*Context) Flush

func (c *Context) Flush()

Flush implements the http.Flusher interface to allow an HTTP handler to flush buffered data to the client.

func (*Context) Get

func (c *Context) Get(key string) (any, bool)

Get returns the value associated with key.

func (*Context) GetBool added in v0.0.2

func (c *Context) GetBool(key string) (b bool)

GetBool returns the value associated with the key as a boolean.

func (*Context) GetDuration added in v0.0.2

func (c *Context) GetDuration(key string) (d time.Duration)

GetDuration returns the value associated with the key as a duration.

func (*Context) GetFloat64 added in v0.0.2

func (c *Context) GetFloat64(key string) (f64 float64)

GetFloat64 returns the value associated with the key as a float64.

func (*Context) GetInt added in v0.0.2

func (c *Context) GetInt(key string) (i int)

GetInt returns the value associated with the key as an integer.

func (*Context) GetInt64 added in v0.0.2

func (c *Context) GetInt64(key string) (i64 int64)

GetInt64 returns the value associated with the key as an integer.

func (*Context) GetString added in v0.0.2

func (c *Context) GetString(key string) (s string)

GetString returns the value associated with the key as a string.

func (*Context) GetStringMap added in v0.0.2

func (c *Context) GetStringMap(key string) (sm map[string]any)

GetStringMap returns the value associated with the key as a map of interfaces.

func (*Context) GetStringMapString added in v0.0.2

func (c *Context) GetStringMapString(key string) (sms map[string]string)

GetStringMapString returns the value associated with the key as a map of strings.

func (*Context) GetStringMapStringSlice added in v0.0.2

func (c *Context) GetStringMapStringSlice(key string) (smss map[string][]string)

GetStringMapStringSlice returns the value associated with the key as a map to a slice of strings.

func (*Context) GetStringSlice added in v0.0.2

func (c *Context) GetStringSlice(key string) (ss []string)

GetStringSlice returns the value associated with the key as a slice of strings.

func (*Context) GetTime added in v0.0.2

func (c *Context) GetTime(key string) (t time.Time)

GetTime returns the value associated with the key as time.

func (*Context) GetUint added in v0.0.2

func (c *Context) GetUint(key string) (ui uint)

GetUint returns the value associated with the key as an unsigned integer.

func (*Context) GetUint64 added in v0.0.2

func (c *Context) GetUint64(key string) (ui64 uint64)

GetUint64 returns the value associated with the key as an unsigned integer.

func (*Context) Header

func (c *Context) Header() http.Header

Header returns the response header

func (*Context) Hijack

func (c *Context) Hijack() (net.Conn, *bufio.ReadWriter, error)

Hijack implements the http.Hijacker interface to allow an HTTP handler to take over the connection.

func (*Context) PathParams

func (c *Context) PathParams() map[string]string

PathParams returns the path parameters associated with the request.

func (*Context) PathRegex

func (c *Context) PathRegex() string

PathRegex returns the path regex used to match the current request, if any.

func (*Context) PathTemplate

func (c *Context) PathTemplate() string

PathTemplate returns the path template used to match the current request, if any.

func (*Context) RouteName

func (c *Context) RouteName() string

RouteName returns the name of the route matched for the current request, if any.

func (*Context) SSEvent added in v0.0.2

func (c *Context) SSEvent(name string, message any) error

SSEvent sends a Server-Sent Event.

func (*Context) Set

func (c *Context) Set(key string, value any)

Set sets the value associated with key.

func (*Context) Size

func (c *Context) Size() int

Size returns the response size

func (*Context) Status

func (c *Context) Status() int

Status returns the response status code

func (*Context) Value

func (c *Context) Value(key any) any

Value returns the value associated with key.

func (*Context) Write

func (c *Context) Write(b []byte) (int, error)

Write writes the response body

func (*Context) WriteHeader

func (c *Context) WriteHeader(s int)

WriteHeader writes the response header

func (*Context) Written

func (c *Context) Written() bool

Written returns whether or not the response for this context has been written.

type CorsConfig

type CorsConfig struct {
	AllowPrivateNetwork bool
	AllowCredentials    bool
	AllowAllOrigins     bool
	AllowOrigins        []string
	AllowMethods        []string
	AllowHeaders        []string
	ExposeHeaders       []string
	MaxAge              time.Duration
	// contains filtered or unexported fields
}

type Engine

type Engine struct {
	Router
	// contains filtered or unexported fields
}

func New

func New(opts ...Option) *Engine

func (*Engine) PrintRoutes

func (e *Engine) PrintRoutes()

func (*Engine) ServeHTTP

func (e *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request)
Example
engine := New()
engine.POST("/test1", func(c *Context) (any, error) {
	return map[string]string{"greeting": "hello"}, nil
})

srv := httptest.NewServer(engine)
rsp, err := srv.Client().Post(srv.URL+"/test1", "", nil)
if err != nil {
	fmt.Println("err occurred:", err)
	return
}
defer rsp.Body.Close()

b, _ := io.ReadAll(rsp.Body)
fmt.Println(string(b))
Output:

{"data":{"greeting":"hello"}}

func (*Engine) UseCors

func (e *Engine) UseCors(c CorsConfig)

type Event added in v0.0.2

type Event struct {
	Event string
	Id    string
	Retry uint
	Data  any
}

func (Event) Encode added in v0.0.2

func (e Event) Encode(c *Context) error

type Handler

type Handler func(*Context) (any, error)

type Middleware

type Middleware func(*Context, Handler) (any, error)

type Option

type Option func(*Engine)

func WithDebug

func WithDebug(open bool) Option

func WithLogger

func WithLogger(logger contract.Logger) Option

func WithResponseFunc

func WithResponseFunc(rspFunc ResponseFunc) Option

type ResponseFunc

type ResponseFunc func(response any, err error, c *Context)

type Route

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

func (Route) Name

func (r Route) Name(name string)

type Router

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

func (Router) Any

func (r Router) Any(path string, handler Handler) Route

func (Router) DELETE

func (r Router) DELETE(path string, handler Handler) Route

func (Router) GET

func (r Router) GET(path string, handler Handler) Route

func (Router) Group

func (r Router) Group(pathPrefix string) Router

func (Router) HEAD

func (r Router) HEAD(path string, handler Handler) Route

func (Router) OPTIONS

func (r Router) OPTIONS(path string, handler Handler) Route

func (Router) PATCH

func (r Router) PATCH(path string, handler Handler) Route

func (Router) POST

func (r Router) POST(path string, handler Handler) Route

func (Router) PUT

func (r Router) PUT(path string, handler Handler) Route

func (Router) Static

func (r Router) Static(relativePath, root string, listFiles bool)

func (Router) StaticFS

func (r Router) StaticFS(relativePath string, fs http.FileSystem, listFiles bool)

func (Router) StaticFile added in v0.0.2

func (r Router) StaticFile(relativePath, filepath string)

func (Router) StaticFileFS added in v0.0.2

func (r Router) StaticFileFS(relativePath, filepath string, fs http.FileSystem)

func (Router) Sub

func (r Router) Sub() Router

func (Router) Use

func (r Router) Use(mds ...Middleware)

Jump to

Keyboard shortcuts

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