{"id":24196750,"url":"https://github.com/pchchv/web","last_synced_at":"2025-10-10T06:13:59.691Z","repository":{"id":143197795,"uuid":"614317006","full_name":"pchchv/web","owner":"pchchv","description":"A microframework to build web apps","archived":false,"fork":false,"pushed_at":"2023-03-21T08:05:56.000Z","size":98,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-03T08:23:56.366Z","etag":null,"topics":["api-server","go","golang","golang-library","golang-module","golang-server","middleware","rest-api","router","web-framework"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pchchv.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":"2023-03-15T10:48:12.000Z","updated_at":"2023-03-31T08:25:20.000Z","dependencies_parsed_at":null,"dependency_job_id":"766b3101-82c0-45b6-9045-a9e337c07df9","html_url":"https://github.com/pchchv/web","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/pchchv/web","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pchchv%2Fweb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pchchv%2Fweb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pchchv%2Fweb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pchchv%2Fweb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pchchv","download_url":"https://codeload.github.com/pchchv/web/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pchchv%2Fweb/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279002963,"owners_count":26083487,"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","status":"online","status_checked_at":"2025-10-10T02:00:06.843Z","response_time":62,"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":["api-server","go","golang","golang-library","golang-module","golang-server","middleware","rest-api","router","web-framework"],"created_at":"2025-01-13T19:37:50.608Z","updated_at":"2025-10-10T06:13:59.662Z","avatar_url":"https://github.com/pchchv.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# **web** [![Go Report Card](https://goreportcard.com/badge/github.com/pchchv/web)](https://goreportcard.com/report/github.com/pchchv/web) [![Go Reference](https://pkg.go.dev/badge/github.com/pchchv/web.svg)](https://pkg.go.dev/github.com/pchchv/web) [![GitHub license](https://img.shields.io/github/license/pchchv/web.svg)](https://github.com/pchchv/web/blob/master/LICENSE)\n\nWeb is a minimalist router for [Go](https://golang.org) to create web applications (server-side) without third-party dependencies. Web will always be compatible with the standard Go library; HTTP handlers have the same signature as [http.HandlerFunc](https://golang.org/pkg/net/http/#HandlerFunc).\n\n## Router\n\nWeb has a simplified, linear path-matching router and supports [URI](https://developer.mozilla.org/en-US/docs/Glossary/URI) definition with the following patterns:\n\n1. `/api/users` - URI with no dynamic values\n2. `/api/users/:userID`\n   - URI with a named parameter, `userID`\n   - If TrailingSlash is true, a URI ending in '/' will be accepted, see [sample](https://github.com/pchchv/web#sample).\n3. `/api/users/:misc*`\n   - Named URI parameter `misc`, with a wildcard suffix '\\*'\n   - This matches everything after `/api/users`. e.g. `/api/users/a/b/c/d`\n\nIf there are multiple handlers corresponding to the same URI, the request will only be handled by the first encountered handler.\nRefer to [sample](https://github.com/pchchv/web#sample) to see how routes are configured. You can access the named URI parameters with the `Context` function.\n\nNote: Web Context **not** available inside special handlers.\n\n```golang\nfunc helloWorld(w http.ResponseWriter, r *http.Request) {\n\twctx := web.Context(r)\n\t// URI paramaters, map[string]string\n\tparams := wctx.Params()\n\t// route, the web.Route which is executing this request\n\troute := wctx.Route\n\tweb.R200(\n\t\tw,\n\t\tfmt.Sprintf(\n\t\t\t\"Route name: '%s', params: '%s'\",\n\t\t\troute.Name,\n\t\t\tparams,\n\t\t),\n\t)\n}\n```\n\n## Handler chaining\n\nHandler chaining allows to execute multiple handlers for a given route. Chaining execution can be set to run even after the handler has written a response to an HTTP request by setting `FallThroughPostResponse` to `true` (see [sample](https://github.com/pchchv/web/blob/master/cmd/main.go)).\n\n## Middleware\n\nWeb [middlware](https://godoc.org/github.com/pchchv/web#Middleware) allows you to wrap all routes with middleware as opposed to a handler chain. The router exposes the [Use](https://godoc.org/github.com/pchchv/web#Router.Use) and [UseOnSpecialHandlers](https://godoc.org/github.com/pchchv/web#Router.UseOnSpecialHandlers) methods to add Middleware to the router.\n\nNotFound and NotImplemented are considered `special` handlers. The `web.Context(r)` inside special handlers will return `nil`.\n\nYou can add any number of intermediate programs to the router, the execution order of the intermediate programs will be [LIFO](\u003chttps://en.wikipedia.org/wiki/Stack_(abstract_data_type)\u003e) (Last In First Out). E.g.:\n\n```golang\nfunc main() {\n\trouter.Use(accesslog.AccessLog, cors.CORS(nil))\n\trouter.Use(\u003cmore middleware\u003e)\n}\n```\n\nFirst **_CorsWrap_** will be executed, then **_AccessLog_**.\n\n## Error handling\n\nWeb context has 2 methods for [set](https://github.com/pchchv/web/blob/master/web.go) and [get](https://github.com/pchchv/web/blob/master/web.go) errors in the request context. This allows the Web to implement a single middleware where errors returned in the HTTP handler can be handled. [set error](https://github.com/pchchv/web/blob/master/cmd/main.go), [get error](https://github.com/pchchv/web/blob/master/cmd/main.go).\n\n## Helper functions\n\nWeb provides several helper functions. When using `Send` or `SendResponse` the response is wrapped in [response struct](https://github.com/pchchv/web/blob/master/responses.go) Web and serialized as JSON.\n\n```json\n{\n  \"data\": \"\u003cany valid JSON payload\u003e\",\n  \"status\": \"\u003cHTTP status code, of type integer\u003e\"\n}\n```\n\nUsing `SendError`, the response is wrapped in [error response struct](https://github.com/pchchv/web/blob/master/responses.go) Web and serialized as JSON.\n\n```json\n{\n  \"errors\": \"\u003cany valid JSON payload\u003e\",\n  \"status\": \"\u003cHTTP status code, of type integer\u003e\"\n}\n```\n\n## HTTPS ready\n\nThe HTTPS server can be easily started by providing a key and a cert file. You can also have both HTTP and HTTPS servers running side by side.\n\nStart HTTPS server\n\n```golang\ncfg := \u0026web.Config{\n\tPort: \"80\",\n\tHTTPSPort: \"443\",\n\tCertFile: \"/path/to/certfile\",\n\tKeyFile: \"/path/to/keyfile\",\n}\nrouter := web.NewRouter(cfg, routes()...)\nrouter.StartHTTPS()\n```\n\nStarting both HTTP \u0026 HTTPS server\n\n```golang\ncfg := \u0026web.Config{\n\tPort: \"80\",\n\tHTTPSPort: \"443\",\n\tCertFile: \"/path/to/certfile\",\n\tKeyFile: \"/path/to/keyfile\",\n}\n\nrouter := web.NewRouter(cfg, routes()...)\ngo router.StartHTTPS()\nrouter.Start()\n```\n\n## Graceful shutdown\n\nGraceful shutdown allows you to shut down the server without affecting live connections/clients connected to the server. Any new connection request after initiating the shutdown will be ignored.  \nSample:\n\n```golang\nfunc main() {\n\tosSig := make(chan os.Signal, 5)\n\n\tcfg := \u0026web.Config{\n\t\tHost:            \"\",\n\t\tPort:            \"8080\",\n\t\tReadTimeout:     15 * time.Second,\n\t\tWriteTimeout:    60 * time.Second,\n\t\tShutdownTimeout: 15 * time.Second,\n\t}\n\trouter := web.NewRouter(cfg, routes()...)\n\n\tgo func() {\n\t\t\u003c-osSig\n\t\t// Initiate HTTP server shutdown\n\t\terr := router.Shutdown()\n\t\tif err != nil {\n\t\t\tfmt.Println(err)\n\t\t\tos.Exit(1)\n\t\t} else {\n\t\t\tfmt.Println(\"shutdown complete\")\n\t\t\tos.Exit(0)\n\t\t}\n\n\t\t// If HTTPS server running\n\t\terr := router.ShutdownHTTPS()\n\t\tif err != nil {\n\t\t\tfmt.Println(err)\n\t\t\tos.Exit(1)\n\t\t} else {\n\t\t\tfmt.Println(\"shutdown complete\")\n\t\t\tos.Exit(0)\n\t\t}\n\t}()\n\n\tgo func(){\n\t\ttime.Sleep(time.Second*15)\n\t\tsignal.Notify(osSig, os.Interrupt, syscall.SIGTERM)\n\t}()\n\n\trouter.Start()\n}\n```\n\n## Logging\n\nWeb exposes a singleton \u0026 global scoped logger variable [LOGHANDLER](https://godoc.org/github.com/pchchv/web#Logger) with which you can plug in your custom logger by implementing the [Logger](https://godoc.org/github.com/pchchv/web#Logger) interface.\n\n### Configuring the default Logger\n\nThe default logger uses the standard Go `log.Logger` library with `os.Stdout` for debugging and information logs and `os.Stderr` for warnings, errors, fatal events as io.Writers by default. You can set io.Writer and also disable certain log types with `GlobalLoggerConfig(stdout, stderr, cfgs...)`.\n\n## Server-Sent Events\n\nMDN has very good documentation on what [SSE (Server-Sent Events)](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events) is.\n\n## Usage\n\nA fully functional sample is available [here](https://github.com/pchchv/web/blob/master/cmd/main.go).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpchchv%2Fweb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpchchv%2Fweb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpchchv%2Fweb/lists"}