ngamux
Build and modify web services using ngamux, a simple HTTP router for Go. Define routes, apply middleware, handle requests, and send responses efficiently.
- License 许可
- MIT
- 最近更新
- 2026-06-07
- Stars
- ★ 0
- Forks
- 1
01 前言 Front Matter
- name
- ngamux
- description
- Build and modify web services using ngamux, a simple HTTP router for Go. Define routes, apply middleware, handle requests, and send responses efficiently.
03 SKILL.md 正文
When to use this skill
Use this skill when the user needs to:
- Define HTTP endpoints: Create new routes for various HTTP methods (e.g.,
mux.Get("/users/:id", getUserHandler)) including dynamic path segments (/users/{id}) and wildcards (/files/*filePath). - Implement request preprocessing/postprocessing: Add global middlewares (e.g., for authentication, logging, CORS) or group-specific middlewares to handle requests before they reach the main handler or after they are processed. For example,
mux.Use(authMiddleware)orapiGroup.Use(loggingMiddleware). - Extract incoming data from requests:
- URL Parameters: Retrieve values from dynamic path segments (e.g.,
req.Params("id")). - Query Parameters: Access query string values (e.g.,
req.Query("name", "Guest")). - Form Data: Parse
application/x-www-form-urlencodedormultipart/form-data(e.g.,req.FormValue("username"),req.FormFile("image")). - JSON Payloads: Decode
application/jsonrequest bodies into Go structs (e.g.,req.JSON(&user)).
- URL Parameters: Retrieve values from dynamic path segments (e.g.,
- Construct and send various response types:
- JSON Responses: Send structured data as JSON (e.g.,
ngamux.Res(rw).Status(http.StatusOK).Json(data)). - Text Responses: Send plain text (e.g.,
ngamux.Res(rw).Status(http.StatusOK).Text("Hello, World!")). - HTML Responses: Render HTML templates (e.g.,
ngamux.Res(rw).Status(http.StatusOK).HTML("template.html", data)). - Custom Status Codes: Set specific HTTP status codes for responses.
- JSON Responses: Send structured data as JSON (e.g.,
- Configure router behavior: Adjust global settings like automatically removing trailing slashes (
ngamux.WithTrailingSlash()), setting the logging verbosity (ngamux.WithLogLevel(slog.LevelDebug)), or providing customjson.Marshal/json.Unmarshalfunctions for specific serialization needs. - Organize routes with grouping: Create nested route groups to apply common path prefixes and middlewares to a set of routes (e.g.,
/api/v1). - Debug routing or handler issues: Inspect route definitions, middleware chains, request context, and logging output to diagnose and resolve problems.
Key Functionality
- Route Definition:
ngamuxprovides methods likemux.Get(),mux.Post(),mux.Put(),mux.Delete(),mux.Patch(),mux.Head(), andmux.All()to registerhttp.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 themappingpackage for optimized key-value storage. - Middleware Application: The
mux.Use()method allows global middleware registration, whilemux.Group()andmux.With()enable group-specific middlewares. Middlewares areMiddlewareFunctypes that wraphttp.HandlerFuncs, allowing for a chain of responsibility pattern. TheWithMiddlewaresutility fromcommon.gohandles the functional chaining. - Request Handling: The
Requeststruct (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 usingquerytags 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 Handling: The
Responsestruct (wrappedhttp.ResponseWriter) provides a fluent API for crafting responses:Res(rw).Status(code int): Sets the HTTP status code.Res(rw).Text(data string): Sendstext/plaincontent.Res(rw).JSON(data any): Sendsapplication/jsoncontent, using the configured JSON marshaller.Res(rw).HTML(path string, data any): Renders HTML templates usinghtml/template.
- Configuration: Global
Configoptions are set viangamux.New()and functional options likengamux.WithTrailingSlash()andngamux.WithLogLevel(). Custom JSON marshalling/unmarshalling can be provided. - Route Grouping: The
mux.Group(path string)andmux.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.
04 同仓库更多 Skills
crowd-prompting
A marketplace where AI agents improve prompts, system instructions, tool descriptions, and other text-based content with domain expertise from real-world operations — and earn tokens for valuable contributions.
ceaser
Interact with the Ceaser privacy protocol on Base L2 using the ceaser-mcp MCP tools. This skill uses the ceaser-mcp npm package for ALL operations -- shield, unshield, note management, and protocol queries. All ceaser tool calls use CLI subcommands (npx -y ceaser-mcp <subcommand>). Ceaser is a privacy-preserving ETH wrapper using Noir/UltraHonk zero-knowledge proofs on Base L2 (chain ID 8453).
ceaser-send
Fully automated private ETH transfer via Ceaser Protocol on Base L2 using the ceaser-mcp MCP tools. This skill uses the ceaser-mcp npm package for ALL operations -- all ceaser tool calls use CLI subcommands (npx -y ceaser-mcp <subcommand>). WARNING -- creates observable on-chain links between user wallet and hot wallet (see Privacy Warning). Generates an ephemeral hot wallet with BIP-39 mnemonic recovery, user funds it, agent signs and broadcasts the Shield TX automatically. Exactly one manual step required (funding the hot wallet with ETH). Uses Noir/UltraHonk zero-knowledge proofs.
dilemm-ai
Compete in DilemmAI, the prisoner's dilemma AI arena at dilemm.ai. Use when an OpenClaw agent wants to create an account, design and submit strategy prompts for their bot, enter matchmaking, analyze historical matches and leaderboards, and iterate on bot strategy. Requires headless browser automation (no API available yet).
moldium
Post and manage content on the Moldium blog platform. Triggered by "post to Moldium", "write a blog post", "publish an article", etc.
arxiv-paper-reviews
Interact with arXiv Crawler API to fetch papers, read reviews, submit comments, search papers, and import papers. Use when working with arXiv papers, fetching paper lists by date/category/interest, viewing paper details with comments, submitting paper reviews, searching papers by title, or importing papers from arXiv URLs via API at http://weakaccept.top:8000/.