{"id":13653006,"url":"https://github.com/ungerik/go-rest","last_synced_at":"2025-04-14T13:32:47.866Z","repository":{"id":3927447,"uuid":"5017662","full_name":"ungerik/go-rest","owner":"ungerik","description":"A small and evil REST framework for Go","archived":false,"fork":false,"pushed_at":"2017-01-20T13:26:12.000Z","size":27,"stargazers_count":128,"open_issues_count":2,"forks_count":16,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-03-24T23:02:18.116Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://go.pkgdoc.org/github.com/ungerik/go-rest","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ungerik.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}},"created_at":"2012-07-13T10:02:15.000Z","updated_at":"2024-01-27T17:53:49.000Z","dependencies_parsed_at":"2022-09-09T16:41:10.222Z","dependency_job_id":null,"html_url":"https://github.com/ungerik/go-rest","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/ungerik%2Fgo-rest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ungerik%2Fgo-rest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ungerik%2Fgo-rest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ungerik%2Fgo-rest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ungerik","download_url":"https://codeload.github.com/ungerik/go-rest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248888610,"owners_count":21178088,"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":[],"created_at":"2024-08-02T02:01:04.794Z","updated_at":"2025-04-14T13:32:47.826Z","avatar_url":"https://github.com/ungerik.png","language":"Go","readme":"## go-rest A small and evil REST framework for Go\n\n### Reflection, Go structs, and JSON marshalling FTW!\n\n* go get github.com/ungerik/go-rest\n* import \"github.com/ungerik/go-rest\"\n* Documentation: http://go.pkgdoc.org/github.com/ungerik/go-rest\n* License: Public Domain\n\nDownload, build and run example:\n\n\tgo get github.com/ungerik/go-rest\n\tgo install github.com/ungerik/go-rest/example \u0026\u0026 example\n\nSmall?\n\nYes, the framework consists of only three functions:\nHandleGET, HandlePOST, RunServer.\n\nEvil?\n\nWell, this package can be considered bad design because\nHandleGET and HandlePOST use dynamic typing to hide 36 combinations\nof handler function types to make the interface _easy_ to use.\n36 static functions would have been more lines of code but\ndramatic _simpler_ in their individual implementations.\nSo simple in fact, that there wouldn't be a point in\nabstracting them away in an extra framework.\nSee this great talk about easy vs. simple:\nhttp://www.infoq.com/presentations/Simple-Made-Easy\nRob Pike may also dislike this approach:\nhttps://groups.google.com/d/msg/golang-nuts/z4T_n4MHbXM/jT9PoYc6I1IJ\nSo yes, this package can be called evil because it is an\nanti-pattern to all that is good and right about Go.\n\nWhy use it then? By maximizing dynamic code\nit is easy to use and reduces code.\nYes, that introduces some internal complexity,\nbut this complexity is still very low in absolute terms\nand thus easy to control and debug.\nThe complexity of the dynamic code also does not spill over\ninto the package users' code, because the arguments and\nresults of the handler functions must be static typed\nand can't be interface{}.\n\nNow let's have some fun:\n\nHandleGET uses a handler function that returns a struct or string\nto create the GET response. Structs will be marshalled as JSON,\nstrings will be used as body with auto-detected content type.\n\nFormat of GET handler:\n\n\tfunc([url.Values]) ([struct|*struct|string][, error]) {}\n\nExample:\n\n\ttype MyStruct struct {\n\t\tA in\n\t\tB string\n\t}\n\n\trest.HandleGET(\"/data.json\", func() *MyStruct {\n\t\treturn \u0026MyStruct{A: 1, B: \"Hello World\"}\n\t})\n\n\trest.HandleGET(\"/index.html\", func() string {\n\t\treturn \"\u003c!doctype html\u003e\u003cp\u003eHello World\"\n\t})\n\nThe GET handler function can optionally accept an url.Values argument\nand return an error as second result value that will be displayed as\n500 internal server error if not nil.\n\nExample:\n\n\trest.HandleGET(\"/data.json\", func(params url.Values) (string, error) {\n\t\tv := params.Get(\"value\")\n\t\tif v == \"\" {\n\t\t\treturn nil, errors.New(\"Expecting GET parameter 'value'\")\n\t\t}\n\t\treturn \"value = \" + v, nil\n\t})\n\nHandlePOST maps POST form data or a JSON document to a struct that is passed\nto the handler function. An error result from handler will be displayed\nas 500 internal server error message. An optional first string result\nwill be displayed as a 200 response body with auto-detected content type.\n\nFormat of POST handler:\n\n\tfunc([*struct|url.Values]) ([struct|*struct|string],[error]) {}\n\nExample:\n\n\trest.HandlePOST(\"/change-data\", func(data *MyStruct) (err error) {\n\t\t// save data\n\t\treturn err\n\t})\n\nBoth HandleGET and HandlePOST also accept one optional object argument.\nIn that case handler is interpreted as a method of the type of object\nand called accordingly.\n\nExample:\n\n\trest.HandleGET(\"/method-call\", (*myType).MethodName, myTypeObject)","funding_links":[],"categories":["`API Frameworks`","web框架","Servers","Web Frameworks","API Frameworks","Web框架","web框架`web 框架`","Utility"],"sub_categories":["Go","版本控制","HTTP Clients","Advanced Console UIs","实用程序/Miscellaneous","Utility/Miscellaneous","Fail injection","版本控制`版本控制相关库`","交流","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fungerik%2Fgo-rest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fungerik%2Fgo-rest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fungerik%2Fgo-rest/lists"}