Documentation
¶
Index ¶
- type Config
- type Context
- func (c *Context) Abort()
- func (c *Context) AbortWithError(statusCode int, err error) error
- func (c *Context) BadRequest(message string) error
- func (c *Context) InternalServerError(originalErr error) error
- func (c *Context) JSON(status int, v interface{}) error
- func (c *Context) Next() error
- func (c *Context) NotFound(message string) error
- func (c *Context) Param(key string) string
- func (c *Context) QueryBool(key string, defaultValue bool) bool
- func (c *Context) QueryInt(key string, defaultValue int) int
- func (c *Context) QueryString(key string, defaultValue string) string
- func (c *Context) Render(status int, name string, data interface{}) error
- func (c *Context) StreamJSON(r io.Reader, v interface{}) error
- func (c *Context) String(status int, message string) error
- type HandlerFunc
- type Server
- func (s *Server) DELETE(path string, handler HandlerFunc)
- func (s *Server) GET(path string, handler HandlerFunc)
- func (s *Server) Handle(method, path string, handler HandlerFunc)
- func (s *Server) ListenAndServe(addr string) error
- func (s *Server) POST(path string, handler HandlerFunc)
- func (s *Server) PUT(path string, handler HandlerFunc)
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) Shutdown(ctx context.Context) error
- func (s *Server) Use(handlers ...HandlerFunc)
- type ServerArgs
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
ReadTimeout time.Duration
WriteTimeout time.Duration
IdleTimeout time.Duration
ReadHeaderTimeout time.Duration
MaxConns int
TrustedProxies []string
AllowedOrigins []string
AllowedMethods []string
AllowedHeaders []string
ExposedHeaders []string
AllowCredentials bool
MaxAge time.Duration
MaxRequestSize int64
EnableCompression bool
EnableSecurityHeaders bool
RateLimit int
BurstLimit int
RateWindow time.Duration
CleanupWindow time.Duration
Debug bool
// contains filtered or unexported fields
}
Config holds the server configuration parameters.
func DefaultConfig ¶
func DefaultConfig() *Config
DefaultConfig returns a Config struct with default values.
type Context ¶
type Context struct {
Request *http.Request
Response http.ResponseWriter
Params httprouter.Params
// contains filtered or unexported fields
}
Context represents the context of the current HTTP request.
func (*Context) Abort ¶
func (c *Context) Abort()
Abort prevents further handlers from being executed.
func (*Context) AbortWithError ¶
AbortWithError sends a JSON error response with the given status code and error, then aborts the context. If the provided error is nil, it uses the default HTTP status text for the given code. Otherwise, it uses the provided error's message. Care should be taken not to expose sensitive internal errors.
func (*Context) BadRequest ¶
BadRequest is a helper function to respond with a 400 Bad Request error. It calls AbortWithError internally. If message is empty, a default "Bad Request" message is used.
func (*Context) InternalServerError ¶
InternalServerError is a helper function to respond with a 500 Internal Server Error. It calls AbortWithError internally. If the provided err is nil, a default "Internal Server Error" message is used. IMPORTANT: In a production environment, the actual `err` should be logged server-side. Avoid exposing detailed internal error messages to the client.
func (*Context) NotFound ¶
NotFound is a helper function to respond with a 404 Not Found error. It calls AbortWithError internally. If message is empty, a default "Not Found" message is used.
func (*Context) QueryBool ¶
QueryBool returns the boolean value of the query parameter for the given key. If the key is not found or the value is not a valid boolean, it returns the defaultValue. It accepts "true", "1", "false", "0". Case-insensitive for "true" and "false".
func (*Context) QueryInt ¶
QueryInt returns the integer value of the query parameter for the given key. If the key is not found or the value is not a valid integer, it returns the defaultValue.
func (*Context) QueryString ¶
QueryString returns the value of the query parameter for the given key. If the key is not found, it returns the defaultValue.
func (*Context) Render ¶
Render executes the given HTML template with the provided data and writes it to the response. It sets the Content-Type to "text/html; charset=utf-8".
func (*Context) StreamJSON ¶
StreamJSON decodes JSON from the given io.Reader and stores it in the value pointed to by v.
type HandlerFunc ¶
HandlerFunc defines the handler function signature for middleware and route handlers.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is the main server struct.
func NewWithConfig ¶
NewWithConfig creates a new Server instance with the given configuration.
func (*Server) DELETE ¶
func (s *Server) DELETE(path string, handler HandlerFunc)
DELETE registers a new DELETE request handler with the given path.
func (*Server) GET ¶
func (s *Server) GET(path string, handler HandlerFunc)
GET registers a new GET request handler with the given path.
func (*Server) Handle ¶
func (s *Server) Handle(method, path string, handler HandlerFunc)
Handle registers a new request handler with the given method and path.
func (*Server) ListenAndServe ¶
ListenAndServe starts the server and listens for incoming connections on the specified address.
func (*Server) POST ¶
func (s *Server) POST(path string, handler HandlerFunc)
POST registers a new POST request handler with the given path.
func (*Server) PUT ¶
func (s *Server) PUT(path string, handler HandlerFunc)
PUT registers a new PUT request handler with the given path.
func (*Server) ServeHTTP ¶
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP makes the server implement the http.Handler interface.
func (*Server) Use ¶
func (s *Server) Use(handlers ...HandlerFunc)
Use adds middleware handlers to the server's middleware stack.
type ServerArgs ¶
ServerArgs holds command line arguments for the server
func ParseArgs ¶
func ParseArgs(arguments []string) (*ServerArgs, error)
ParseArgs parses command line arguments from the provided string slice.