Documentation
¶
Index ¶
- Constants
- Variables
- func Chain(middlewares ...func(next http.Handler) http.Handler) func(next http.Handler) http.Handler
- type Context
- func (c *Context) Bind(obj any) error
- func (c *Context) Err(err error) error
- func (c *Context) SendBlob(reader io.Reader) error
- func (c *Context) SendFile(name string, reader io.Reader) error
- func (c *Context) SendJSON(data any) error
- func (c *Context) SendJSONP(data any, indent string) error
- func (c *Context) SendJSONRaw(data io.Reader) error
- func (c *Context) SendNoContent() error
- func (c *Context) SendString(s string) error
- func (c *Context) SendZip(name string, files map[string]io.Reader) error
- func (c *Context) SetHeader(kv ...string) *Context
- func (c *Context) SetStatus(code int) *Context
- type HandlerError
- type HandlerFunc
- type Logger
- type Mux
- func (m *Mux) CONNECT(path string, handler http.HandlerFunc, ...)
- func (m *Mux) DELETE(path string, handler http.HandlerFunc, ...)
- func (m *Mux) ErrorHandler(handler func(c *Context, err error))
- func (m *Mux) GET(path string, handler http.HandlerFunc, ...)
- func (m Mux) Group(pathGroup string, middlewares ...func(next http.Handler) http.Handler) *Mux
- func (m *Mux) HEAD(path string, handler http.HandlerFunc, ...)
- func (m *Mux) Handle(path string, handler http.Handler, ...)
- func (m *Mux) HandleFunc(path string, handler http.HandlerFunc, ...)
- func (m *Mux) HandleFuncWildcard(path string, handler http.HandlerFunc, ...)
- func (m *Mux) HandleWildcard(path string, handler http.Handler, ...)
- func (m *Mux) HandleWithMethod(method, path string, handler http.HandlerFunc, ...)
- func (m *Mux) NotFound(handler http.HandlerFunc)
- func (m *Mux) OPTIONS(path string, handler http.HandlerFunc, ...)
- func (m *Mux) PATCH(path string, handler http.HandlerFunc, ...)
- func (m *Mux) POST(path string, handler http.HandlerFunc, ...)
- func (m *Mux) PUT(path string, handler http.HandlerFunc, ...)
- func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (m *Mux) TRACE(path string, handler http.HandlerFunc, ...)
- func (m *Mux) Use(middlewares ...func(next http.Handler) http.Handler)
- func (m *Mux) Wrap(handler HandlerFunc) func(http.ResponseWriter, *http.Request)
- type Option
- type OptionStart
- type Server
Constants ¶
const ( MIMEApplicationJSON = "application/json" MIMEApplicationJSONCharsetUTF8 = MIMEApplicationJSON + "; " + charsetUTF8 MIMEApplicationJavaScript = "application/javascript" MIMEApplicationJavaScriptCharsetUTF8 = MIMEApplicationJavaScript + "; " + charsetUTF8 MIMEApplicationXML = "application/xml" MIMEApplicationXMLCharsetUTF8 = MIMEApplicationXML + "; " + charsetUTF8 MIMETextXML = "text/xml" MIMETextXMLCharsetUTF8 = MIMETextXML + "; " + charsetUTF8 MIMEApplicationForm = "application/x-www-form-urlencoded" MIMEApplicationProtobuf = "application/protobuf" MIMEApplicationMsgpack = "application/msgpack" MIMETextHTML = "text/html" MIMETextHTMLCharsetUTF8 = MIMETextHTML + "; " + charsetUTF8 MIMETextPlain = "text/plain" MIMETextPlainCharsetUTF8 = MIMETextPlain + "; " + charsetUTF8 MIMEMultipartForm = "multipart/form-data" MIMEMultipartMixed = "multipart/mixed" MIMEOctetStream = "application/octet-stream" )
const ( HeaderContentType = "Content-Type" HeaderContentDisposition = "Content-Disposition" HeaderXRequestID = "X-Request-Id" HeaderVary = "Vary" HeaderOrigin = "Origin" HeaderLocation = "Location" HeaderAccessControlAllowOrigin = "Access-Control-Allow-Origin" HeaderAccessControlAllowCredentials = "Access-Control-Allow-Credentials" HeaderAccessControlExposeHeaders = "Access-Control-Expose-Headers" HeaderAccessControlRequestMethod = "Access-Control-Request-Method" HeaderAccessControlRequestHeaders = "Access-Control-Request-Headers" HeaderAccessControlAllowMethods = "Access-Control-Allow-Methods" HeaderAccessControlAllowHeaders = "Access-Control-Allow-Headers" HeaderAccessControlMaxAge = "Access-Control-Max-Age" )
Variables ¶
var ( DefaultShutdownTimeout = 10 * time.Second DefaultReadHeaderTimeout = 10 * time.Second ErrAlreadyStarted = errors.New("server started already") ErrListen = errors.New("listen") ListenerAddrContextKey = "listener_addr" )
var DefaultErrHandler = func(c *Context, err error) { var errResp *HandlerError if errors.As(err, &errResp) { c.SetStatus(errResp.Code).SendJSON(errResp) return } c.SetStatus(http.StatusInternalServerError).SendJSON(map[string]string{"message": err.Error()}) }
Functions ¶
Types ¶
type Context ¶ added in v0.1.1
type Context struct { Request *http.Request Response http.ResponseWriter // contains filtered or unexported fields }
func NewContext ¶ added in v0.1.1
func NewContext(w http.ResponseWriter, r *http.Request) *Context
func (*Context) Bind ¶ added in v0.1.11
Bind binds the request data to the provided struct based on content type and struct tags.
- The obj parameter must be a pointer.
func (*Context) SendBlob ¶ added in v0.2.0
SendBlob streams data from an io.Reader to the response.
- The caller is responsible for setting appropriate headers (e.g., Content-Type).
func (*Context) SendNoContent ¶ added in v0.1.9
SendNoContent always sends a 204 No Content response without body.
func (*Context) SendString ¶ added in v0.1.11
func (*Context) SendZip ¶ added in v0.2.0
SendZip sends files to the client as a zip file.
- If name is empty, defaults to "files.zip".
type HandlerError ¶ added in v0.1.11
func (HandlerError) Error ¶ added in v0.1.11
func (e HandlerError) Error() string
func (HandlerError) MarshalJSON ¶ added in v0.1.11
func (e HandlerError) MarshalJSON() ([]byte, error)
func (HandlerError) Unwrap ¶ added in v0.1.11
func (e HandlerError) Unwrap() error
type HandlerFunc ¶ added in v0.1.9
type Mux ¶ added in v0.1.1
type Mux struct {
// contains filtered or unexported fields
}
func (*Mux) ErrorHandler ¶ added in v0.1.9
ErrorHandler sets the handler for 500 Internal Server Error responses.
- If not set, it defaults to a generic error handler.
- Only usable for ada.HandlerFunc handlers.
func (*Mux) HandleFunc ¶ added in v0.1.1
func (*Mux) HandleFuncWildcard ¶ added in v0.2.0
func (m *Mux) HandleFuncWildcard(path string, handler http.HandlerFunc, middlewares ...func(next http.Handler) http.Handler)
HandleFuncWildcard is registering all paths under the given path.
func (*Mux) HandleWildcard ¶ added in v0.2.0
func (m *Mux) HandleWildcard(path string, handler http.Handler, middlewares ...func(next http.Handler) http.Handler)
HandleWildcard is registering all paths under the given path.
func (*Mux) HandleWithMethod ¶ added in v0.1.2
func (*Mux) NotFound ¶ added in v0.1.1
func (m *Mux) NotFound(handler http.HandlerFunc)
NotFound sets the handler for 404 Not Found responses.
- If not set, it defaults to http.NotFound.
func (*Mux) ServeHTTP ¶ added in v0.1.1
func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the http.Handler interface for Mux.
func (*Mux) Wrap ¶ added in v0.1.9
func (m *Mux) Wrap(handler HandlerFunc) func(http.ResponseWriter, *http.Request)
Wrap converts ada.HandlerFunc to http.HandlerFunc.
type Option ¶
type Option func(*option)
func WithLogger ¶
func WithShutdownTimeout ¶ added in v0.1.1
WithShutdownTimeout sets the shutdown timeout, default is 10 seconds.
type OptionStart ¶ added in v0.1.7
type OptionStart func(*optionStart)
func WithBaseContext ¶ added in v0.1.8
func WithBaseContext(ctx context.Context) OptionStart
WithBaseContext sets the base context, default is context.Background().
- Default is context.Background().
func WithContext ¶ added in v0.1.8
func WithContext(ctx context.Context) OptionStart
WithContext sets the context, usable for stopping the server.
- Same as StartWithContext's ctx
func WithHTTPServerFunc ¶ added in v0.1.8
func WithHTTPServerFunc(fn func(server *http.Server) *http.Server) OptionStart
func WithNetwork ¶ added in v0.1.1
func WithNetwork(network string) OptionStart
WithNetwork sets the network, default is "tcp".
func WithReadHeaderTimeout ¶ added in v0.1.11
func WithReadHeaderTimeout(d time.Duration) OptionStart
WithReadHeaderTimeout sets the ReadHeaderTimeout for the http.Server.
- Default is 5 seconds.
type Server ¶
type Server struct { *Mux // contains filtered or unexported fields }
func NewWithFunc ¶ added in v0.1.1
func (*Server) Start ¶
func (s *Server) Start(addr string, opts ...OptionStart) error
Start starts the server with the given address.
- If the server fails to start, an error will be returned.
func (*Server) StartWithContext ¶ added in v0.1.1
StartWithContext starts the server with the given context and address.
- If the context is canceled, the server will be stopped.
- If the server fails to start, an error will be returned.