srvhttp

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2024 License: Apache-2.0 Imports: 16 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

func (*Context) ClientIP

func (c *Context) ClientIP() string

func (*Context) Flush

func (c *Context) Flush()

func (*Context) Get

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

func (*Context) Header

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

func (*Context) Hijack

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

func (*Context) PathParams

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

func (*Context) PathRegex

func (c *Context) PathRegex() string

func (*Context) PathTemplate

func (c *Context) PathTemplate() string

func (*Context) RouteName

func (c *Context) RouteName() string

func (*Context) Set

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

func (*Context) Size

func (c *Context) Size() int

func (*Context) Status

func (c *Context) Status() int

func (*Context) Value

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

func (*Context) Write

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

func (*Context) WriteHeader

func (c *Context) WriteHeader(s int)

func (*Context) Written

func (c *Context) Written() bool

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 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) 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