{"id":41059485,"url":"https://github.com/marekgalovic/go-http-server","last_synced_at":"2026-01-22T11:41:48.274Z","repository":{"id":80307664,"uuid":"75120439","full_name":"marekgalovic/go-http-server","owner":"marekgalovic","description":"Wrapper around GO's native net/http library.","archived":false,"fork":false,"pushed_at":"2016-12-19T20:04:59.000Z","size":21,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-06-20T09:21:12.984Z","etag":null,"topics":["golang","http","server"],"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/marekgalovic.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":"2016-11-29T20:45:39.000Z","updated_at":"2021-02-12T21:42:20.000Z","dependencies_parsed_at":null,"dependency_job_id":"797bcb9d-b9ba-4260-8296-6f55269d3dcc","html_url":"https://github.com/marekgalovic/go-http-server","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/marekgalovic/go-http-server","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marekgalovic%2Fgo-http-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marekgalovic%2Fgo-http-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marekgalovic%2Fgo-http-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marekgalovic%2Fgo-http-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/marekgalovic","download_url":"https://codeload.github.com/marekgalovic/go-http-server/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/marekgalovic%2Fgo-http-server/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28662280,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-22T01:17:37.254Z","status":"online","status_checked_at":"2026-01-22T02:00:07.137Z","response_time":144,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["golang","http","server"],"created_at":"2026-01-22T11:41:47.534Z","updated_at":"2026-01-22T11:41:48.269Z","avatar_url":"https://github.com/marekgalovic.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Go http server [![Build Status](https://travis-ci.org/marekgalovic/go-http-server.svg?branch=master)](https://travis-ci.org/marekgalovic/go-http-server)\n\nThis package provides wrapper around GO's native `net/http` library. It allows you to define parametrized routes and authentication providers while keeping minimal footprint.\n\n## Getting started\nMinimal setup is easy. You need to create a `Config` object that allows you to modify properties of your server and then pass the config to a `NewServer` function.\n```go\nimport \"github.com/marekgalovic/go-http-server\"\n\nfunc main() {\n    config := server.NewConfig()\n    s := server.NewServer(config)\n\n    s.Get(\"/articles\", ListArticles, nil)\n\n    s.Listen()\n}\n```\n\n## Handler functions\nAs you can see above, every route defnition needs to be associated with a handler function. The handler function accepts only one parameter of type `Request`.\n\n```go\n// example handler function\nfunc ListArticles(request *server.Request) *server.Response {\n    articles := []*Article{\n        \u0026Article{Id: 1, Title: \"First\"},\n        \u0026Article{Id: 2, Title: \"Second\"},\n    }\n    return request.Response().Json(articles)\n}\n```\n\n## Parametrized routes\nThere are 4 methods that allow you to define your routes. A code example below shows how to define restful endpoinds for a resource. First parameter is a route of the resource, second parameter is a handler function and third parameter is an authentication provider.\n\n```go\n// example route definitions\ns.Get(\"/articles\", ListArticles, nil)\ns.Get(\"/article/:id\", ShowArticle, nil)\ns.Post(\"/article\", CreateArticle, nil)\ns.Put(\"/article/:id\", UpdateArticle, nil)\ns.Delete(\"/article/:id\", UpdateArticle, nil)\n```\n\n## Request\nRequest object exposes attributes about the request as well as defines methods to create responses. Available attributes are `Method (string)`, `Path (string)`, `Params (url.Values)`, `Header (http.Header)`, `RemoteAddr (string)`, `Body (io.ReadCloser)`. There is also a bunch of methods that you can use in your handlers.\n\n`Get` method to access request parameters.\n```go\n// example path definition /article/:id\n// request path /article/5?foo=bar\n\nid := request.Get(\":id\")\nfoo := request.Get(\"foo\")\n```\n\n`Json` method to read JSON encoded body to a struct.\n```go\nvar article Article\nrequest.Json(\u0026article)\n```\n\n`SetCookie` \u0026 `GetCookie` helpers to modify stored cookies.\n```go\nrequest.GetCookie(\"name\")\nrequest.SetCookie(\"name\", \"value\", 60 * time.Minute)\n```\n\n`Response` method returns a response`(*Response)` object associated with this request.\n```go\nrequest.Response()\n```\n\n## Response\n\n`Plain` method to respond with plain-text body.\n```go\nrequest.Response().Plain(\"Article id %d\", 1)\n```\n\n`Json` method to write JSON responses.\n```go\nrequest.Response().Json(map[string]string{\"foo\": \"bar\"})\n```\n\n`Error` to write error responses with specific code.\n```go\nrequest.Response().Error(404, \"Resource id: %d not found\", 2)\n```\n\n`ErrorJson` to write error responses with JSON body.\n```go\nrequest.Response().Error(500, map[string]string{\"message\": \"Unable to connect to database\"})\n```\n\n`File` to respond with a file. File path should be relative to `StaticRoot` defined in server config.\n```go\nrequest.Response().File(\"/path/to/my_file.pdf\")\n```\n\n`Redirect`\n```go\nrequest.Response().Redirect(301, \"http://google.com\")\n```\n\n`SetCode`\n```go\nrequest.Response().SetCode(500)\n```\n\n`SetHeader` to set a response headers.\n```go\nrequest.Response().SetHeader(\"Keep-Alive\", \"timeout=5\")\n```\n*Response methods support method chaining so you can use `request.Response().SetCode(404).Plain(\"Resource not found\")`*\n\n## Sessions\nAlthough this package doesn't implement sessions support directly, it provides a convinient wrapper around [gorilla/sessions](https://github.com/gorilla/sessions). To set a session store call `UseSessionStore` method on server object and provide a store instance that implements `sessions.Store` interface.\n```go\ns.UseSessionStore(sessions.NewCookieStore([]byte(\"your-secret-key\")))\n```\nAccessing session variables from your handler functions.\n```go\nsession, _ := request.Session(\"session_name\")\n// Get a value\nsession.Get(\"key\")\n// Set a value\nsession.Set(\"key\", \"value\")\n// Delete a value\nsession.Delete(\"key\")\n```\n`Set`, `Delete` will return a non nil error if there was an error while saving a session value. `request.Session` will return a non nil error if either no session store is set or there was an error while deserializing the session.\n\n## Authentication providers\nYou can create authentication provider to create authentication strategy that best fits your needs. There is only one method defined by `AuthProvider` interface called `Verify`. This method accepts `Request` object as a parameter and returns `Response` object if authentication fails and `nil` if authentication was successful.\n\nDefine an authentication provider\n```go\ntype MyAuthProvider struct {}\n\nfunc (auth *MyAuthProvider) Verify(request *server.Request) *server.Response {\n    if request.Get(\"auth_token\") == \"secret_token\" {\n        return nil\n    }\n    return request.Response().Error(401, \"Authentication failed\")\n}\n```\nUse the provider to protect your routes\n```go\nauth := \u0026MyAuthProvider{}\n\ns.Get(\"/articles\", ListArticles, nil) // public route\ns.Post(\"/article\", CreateArticle, auth) // protected route\n```\n\n## SSL support\nTo configure a secure server you need to provide a path to your certificate and key.\n```go\nconfig.CertFile = \"server.crt\"\nconfig.KeyFile = \"server.key\"\n```\n\n## Contributing\nBug reports and pull requests are welcome on GitHub at https://github.com/marekgalovic/go-http-server. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n\n## License\nThe package is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarekgalovic%2Fgo-http-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmarekgalovic%2Fgo-http-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmarekgalovic%2Fgo-http-server/lists"}