{"id":17201549,"url":"https://github.com/liquidthedangerous/commonhttp","last_synced_at":"2025-07-24T15:36:47.904Z","repository":{"id":257239647,"uuid":"857412026","full_name":"LiquidTheDangerous/commonhttp","owner":"LiquidTheDangerous","description":"This project provides functionality to register controllers for http.ServeMux or any other compatible multiplexers. It simplifies the process of organizing and managing HTTP handlers in your Go web applications.","archived":false,"fork":false,"pushed_at":"2024-09-15T15:20:15.000Z","size":23,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-30T08:25:29.124Z","etag":null,"topics":["go","golang","http","web"],"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/LiquidTheDangerous.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-14T15:34:30.000Z","updated_at":"2024-09-15T15:20:18.000Z","dependencies_parsed_at":"2024-12-03T01:24:14.352Z","dependency_job_id":"fc59811b-2378-474f-bc18-2dcd89086a4d","html_url":"https://github.com/LiquidTheDangerous/commonhttp","commit_stats":null,"previous_names":["liquidthedangerous/commonhttp"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiquidTheDangerous%2Fcommonhttp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiquidTheDangerous%2Fcommonhttp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiquidTheDangerous%2Fcommonhttp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LiquidTheDangerous%2Fcommonhttp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LiquidTheDangerous","download_url":"https://codeload.github.com/LiquidTheDangerous/commonhttp/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245431726,"owners_count":20614184,"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":["go","golang","http","web"],"created_at":"2024-10-15T02:12:07.977Z","updated_at":"2025-03-25T09:13:25.771Z","avatar_url":"https://github.com/LiquidTheDangerous.png","language":"Go","readme":"# Go HTTP Controller Registration\n\nThis project provides functionality to register controllers for http.ServeMux or any other compatible multiplexers.\nIt simplifies the process of organizing and managing HTTP handlers in your Go web applications.\n\n## Installation\n\n```bash\ngo get github.com/LiquidTheDangerous/commonhttp\n```\n\n## Usage\n\nHere’s a simple example of how to register controllers:\n\n```go\npackage main\n\nimport (\n\t\"io\"\n\t\"net/http\"\n\n\t\"github.com/LiquidTheDangerous/commonhttp/controller\"\n)\n\ntype ExampleController struct {\n}\n\nfunc (e *ExampleController) Routes() controller.Routes {\n\treturn []controller.RouteDef{controller.Route(\"GET\", \"/hello\", func(w http.ResponseWriter, r *http.Request) {\n\t\tio.WriteString(w, \"Hello, world\")\n\t})}\n}\n\nfunc main() {\n\tmux := http.NewServeMux()\n\tcontroller.MustRegisterController(mux, \u0026ExampleController{})\n\tif err := http.ListenAndServe(\":8080\", mux); err != nil {\n\t\tpanic(err)\n\t}\n}\n\n```\n\n### Creating a Controller\n\nYou can create a controller by implementing the Controller interface.\nThe handler for a Route can be a function or an object implementing http.Handler.\n\n```go\n\nvar handler http.Handler\nvar f func(w http.ResponseWriter, r *http.Request)\n...\nfunc (c *MyController) Routes() controller.Routes {\n    return controller.Routes{controller.Route(\"GET\", \"/handler\", handler), controller.Route(\"GET\", \"/function\", f)}\n}\n...\n```\n\n## Using gorilla mux\n\n```go\npackage main\n// create function, that registers route to gorilla router\nfunc RegisterGorillaMux(m any, handler http.Handler, route *controller.Route) error {\n    r, ok := m.(*mux.Router)\n    if !ok {\n        return fmt.Errorf(\"route %T is not a mux.Router\", m)\n    }\n    r.Handle(route.Pattern, handler).Methods(route.Method)\n    return nil\n}\n\nfunc main() {\n\tm := mux.NewRouter()\n\t// Provide this function via option `WithHandlerRegistrarFunc`\n    controller.MustRegisterController(m, \u0026MyController{}, controller.WithHandlerRegistrarFunc(RegisterGorillaMux))\n    if err := http.ListenAndServe(\":8080\", m); err != nil {\n        panic(err)\n    }\n}\n\n```\n\n## Contributing\n\nContributions are welcome! \nIf you have suggestions for improvements or find bugs, feel free to open an issue or submit a pull request.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliquidthedangerous%2Fcommonhttp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fliquidthedangerous%2Fcommonhttp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fliquidthedangerous%2Fcommonhttp/lists"}