Documentation
¶
Overview ¶
Package http provides simplified HTTP client and server utilities.
This package wraps Go's standard net/http and gorilla/mux packages, offering convenient functions for making HTTP requests and managing HTTP servers with routing capabilities.
Features:
- Simplified HTTP client with authentication support
- HTTP server with routing (powered by gorilla/mux)
- Handler and middleware registration
- Path prefix routing
- Graceful server shutdown
- Custom transport configuration
Example:
// Client
resp, _ := http.Request("http://api.example.com", http.MethodGet, nil, "", 10, "", "", nil)
// Server
var server http.Server
server.RegisterHandlerFunc("/api", handler)
server.Start(":8080", nil)
Package http provides simplified HTTP client and server utilities.
This package wraps Go's standard net/http and gorilla/mux packages, offering convenient functions for making HTTP requests and managing HTTP servers with routing capabilities.
Features:
- Simplified HTTP client with authentication support
- HTTP server with routing (powered by gorilla/mux)
- Handler and middleware registration
- Path prefix routing
- Graceful server shutdown
- Custom transport configuration
Example:
// Client
resp, _ := http.Request("http://api.example.com", http.MethodGet, nil, "", 10, "", "", nil)
// Server
var server http.Server
server.RegisterHandlerFunc("/api", handler)
server.Start(":8080", nil)
Index ¶
- type Response
- type Server
- func (s *Server) RegisterHandler(path string, handler http.Handler, methods ...string)
- func (s *Server) RegisterHandlerFunc(path string, handlerFunc http.HandlerFunc, methods ...string)
- func (s *Server) RegisterPathPrefixHandler(prefix string, handler http.Handler, methods ...string)
- func (s *Server) RegisterPathPrefixHandlerFunc(prefix string, handlerFunc http.HandlerFunc, methods ...string)
- func (s *Server) SetRouter(router *mux.Router)
- func (s *Server) Start(address string, listenAndServeFailureFunc func(err error), ...) error
- func (s *Server) Stop(shutdownTimeout time.Duration) error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Response ¶
Response is response information.
func Request ¶
func Request(url, method string, header map[string][]string, body string, timeout time.Duration, username, password string, transport *http.Transport) (Response, error)
Request performs an HTTP request and returns the response.
Parameters:
- url: Target URL for the HTTP request
- method: HTTP method (http.MethodGet, http.MethodPost, etc.)
- header: HTTP headers as a map of header names to value slices
- body: Request body as a string
- timeout: Request timeout in seconds
- username: Username for HTTP Basic Authentication (empty string for no auth)
- password: Password for HTTP Basic Authentication (empty string for no auth)
- transport: Custom HTTP transport configuration (nil for default)
Returns:
- Response: HTTP response containing headers, body, and status code
- error: Error if request fails, nil on success
The function creates an HTTP client with the specified timeout and transport settings, performs the request, and returns the complete response. The response body is read entirely into memory.
Example:
// Simple GET request
resp, err := http.Request(
"https://api.example.com/users",
http.MethodGet,
nil, // no headers
"", // no body
10, // 10 second timeout
"", // no username
"", // no password
nil, // default transport
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Status: %d\n", resp.StatusCode)
fmt.Printf("Body: %s\n", resp.Body)
// POST request with headers and authentication
headers := map[string][]string{
"Content-Type": {"application/json"},
"X-API-Key": {"secret123"},
}
body := `{"name": "Alice", "email": "alice@example.com"}`
resp, err = http.Request(
"https://api.example.com/users",
http.MethodPost,
headers,
body,
30,
"admin",
"password123",
nil,
)
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is a struct that provides server related methods.
func (*Server) RegisterHandler ¶ added in v1.1.8
RegisterHandler registers an HTTP handler for the specified path.
Parameters:
- path: URL path pattern to match (e.g., "/api/users")
- handler: HTTP handler to handle requests
- methods: Optional HTTP methods to restrict (e.g., http.MethodGet, http.MethodPost)
If no methods are specified, the handler accepts all HTTP methods. Multiple methods can be provided to restrict the handler to specific request types.
Example:
// Handle all methods
server.RegisterHandler("/api/health", healthHandler)
// Handle only GET requests
server.RegisterHandler("/api/users", usersHandler, http.MethodGet)
// Handle GET and POST requests
server.RegisterHandler("/api/items", itemsHandler, http.MethodGet, http.MethodPost)
func (*Server) RegisterHandlerFunc ¶ added in v1.1.6
func (s *Server) RegisterHandlerFunc(path string, handlerFunc http.HandlerFunc, methods ...string)
RegisterHandlerFunc registers an HTTP handler function for the specified path.
Parameters:
- path: URL path pattern to match (e.g., "/api/users")
- handlerFunc: HTTP handler function to handle requests
- methods: Optional HTTP methods to restrict (e.g., http.MethodGet, http.MethodPost)
This is a convenience method for registering handler functions directly without wrapping them in a Handler type. If no methods are specified, accepts all HTTP methods.
Example:
// Handle all methods
server.RegisterHandlerFunc("/api/ping", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("pong"))
})
// Handle only GET requests
server.RegisterHandlerFunc("/api/users", getUsersHandler, http.MethodGet)
// Handle GET and POST requests
server.RegisterHandlerFunc("/api/data", dataHandler, http.MethodGet, http.MethodPost)
func (*Server) RegisterPathPrefixHandler ¶ added in v1.1.6
RegisterPathPrefixHandler registers an HTTP handler for all paths matching the prefix.
Parameters:
- prefix: URL path prefix to match (e.g., "/static/", "/api/v1/")
- handler: HTTP handler to handle requests
- methods: Optional HTTP methods to restrict (e.g., http.MethodGet, http.MethodPost)
This method matches all paths that start with the specified prefix. Useful for serving static files or grouping related endpoints. If no methods are specified, accepts all HTTP methods.
Example:
// Serve static files (all methods)
fileServer := http.FileServer(http.Dir("./static"))
server.RegisterPathPrefixHandler("/static/", fileServer)
// API v1 endpoints (GET only)
server.RegisterPathPrefixHandler("/api/v1/", apiV1Handler, http.MethodGet)
// Admin endpoints (GET and POST)
server.RegisterPathPrefixHandler("/admin/", adminHandler, http.MethodGet, http.MethodPost)
func (*Server) RegisterPathPrefixHandlerFunc ¶ added in v1.1.8
func (s *Server) RegisterPathPrefixHandlerFunc(prefix string, handlerFunc http.HandlerFunc, methods ...string)
RegisterPathPrefixHandlerFunc registers an HTTP handler function for paths matching the prefix.
Parameters:
- prefix: URL path prefix to match (e.g., "/static/", "/api/v1/")
- handlerFunc: HTTP handler function to handle requests
- methods: Optional HTTP methods to restrict (e.g., http.MethodGet, http.MethodPost)
This is a convenience method for registering handler functions for path prefixes without wrapping them in a Handler type. Matches all paths starting with the prefix. If no methods are specified, accepts all HTTP methods.
Example:
// Handle all /api/ requests
server.RegisterPathPrefixHandlerFunc("/api/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"status": "ok"}`))
})
// Handle /files/ with GET only
server.RegisterPathPrefixHandlerFunc("/files/", filesHandler, http.MethodGet)
func (*Server) SetRouter ¶
SetRouter sets a custom router for the server.
Parameters:
- router: gorilla/mux router instance, or nil to reset
This method allows using a pre-configured router instead of the default one. Setting the router to nil will reset it, and a new router will be created on the next operation. This is useful for advanced routing configurations or testing.
Example:
// Use custom router
router := mux.NewRouter()
router.StrictSlash(true)
router.HandleFunc("/", homeHandler)
var server http.Server
server.SetRouter(router)
server.Start(":8080", nil)
// Reset router
server.SetRouter(nil)
func (*Server) Start ¶
func (s *Server) Start(address string, listenAndServeFailureFunc func(err error), middlewareFunc ...mux.MiddlewareFunc) error
Start starts the HTTP server on the specified address.
Parameters:
- address: Address to bind the server to (e.g., ":8080", "localhost:8080")
- listenAndServeFailureFunc: Optional callback function called if server fails to start
- middlewareFunc: Optional middleware functions to apply to all requests
Returns:
- error: Always returns nil (errors are reported via listenAndServeFailureFunc)
The server starts in a background goroutine, so this method returns immediately. If no middleware is provided, a pass-through middleware is added. Multiple middleware functions are executed in the order provided.
Example:
var server http.Server
server.RegisterHandlerFunc("/api", apiHandler)
// Start without middleware
err := server.Start(":8080", func(err error) {
log.Fatal(err)
})
// Start with logging middleware
loggingMiddleware := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s", r.Method, r.URL.Path)
next.ServeHTTP(w, r)
})
}
err = server.Start(":8080", nil, loggingMiddleware)
func (*Server) Stop ¶
Stop gracefully shuts down the HTTP server.
Parameters:
- shutdownTimeout: Maximum duration in seconds to wait for active connections to complete
Returns:
- error: Error if shutdown fails or times out, nil on successful shutdown
The server stops accepting new connections immediately and waits for active connections to complete within the timeout period. After the timeout, remaining connections are forcefully closed. The router is reset to nil after shutdown.
Example:
var server http.Server
server.Start(":8080", nil)
// Later, graceful shutdown with 30 second timeout
err := server.Stop(30)
if err != nil {
log.Printf("Shutdown error: %v", err)
}
log.Println("Server stopped gracefully")