Gorilla Mux + EdgeOne Pages
Powerful URL router with regex pattern matching, subrouters, and middleware support for Go.
cloud-functions/api.go
package main
import (
"github.com/gorilla/mux"
"net/http"
)
func main() {
r := mux.NewRouter()
r.Use(loggingMiddleware)
r.HandleFunc("/", welcome).Methods("GET")
r.HandleFunc("/health", health).Methods("GET")
// Todo CRUD subrouter
api := r.PathPrefix("/api/todos").Subrouter()
api.HandleFunc("", listTodos).Methods("GET")
api.HandleFunc("", createTodo).Methods("POST")
api.HandleFunc("/{id:[0-9]+}", getTodo).Methods("GET")
api.HandleFunc("/{id:[0-9]+}/toggle", toggleTodo).Methods("PATCH")
api.HandleFunc("/{id:[0-9]+}", deleteTodo).Methods("DELETE")
http.ListenAndServe(":9000", r)
}API Endpoints
GET/api/
Welcome page listing all available routes
GET/api/health
Health check endpoint returning service status
GET/api/api/todos
GET route returning all todos with total count
POST/api/api/todos
POST route with JSON body to create a new todo
Request Body:
{
"title": "Learn Gorilla Mux"
}GET/api/api/todos/1
Dynamic route parameter with mux.Vars(r)["id"]
PATCH/api/api/todos/1/toggle
Toggle todo completion status
DELETE/api/api/todos/3
Delete a todo by ID
Regex Matching
URL pattern matching with regex constraints on route variables
Subrouters
Nested subrouters for clean, modular route organization
Middleware Chain
Stack middleware for logging, auth, CORS, and more