ghttp

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2022 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ServerStatusStopped = 0
	ServerStatusRunning = 1
)

Variables

This section is empty.

Functions

func CORSDefault

func CORSDefault(header http.Header) http.Header

Types

type ApiJsonResult

type ApiJsonResult struct {
	Code  int         `json:"code"`
	Msg   string      `json:"msg,omitempty"`
	Data  interface{} `json:"data,omitempty"`
	MsgId string      `json:"msgId"`
	Time  int64       `json:"time"`
}

type Client

type Client = client2.Client

func NewClient

func NewClient() *Client

NewClient creates and returns a new HTTP client object.

type ClientResponse

type ClientResponse = client2.Response

func Connect

func Connect(url string, data ...interface{}) (*ClientResponse, error)

Connect is a convenience method for sending CONNECT request. NOTE that remembers CLOSING the response object when it'll never be used.

func Delete

func Delete(url string, data ...interface{}) (*ClientResponse, error)

Delete is a convenience method for sending DELETE request. NOTE that remembers CLOSING the response object when it'll never be used.

func DoRequest

func DoRequest(method, url string, data ...interface{}) (*ClientResponse, error)

DoRequest is a convenience method for sending custom http method request. NOTE that remembers CLOSING the response object when it'll never be used.

func Get

func Get(url string, data ...interface{}) (*ClientResponse, error)

Get is a convenience method for sending GET request. NOTE that remembers CLOSING the response object when it'll never be used.

func Head(url string, data ...interface{}) (*ClientResponse, error)

Head is a convenience method for sending HEAD request. NOTE that remembers CLOSING the response object when it'll never be used. Deprecated, please use g.Client().Head or NewClient().Head instead.

func Options

func Options(url string, data ...interface{}) (*ClientResponse, error)

Options is a convenience method for sending OPTIONS request. NOTE that remembers CLOSING the response object when it'll never be used.

func Patch

func Patch(url string, data ...interface{}) (*ClientResponse, error)

Patch is a convenience method for sending PATCH request. NOTE that remembers CLOSING the response object when it'll never be used. Deprecated, please use g.Client().Patch or NewClient().Patch instead.

func Post

func Post(url string, data ...interface{}) (*ClientResponse, error)

Post is a convenience method for sending POST request. NOTE that remembers CLOSING the response object when it'll never be used.

func Put

func Put(url string, data ...interface{}) (*ClientResponse, error)

Put is a convenience method for sending PUT request. NOTE that remembers CLOSING the response object when it'll never be used.

type Group

type Group struct {
	sync.Mutex
	// contains filtered or unexported fields
}

路由组

func (*Group) AddRoute

func (g *Group) AddRoute(name string, handlerFunc HandlerFunc, middleware ...MiddlewareFunc)

AddRoute 添加路由

func (*Group) Use

func (g *Group) Use(middleware ...MiddlewareFunc) *Group

type HandlerFunc

type HandlerFunc func(req *Request)

type MiddlewareFunc

type MiddlewareFunc func(HandlerFunc) HandlerFunc

type Request

type Request struct {
	*http.Request
	Response *Response // Corresponding Response of this request.
	// contains filtered or unexported fields
}

Request is the context object for a request.

func NewRequest

func NewRequest(r *http.Request, w http.ResponseWriter) *Request

NewRequest creates and returns a new request object.

func (*Request) GetBody

func (r *Request) GetBody() []byte

GetBody retrieves and returns request body content as bytes. It can be called multiple times retrieving the same body content.

func (*Request) GetBodyString

func (r *Request) GetBodyString() string

GetBodyString retrieves and returns request body content as string. It can be called multiple times retrieving the same body content.

func (*Request) GetBool

func (r *Request) GetBool(key string, def ...interface{}) bool

GetBool is an alias and convenient function for GetRequestBool. See GetRequestBool.

func (*Request) GetClientIp

func (r *Request) GetClientIp() string

GetClientIp returns the client ip of this request without port. Note that this ip address might be modified by client header.

func (*Request) GetFloat32

func (r *Request) GetFloat32(key string, def ...interface{}) float32

GetFloat32 is an alias and convenient function for GetRequestFloat32. See GetRequestFloat32.

func (*Request) GetFloat64

func (r *Request) GetFloat64(key string, def ...interface{}) float64

GetFloat64 is an alias and convenient function for GetRequestFloat64. See GetRequestFloat64.

func (*Request) GetForm

func (r *Request) GetForm(key string, def ...interface{}) interface{}

GetForm retrieves and returns parameter <key> from form. It returns <def> if <key> does not exist in the form and <def> is given, or else it returns nil.

func (*Request) GetHost

func (r *Request) GetHost() string

GetHost returns current request host name, which might be a domain or an IP without port.

func (*Request) GetInt

func (r *Request) GetInt(key string, def ...interface{}) int

GetInt is an alias and convenient function for GetRequestInt. See GetRequestInt.

func (*Request) GetInt32

func (r *Request) GetInt32(key string, def ...interface{}) int32

GetInt32 is an alias and convenient function for GetRequestInt32. See GetRequestInt32.

func (*Request) GetInt64

func (r *Request) GetInt64(key string, def ...interface{}) int64

GetInt64 is an alias and convenient function for GetRequestInt64. See GetRequestInt64.

func (*Request) GetParam

func (r *Request) GetParam(key string, def ...interface{}) interface{}

GetParam returns custom parameter with given name <key>. It returns <def> if <key> does not exist. It returns nil if <def> is not passed.

func (*Request) GetQuery

func (r *Request) GetQuery(key string, def ...interface{}) interface{}

GetQuery retrieves and returns parameter with given name <key> from query string and request body. It returns <def> if <key> does not exist in the query and <def> is given, or else it returns nil.

Note that if there're multiple parameters with the same name, the parameters are retrieved and overwrote in order of priority: query > body.

func (*Request) GetQueryParams

func (r *Request) GetQueryParams() interface{}

GetQueryParams return all parameters by GET Http Method

func (*Request) GetReferer

func (r *Request) GetReferer() string

GetReferer returns referer of this request.

func (*Request) GetRemoteIp

func (r *Request) GetRemoteIp() string

GetRemoteIp returns the ip from RemoteAddr.

func (*Request) GetRequest

func (r *Request) GetRequest(key string, def ...interface{}) interface{}

GetRequest retrieves and returns the parameter named <key> passed from client and custom params as interface{}, no matter what HTTP method the client is using. The parameter <def> specifies the default value if the <key> does not exist.

GetRequest is one of the most commonly used functions for retrieving parameters.

Note that if there're multiple parameters with the same name, the parameters are retrieved and overwrote in order of priority: router < query < body < form < custom.

func (*Request) GetRequestBool

func (r *Request) GetRequestBool(key string, def ...interface{}) bool

GetRequestBool retrieves and returns the parameter named <key> passed from client and custom params as bool, no matter what HTTP method the client is using. The parameter <def> specifies the default value if the <key> does not exist.

func (*Request) GetRequestFloat32

func (r *Request) GetRequestFloat32(key string, def ...interface{}) float32

GetRequestFloat32 retrieves and returns the parameter named <key> passed from client and custom params as float32, no matter what HTTP method the client is using. The parameter <def> specifies the default value if the <key> does not exist.

func (*Request) GetRequestFloat64

func (r *Request) GetRequestFloat64(key string, def ...interface{}) float64

GetRequestFloat64 retrieves and returns the parameter named <key> passed from client and custom params as float64, no matter what HTTP method the client is using. The parameter <def> specifies the default value if the <key> does not exist.

func (*Request) GetRequestInt

func (r *Request) GetRequestInt(key string, def ...interface{}) int

GetRequestInt retrieves and returns the parameter named <key> passed from client and custom params as int, no matter what HTTP method the client is using. The parameter <def> specifies the default value if the <key> does not exist.

func (*Request) GetRequestInt32

func (r *Request) GetRequestInt32(key string, def ...interface{}) int32

GetRequestInt32 retrieves and returns the parameter named <key> passed from client and custom params as int32, no matter what HTTP method the client is using. The parameter <def> specifies the default value if the <key> does not exist.

func (*Request) GetRequestInt64

func (r *Request) GetRequestInt64(key string, def ...interface{}) int64

GetRequestInt64 retrieves and returns the parameter named <key> passed from client and custom params as int64, no matter what HTTP method the client is using. The parameter <def> specifies the default value if the <key> does not exist.

func (*Request) GetRequestString

func (r *Request) GetRequestString(key string, def ...interface{}) string

GetRequestString retrieves and returns the parameter named <key> passed from client and custom params as string, no matter what HTTP method the client is using. The parameter <def> specifies the default value if the <key> does not exist.

func (*Request) GetRequestUint

func (r *Request) GetRequestUint(key string, def ...interface{}) uint

GetRequestUint retrieves and returns the parameter named <key> passed from client and custom params as uint, no matter what HTTP method the client is using. The parameter <def> specifies the default value if the <key> does not exist.

func (*Request) GetRequestUint32

func (r *Request) GetRequestUint32(key string, def ...interface{}) uint32

GetRequestUint32 retrieves and returns the parameter named <key> passed from client and custom params as uint32, no matter what HTTP method the client is using. The parameter <def> specifies the default value if the <key> does not exist.

func (*Request) GetRequestUint64

func (r *Request) GetRequestUint64(key string, def ...interface{}) uint64

GetRequestUint64 retrieves and returns the parameter named <key> passed from client and custom params as uint64, no matter what HTTP method the client is using. The parameter <def> specifies the default value if the <key> does not exist.

func (*Request) GetRequestUint8

func (r *Request) GetRequestUint8(key string, def ...interface{}) uint8

GetRequestUint retrieves and returns the parameter named <key> passed from client and custom params as uint, no matter what HTTP method the client is using. The parameter <def> specifies the default value if the <key> does not exist.

func (*Request) GetRequestVar

func (r *Request) GetRequestVar(key string, def ...interface{}) *gvar.Var

GetRequestVar retrieves and returns the parameter named <key> passed from client and custom params as gvar.Var, no matter what HTTP method the client is using. The parameter <def> specifies the default value if the <key> does not exist.

func (*Request) GetString

func (r *Request) GetString(key string, def ...interface{}) string

GetString is an alias and convenient function for GetRequestString. See GetRequestString.

func (*Request) GetUint

func (r *Request) GetUint(key string, def ...interface{}) uint

GetUint is an alias and convenient function for GetRequestUint. See GetRequestUint.

func (*Request) GetUint32

func (r *Request) GetUint32(key string, def ...interface{}) uint32

GetUint32 is an alias and convenient function for GetRequestUint32. See GetRequestUint32.

func (*Request) GetUint64

func (r *Request) GetUint64(key string, def ...interface{}) uint64

GetUint64 is an alias and convenient function for GetRequestUint64. See GetRequestUint64.

func (*Request) GetUint8

func (r *Request) GetUint8(key string, def ...interface{}) uint8

GetUint8 is an alias and convenient function for GetRequestUint32. See GetRequestUint32.

func (*Request) GetUrl

func (r *Request) GetUrl() string

GetUrl returns current URL of this request.

type Response

type Response struct {
	*ResponseWriter                 // Underlying ResponseWriter.
	Writer          *ResponseWriter // Alias of ResponseWriter.
	Request         *Request        // According request.
}

Response is the ghttp response manager. Note that it implements the ghttp.ResponseWriter interface with buffering feature.

func NewResponse

func NewResponse(w http.ResponseWriter) *Response

NewResponse creates and returns a new Response object.

func (*Response) ApiFail

func (r *Response) ApiFail(err error)

func (*Response) ApiRes

func (r *Response) ApiRes(data []byte)

func (*Response) ApiSuccess

func (r *Response) ApiSuccess(data interface{})

func (*Response) Write

func (r *Response) Write(content ...interface{})

Write writes <content> to the response buffer.

func (*Response) WriteJson

func (r *Response) WriteJson(content interface{}) error

WriteJson writes <content> to the response with JSON format.

type ResponseWriter

type ResponseWriter struct {
	Status int // HTTP status.
	// contains filtered or unexported fields
}

ResponseWriter is the custom writer for ghttp response.

func (*ResponseWriter) Flush

func (w *ResponseWriter) Flush()

OutputBuffer outputs the buffer to client and clears the buffer.

func (*ResponseWriter) Header

func (w *ResponseWriter) Header() http.Header

Header implements the interface function of ghttp.ResponseWriter.Header.

func (*ResponseWriter) RawWriter

func (w *ResponseWriter) RawWriter() http.ResponseWriter

RawWriter returns the underlying ResponseWriter.

func (*ResponseWriter) Write

func (w *ResponseWriter) Write(data []byte) (int, error)

Write implements the interface function of ghttp.ResponseWriter.Write.

func (*ResponseWriter) WriteHeader

func (w *ResponseWriter) WriteHeader(status int)

WriteHeader implements the interface of ghttp.ResponseWriter.WriteHeader.

type Router

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

路由器

func NewRouter

func NewRouter() *Router

func (*Router) Run

func (r *Router) Run(req *Request)

type Server

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

func NewServer

func NewServer() *Server

func (*Server) AddRoute

func (s *Server) AddRoute(path string, handler HandlerFunc, middleware ...MiddlewareFunc)

添加路由

func (*Server) GetHost

func (s *Server) GetHost() string

获取服务器IP地址

func (*Server) GetId

func (s *Server) GetId() string

获取服务器ID

func (*Server) GetName

func (s *Server) GetName() string

获取服务器名称

func (*Server) GetPort

func (s *Server) GetPort() int32

获取服务器端口

func (*Server) Run

func (s *Server) Run()

运行服务器

func (*Server) Start

func (s *Server) Start()

启动服务器

func (*Server) Stop

func (s *Server) Stop()

停止服务器

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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