{"id":16908799,"url":"https://github.com/hanjm/fasthttpmiddleware","last_synced_at":"2025-04-11T15:42:22.208Z","repository":{"id":57494682,"uuid":"91883471","full_name":"hanjm/fasthttpmiddleware","owner":"hanjm","description":"fasthttpmiddleware is a funny middleware onion for fasthttp.","archived":false,"fork":false,"pushed_at":"2017-05-25T02:32:00.000Z","size":50,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-25T11:52:18.042Z","etag":null,"topics":["fasthttp","go","http","middleware"],"latest_commit_sha":null,"homepage":"https://godoc.org/github.com/hanjm/fasthttpmiddleware","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hanjm.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-05-20T11:03:23.000Z","updated_at":"2023-09-04T06:32:38.000Z","dependencies_parsed_at":"2022-09-02T20:22:28.328Z","dependency_job_id":null,"html_url":"https://github.com/hanjm/fasthttpmiddleware","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hanjm%2Ffasthttpmiddleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hanjm%2Ffasthttpmiddleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hanjm%2Ffasthttpmiddleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hanjm%2Ffasthttpmiddleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hanjm","download_url":"https://codeload.github.com/hanjm/fasthttpmiddleware/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248433203,"owners_count":21102529,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fasthttp","go","http","middleware"],"created_at":"2024-10-13T18:53:01.946Z","updated_at":"2025-04-11T15:42:22.170Z","avatar_url":"https://github.com/hanjm.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"## A funny middleware onion for [fasthttp](github.com/valyala/fasthttp), inspired by [Alice](https://github.com/justinas/alice)\n[![GoDoc](https://godoc.org/github.com/hanjm/fasthttpmiddleware?status.svg)](https://godoc.org/github.com/hanjm/fasthttpmiddleware)\n[![Go Report Card](https://goreportcard.com/badge/github.com/hanjm/fasthttpmiddleware)](https://goreportcard.com/report/github.com/hanjm/fasthttpmiddleware)\n[![code-coverage](http://gocover.io/_badge/github.com/hanjm/fasthttpmiddleware)](http://gocover.io/github.com/hanjm/fasthttpmiddleware)\n\n### Example\n\n```go\npackage main\n\nimport (\n\t\"bytes\"\n\t\"github.com/buaazp/fasthttprouter\"\n\t\"github.com/hanjm/fasthttpmiddleware\"\n\t\"github.com/hanjm/zaplog\"\n\t\"github.com/valyala/fasthttp\"\n)\n\nfunc exampleAuthFunc(ctx *fasthttp.RequestCtx) bool {\n\tif bytes.HasPrefix(ctx.Path(), []byte(\"/protect\")) {\n\t\treturn false\n\t}\n\treturn true\n}\n\nfunc requestHandler(ctx *fasthttp.RequestCtx) {\n\tctx.WriteString(\"hello\")\n}\n\nfunc panicHandler(ctx *fasthttp.RequestCtx) {\n\tpanic(\"test panic\")\n}\n\nfunc main() {\n\tlogger := zaplog.NewNoCallerLogger(false)\n\tmo := fasthttpmiddleware.NewNormalMiddlewareOnion(exampleAuthFunc, logger)\n\tmoWithoutAuth := fasthttpmiddleware.NewMiddlewareOnion(\n\t\tfasthttpmiddleware.NewLogMiddleware(logger, false),\n\t\tfasthttpmiddleware.NewRecoverMiddleware(logger),\n\t)\n\trouter := fasthttprouter.New()\n\trouter.GET(\"/\", mo.Apply(requestHandler))\n\trouter.GET(\"/protect\", mo.Apply(requestHandler))\n\trouter.GET(\"/panic\", mo.Apply(panicHandler))\n\trouter.GET(\"/noAuth\", moWithoutAuth.Apply(requestHandler))\n\tfasthttp.ListenAndServe(\":8000\", router.Handler)\n}\n```\n\n### Document\n\n```go\ntype AuthFunc func(ctx *fasthttp.RequestCtx) bool\n    AuthFunc is your custom auth function type\n\ntype Middleware func(h fasthttp.RequestHandler) fasthttp.RequestHandler\n    Middleware is a function which receive a fasthttp.RequestHandler then\n    return a fasthttp.RequestHandler.\n\nfunc NewAuthMiddleware(authFunc AuthFunc) Middleware\n    NewAuthMiddleware accepts a customer auth function and then returns a\n    middleware which only accepts auth passed request. If auth function\n    returns false, it will term the HTTP request and response 403 status\n    code\n\nfunc NewLogMiddleware(logger *zap.Logger, xRealIp bool) Middleware\n    NewLogMiddleware returns a middleware which log code(status code),\n    time(response time), method(request method), path(request URL ath),\n    addr(remote address). if the status code is 2xx, the log level is info,\n    otherwise, the log level is warn. if your app is behind of Nginx, you\n    may meed to set xRealIp to True so that get an really remote address.\n\nfunc NewPrometheusMiddleware(bindAddr string, logger *zap.Logger) Middleware\n    NewPrometheusMiddleware return a middleware which can be used by\n    [prometheus](https://github.com/prometheus/prometheus) collecting\n    metrics. The prometheus is a monitoring system and time series database.\n\nfunc NewRecoverMiddleware(logger *zap.Logger) Middleware\n    NewRecoverMiddleware return a middleware which can let app recover from\n    a panic in request handler. panic stack info will appear on the field\n    named \"stacktrace\" in the log line\n\ntype MiddlewareOnion struct {\n    // contains filtered or unexported fields\n}\n    MiddlewareOnion represent the middleware like an onion, the bigger index\n    of middleware in MiddlewareOnion.layers locate at outside\n\nfunc NewMiddlewareOnion(middlewares ...Middleware) MiddlewareOnion\n    NewMiddlewareOnion returns a middleware onion with given middlewares\n\nfunc NewNormalMiddlewareOnion(authFunc AuthFunc, xRealIp bool, logger *zap.Logger) MiddlewareOnion\n    NewNormalMiddlewareOnion returns a normal middleware onion. recover -\u003e\n    auth -\u003e log. the type of AuthFunc is \"func(ctx *fasthttp.RequestCtx)\n    bool\". if your app is behind of Nginx, you may meed to set xRealIp to\n    True so that get an actual remote address.\n\nfunc (o MiddlewareOnion) Append(middlewares ...Middleware) MiddlewareOnion\n    Append copy all middleware layers to newLayers, then append middlewares\n    to newLayers, then return a new middleware onion.\n\nfunc (o MiddlewareOnion) Apply(h fasthttp.RequestHandler) fasthttp.RequestHandler\n    Apply apply the middleware onion to a fasthttp.RequestHandler\n\n\n\n\t\n\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhanjm%2Ffasthttpmiddleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhanjm%2Ffasthttpmiddleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhanjm%2Ffasthttpmiddleware/lists"}