Install
openclaw skills install ngamuxBuild and modify HTTP routes and middleware in Go with Ngamux, supporting dynamic paths, request parsing, JSON/form handling, and fluent response writing.
openclaw skills install ngamuxUse this skill when the user needs to:
mux.Get("/users/:id", getUserHandler)) including dynamic path segments (/users/{id}) and wildcards (/files/*filePath).mux.Use(authMiddleware) or apiGroup.Use(loggingMiddleware).req.Params("id")).req.Query("name", "Guest")).application/x-www-form-urlencoded or multipart/form-data (e.g., req.FormValue("username"), req.FormFile("image")).application/json request bodies into Go structs (e.g., req.JSON(&user)).ngamux.Res(rw).Status(http.StatusOK).Json(data)).ngamux.Res(rw).Status(http.StatusOK).Text("Hello, World!")).ngamux.Res(rw).Status(http.StatusOK).HTML("template.html", data)).ngamux.WithTrailingSlash()), setting the logging verbosity (ngamux.WithLogLevel(slog.LevelDebug)), or providing custom json.Marshal/json.Unmarshal functions for specific serialization needs./api/v1).ngamux provides methods like mux.Get(), mux.Post(), mux.Put(), mux.Delete(), mux.Patch(), mux.Head(), and mux.All() to register http.HandlerFuncs for specific HTTP methods and paths. It supports path parameters (e.g., /{id}) and wildcards (/*filePath). Routes are efficiently stored and matched using a tree-like structure, leveraging the mapping package for optimized key-value storage.mux.Use() method allows global middleware registration, while mux.Group() and mux.With() enable group-specific middlewares. Middlewares are MiddlewareFunc types that wrap http.HandlerFuncs, allowing for a chain of responsibility pattern. The WithMiddlewares utility from common.go handles the functional chaining.Request struct (wrapped *http.Request) offers convenience methods:
Req().Params(key string): Retrieves URL path parameters parsed during routing.Req().Query(key string, fallback ...string): Accesses URL query string parameters.Req().QueriesParser(data any): Automatically binds query parameters to a Go struct using query tags and reflection.Req().FormValue(key string): Gets form field values.Req().FormFile(key string, maxFileSize ...int64): Handles file uploads from multipart forms.Req().JSON(store any): Decodes JSON request bodies into a provided Go interface.Req().Locals(key any, value ...any): Manages request-scoped data in the context.Response struct (wrapped http.ResponseWriter) provides a fluent API for crafting responses:
Res(rw).Status(code int): Sets the HTTP status code.Res(rw).Text(data string): Sends text/plain content.Res(rw).JSON(data any): Sends application/json content, using the configured JSON marshaller.Res(rw).HTML(path string, data any): Renders HTML templates using html/template.Config options are set via ngamux.New() and functional options like ngamux.WithTrailingSlash() and ngamux.WithLogLevel(). Custom JSON marshalling/unmarshalling can be provided.mux.Group(path string) and mux.GroupFunc(path string, router func(mux *Ngamux)) methods allow hierarchical organization of routes, where child groups inherit path prefixes and can apply their own set of middlewares, leading to cleaner code organization and shared logic.