{"id":24083338,"url":"https://github.com/mitranim/goh","last_synced_at":"2025-07-01T03:33:56.853Z","repository":{"id":57569194,"uuid":"344134734","full_name":"mitranim/goh","owner":"mitranim","description":"Go Http Handlers. Utility types that represent a not-yet-sent HTTP response as a value (status, header, body) with NO added abstractions or interfaces.","archived":false,"fork":false,"pushed_at":"2023-03-27T09:58:19.000Z","size":33,"stargazers_count":2,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-24T06:44:38.618Z","etag":null,"topics":["golang","http-response"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/mitranim/goh","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mitranim.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-03-03T13:23:56.000Z","updated_at":"2023-08-01T09:08:57.000Z","dependencies_parsed_at":"2024-06-20T18:52:32.535Z","dependency_job_id":"a96d6517-5c2c-4a07-a1d3-8095d9d81732","html_url":"https://github.com/mitranim/goh","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitranim%2Fgoh","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitranim%2Fgoh/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitranim%2Fgoh/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mitranim%2Fgoh/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mitranim","download_url":"https://codeload.github.com/mitranim/goh/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240959115,"owners_count":19884911,"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":["golang","http-response"],"created_at":"2025-01-09T23:56:25.459Z","updated_at":"2025-02-27T01:17:19.825Z","avatar_url":"https://github.com/mitranim.png","language":"Go","readme":"## Overview\n\n`goh` = **Go** **H**TTP **H**andlers.\n\nUtility types that represent a not-yet-sent HTTP response as a value (status, header, body) with _no added abstractions_. All types implement `http.Hander`.\n\nRecommended in conjunction with [`github.com/mitranim/rout`](https://github.com/mitranim/rout): router with support for returning responses as `http.Handler`.\n\nSmall and dependency-free.\n\nSee API docs at https://pkg.go.dev/github.com/mitranim/goh.\n\n## Usage\n\n```golang\nimport \"github.com/mitranim/goh\"\n\ntype Val struct {\n  One int64 `json:\"one\"`\n  Two int64 `json:\"two\"`\n}\n\ntype ErrJson struct {\n  Error error `json:\"error\"`\n}\n\n// Simple string example.\nfunc handler(req *http.Request) http.Handler {\n  return goh.StringOk(`response text`)\n}\n\n// String example with status and headers.\nfunc handler(req *http.Request) http.Handler {\n  return goh.String{\n    Status: http.StatusCreated,\n    Header: http.Header{`Content-Type`: {`text/html`}},\n    Body:   `\u003cbody\u003eresponse text\u003c/body\u003e`,\n  }\n}\n\n// Simple JSON example.\nfunc handler(req *http.Request) http.Handler {\n  return goh.JsonOk(Val{10, 20})\n}\n\n// JSON example with custom error handler.\nfunc handler(req *http.Request) http.Handler {\n  return goh.Json{\n    Body: Val{10, 20},\n    ErrFunc: writeErrAsJson,\n  }\n}\n\n// You can customize the default error handler.\nfunc init() {\n  goh.HandleErr = writeErrAsJson\n}\n\n// Example custom error handler.\n// Should be provided to response types as `ErrFunc: writeErrAsJson`.\nfunc writeErrAsJson(\n  rew http.ResponseWriter, req *http.Request, err error, wrote bool,\n) {\n  if err == nil {\n    return\n  }\n\n  if !wrote {\n    rew.WriteHeader(http.StatusInternalServerError)\n    err := json.NewEncoder(rew).Encode(ErrJson{err})\n    if err != nil {\n      fmt.Fprintf(os.Stderr, \"secondary error while writing error response: %+v\\n\", err)\n      return\n    }\n  }\n\n  fmt.Fprintf(os.Stderr, \"error while writing HTTP response: %+v\\n\", err)\n}\n```\n\n## Changelog\n\n### `v0.1.11`\n\nBreaking:\n\n* Renamed `.MaybeHan` to `.HanOpt` in various types.\n\nAdded:\n\n* `File.ServedHTTP`.\n* `File.Exists`.\n* `File.Existing`.\n* `Dir.ServedHTTP`.\n* `Dir.Resolve`.\n* `Dir.Allow`.\n* `Dir.File`.\n* `HttpHandlerOpt` (implemented by `File` and `Dir`).\n\nFixed:\n\n* `File` no longer writes its `.Header` when the target file is not found.\n\n### `v0.1.10`\n\n`Json` and `Xml` now support pretty-printing via field `.Indent`.\n\n### `v0.1.9`\n\nCosmetic renaming and minor cleanup. Renamed `ErrHandlerDefault` → `HandleErr`, `ErrHandler` → `WriteErr`, tweaked argument order in `ErrFunc`, tweaked default error handling in `WriteErr`, tweaked error messages.\n\n### `v0.1.8`\n\nLexicon change: \"Res\" → \"Han\" for functions that return `http.Handler`.\n\nAdd `TryJsonBytes`.\n\n### `v0.1.7`\n\nAdded file-serving facilities:\n\n  * `File`\n  * `Dir`\n  * `Filter`\n  * `FilterFunc`\n  * `AllowDors`\n\nThis provides richer file-serving functionality than `net/http`, including the ability to serve paths from a folder selectively, or being able to \"try file\" and fall back on something else.\n\n### `v0.1.6`\n\n`Json.TryBytes` and `Xml.TryBytes` no longer panic on nil header.\n\n### `v0.1.5`\n\nAdded `Json.TryBytes` and `Xml.TryBytes` for pre-encoding static responses.\n\n### `v0.1.4`\n\nAdded `.Res()` methods for request → response signatures.\n\n### `v0.1.3`\n\nAdded `Err`, `Handler`, `Respond`.\n\n### `v0.1.2`\n\n`Redirect` no longer writes the HTTP status before invoking `http.Redirect`.\n\n### `v0.1.1`\n\nOptional support for `\u003c?xml?\u003e`.\n\n## License\n\nhttps://unlicense.org\n\n## Misc\n\nI'm receptive to suggestions. If this library _almost_ satisfies you but needs changes, open an issue or chat me up. Contacts: https://mitranim.com/#contacts\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmitranim%2Fgoh","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmitranim%2Fgoh","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmitranim%2Fgoh/lists"}