Discover where the OpenFaaS REST API is defined and understand how it routes requests to the appropriate handlers.
REST APIRequest RoutingHTTP Endpoints
🔌 Where is the REST API Defined?
The OpenFaaS REST API is defined in the Gateway's HTTP server setup, using Go's standard HTTP package and custom routing logic to handle different types of requests.
API Definition Location:
•Main server setup in main.go
•Route handlers in handlers/ package
•Middleware configuration in middleware/ package
•API versioning and endpoint definitions
🏗️ API Structure Overview
The OpenFaaS API follows RESTful principles and is organized into logical groups based on functionality.
Function Management
• POST /system/functions
• GET /system/functions
• DELETE /system/functions/{name}
• PUT /system/functions/{name}
Function Invocation
• POST /function/{name}
• GET /function/{name}
• PUT /function/{name}
• DELETE /function/{name}
System Operations
• GET /system/info
• GET /system/namespaces
• GET /system/scale
• POST /system/scale
Health & Metrics
• GET /healthz
• GET /metrics
• GET /system/namespaces
• GET /system/scale
🛣️ How Does Request Routing Work?
The Gateway uses a sophisticated routing system that matches incoming HTTP requests to the appropriate handler functions based on URL patterns and HTTP methods.
Routing Process:
1.
Request Arrival
HTTP request reaches the Gateway server
2.
URL Parsing
Request URL is parsed to extract path and query parameters
3.
Route Matching
URL pattern is matched against registered routes
4.
Handler Selection
Appropriate handler function is selected
5.
Request Processing
Handler processes the request and generates response
💻 API Definition Code Example
Here's how the API routes are defined in the OpenFaaS Gateway codebase.
// Main API route setup
func setupRoutes(router *mux.Router, h *handlers.Handler) {
// Function management routes router.HandleFunc("/system/functions", h.ListFunctions).Methods("GET") router.HandleFunc("/system/functions", h.DeployFunction).Methods("POST") router.HandleFunc("/system/functions/{name}", h.DeleteFunction).Methods("DELETE")
// Function invocation routes router.HandleFunc("/function/{name}", h.InvokeFunction).Methods("POST", "GET", "PUT", "DELETE")
// System routes router.HandleFunc("/system/info", h.SystemInfo).Methods("GET") router.HandleFunc("/healthz", h.Health).Methods("GET") }
🔗 Middleware Integration
The API routes are enhanced with middleware that provides cross-cutting concerns like authentication, logging, and request validation.
Authentication Middleware
Validates API keys, JWT tokens, or other authentication mechanisms before processing requests.
Logging Middleware
Logs incoming requests, response times, and error conditions for monitoring and debugging.
CORS Middleware
Handles Cross-Origin Resource Sharing headers for web-based clients.
Rate Limiting
Prevents abuse by limiting the number of requests from a single client.
➡️ What's Next?
Now that you understand how the API is defined and routes requests, let's explore how the Gateway transforms and authenticates requests through middleware.