{"id":36668976,"url":"https://github.com/morikuni/yacm","last_synced_at":"2026-01-12T10:38:17.173Z","repository":{"id":57607187,"uuid":"52209963","full_name":"morikuni/yacm","owner":"morikuni","description":"yacm provides a way to build a http handlers with middlewares","archived":false,"fork":false,"pushed_at":"2017-05-09T14:41:25.000Z","size":62,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-06-20T11:48:58.400Z","etag":null,"topics":["go","http","middleware"],"latest_commit_sha":null,"homepage":"","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/morikuni.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":"2016-02-21T14:19:22.000Z","updated_at":"2017-04-24T11:18:11.000Z","dependencies_parsed_at":"2022-08-30T05:21:59.361Z","dependency_job_id":null,"html_url":"https://github.com/morikuni/yacm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/morikuni/yacm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morikuni%2Fyacm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morikuni%2Fyacm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morikuni%2Fyacm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morikuni%2Fyacm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/morikuni","download_url":"https://codeload.github.com/morikuni/yacm/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morikuni%2Fyacm/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28338768,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-12T06:09:07.588Z","status":"ssl_error","status_checked_at":"2026-01-12T06:05:18.301Z","response_time":98,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["go","http","middleware"],"created_at":"2026-01-12T10:38:16.565Z","updated_at":"2026-01-12T10:38:17.164Z","avatar_url":"https://github.com/morikuni.png","language":"Go","readme":"# yacm(八雲) - yet another composable middleware\n\n[![Build Status](https://travis-ci.org/morikuni/yacm.svg?branch=master)](https://travis-ci.org/morikuni/yacm)\n[![GoDoc](https://godoc.org/github.com/morikuni/yacm?status.svg)](https://godoc.org/github.com/morikuni/yacm)\n\nyacm provides a way to build a http handlers with middlewares.\n\n## Install\n\n```sh\ngo get github.com/morikuni/yacm\n```\n\n## Design\n\nA typical middleware stack is like this.\n\n```go\ntype Middleware func(http.Handler) http.Handler\n\n// from net/http\ntype Handler interface {\n\tServeHTTP(ResponseWriter, *Request)\n}\n```\n\nComposition rule is\n\n```\nMiddleware + Handler = Handler\n```\n\nand some libraries provide\n\n```\nMiddleware + Middleware = Middleware\n```\n\n```\n +----------------+\n |     Client     |\n +---+--------^---+\n     |        |\n  req|     res|\n     |        |\n +---v--------+---+ ----------+\n |   Middleware   |           |\n +---+--------^---+           |\n     |        |               |\n  req|     res|               |\n     |        |               |\n +---v--------+---+ -+        |\n |   Middleware   |  |        |Handler\n +---+--------^---+  |        |\n     |        |      |        |\n  req|     res|      |Handler |\n     |        |      |        |\n +---v--------+---+  |        |\n |    Handler     |  |        |\n +----------------+ -+ -------+\n```\n\nyacm's stack is this.\n\n```go\ntype Filter interface {\n\tWrapService(w http.ResponseWriter, r *http.Request, s Service) error\n}\n\ntype Service interface {\n\tTryServeHTTP(w http.ResponseWriter, r *http.Request) error\n}\n\ntype Catcher interface {\n\tCatchError(w http.ResponseWriter, r *http.Request, err error) error\n}\n\ntype Shutter interface {\n\tShutError(w http.ResponseWriter, r *http.Request, err error)\n}\n\n// from net/http\ntype Handler interface {\n\tServeHTTP(ResponseWriter, *Request)\n}\n```\n\n`Service` corresponds to `Handler`, and `Filter` corresponds to `Middleware`.\nThe difference is that these may return an error.\n\nComposition rule is\n\n```\nFilter  + Filter  = Filter\nFilter  + Service = Service\nCather  + Cather  = Cather\nCather  + Shutter = Shutter\nService + Shutter = Handler\n```\n\n```\n                   +----------------+\n                   |     Client     |\n                   +---+--------^---+\n                       |        |\n                    req|     res+----------+\n                       |        |          |\n       +------- +- +---v--------+---+      |  +---------------+ ----------+\n       |        |  |     Filter     +--+   +--+    Shutter    |           |\n       |        |  +---+--------^---+  |   |  +------------^--+           |\n       |        |      |        |      |   |               |              |\n       |  Filter|   req|     res|      |   |res         err|              |\n       |        |      |        |      |   |               |              |\n       |        |  +---v--------+---+  |   |  +------------+--+ -+        |\nService|        |  |     Filter     +--+   +--+    Catcher    |  |        |Shutter\n       |        +- +---+--------^---+  |   |  +------------^--+  |        |\n       |               |        |      |   |               |     |        |\n       |            req|     res|      |   +------+     err|     |Catcher |\n       |               |        |      |          |        |     |        |\n       |           +---v--------+---+  |      +---+--------+--+  |        |\n       |           |    Service     +--+------\u003e    Catcher    |  |        |\n       +---------- +----------------+   err   +---------------+ -+ -------+\n```\n\nYou can also use `Handler` as a `Service`, `Middleware` as a `Filter`.\n\n## Example\n\n```go\npackage main\n\nimport (\n\t\"errors\"\n\t\"log\"\n\t\"net/http\"\n\n\t\"github.com/morikuni/yacm\"\n)\n\nfunc Logging(h http.Handler) http.Handler {\n\treturn http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\tlog.Println(r.Method, r.URL)\n\t\th.ServeHTTP(w, r)\n\t})\n}\n\nvar ErrNotGet = errors.New(\"method is not GET\")\n\nfunc GetOnly(w http.ResponseWriter, r *http.Request, next yacm.Service) error {\n\tif r.Method != \"GET\" {\n\t\treturn ErrNotGet\n\t}\n\treturn next.TryServeHTTP(w, r)\n}\n\nfunc Catcher(w http.ResponseWriter, r *http.Request, err error) error {\n\tswitch err {\n\tcase ErrNotGet:\n\t\tw.WriteHeader(http.StatusMethodNotAllowed)\n\t\tw.Write([]byte(http.StatusText(http.StatusMethodNotAllowed)))\n\t\treturn nil\n\tdefault:\n\t\treturn err\n\t}\n}\n\nfunc main() {\n\tenv := yacm.NewEnv().\n\t\tAppendMiddlewares(Logging).\n\t\tAppendFilterFuncs(GetOnly).\n\t\tAppendCatcherFuncs(Catcher)\n\n\thttp.Handle(\"/hello\", env.ServeFunc(func(w http.ResponseWriter, r *http.Request) error {\n\t\tw.Write([]byte(\"hello\"))\n\t\treturn nil\n\t}))\n\thttp.Handle(\"/error\", env.ServeFunc(func(w http.ResponseWriter, r *http.Request) error {\n\t\t// Since this error will never be handled by Catcher,\n\t\t// it will be 500 internal server error by yacm.DefaultShutter\n\t\treturn errors.New(\"unknown error\")\n\t}))\n\n\thttp.ListenAndServe(\":8080\", nil)\n}\n```\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorikuni%2Fyacm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmorikuni%2Fyacm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorikuni%2Fyacm/lists"}