{"id":29935016,"url":"https://github.com/enrichman/httpgrace","last_synced_at":"2025-08-02T20:09:02.376Z","repository":{"id":296066232,"uuid":"991617890","full_name":"enrichman/httpgrace","owner":"enrichman","description":"Go `net/http` wrapper with graceful shutdown baked in.","archived":false,"fork":false,"pushed_at":"2025-07-14T19:26:33.000Z","size":14,"stargazers_count":135,"open_issues_count":0,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-14T23:53:48.393Z","etag":null,"topics":["go","golang","golang-library","golang-package","graceful-shutdown","http","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/enrichman.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,"zenodo":null}},"created_at":"2025-05-27T22:45:43.000Z","updated_at":"2025-07-14T19:24:26.000Z","dependencies_parsed_at":"2025-05-28T21:21:15.870Z","dependency_job_id":"06686d68-e58b-4295-af37-c08161207caa","html_url":"https://github.com/enrichman/httpgrace","commit_stats":null,"previous_names":["enrichman/httpgrace"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/enrichman/httpgrace","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enrichman%2Fhttpgrace","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enrichman%2Fhttpgrace/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enrichman%2Fhttpgrace/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enrichman%2Fhttpgrace/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/enrichman","download_url":"https://codeload.github.com/enrichman/httpgrace/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/enrichman%2Fhttpgrace/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268448222,"owners_count":24251999,"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-08-02T02:00:12.353Z","response_time":74,"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":["go","golang","golang-library","golang-package","graceful-shutdown","http","http-server"],"created_at":"2025-08-02T20:09:00.938Z","updated_at":"2025-08-02T20:09:02.367Z","avatar_url":"https://github.com/enrichman.png","language":"Go","readme":"# httpgrace\n\n`httpgrace` is a minimal Go library providing drop-in replacements for the `net/http` standard library functions to run HTTP/HTTPS servers with graceful shutdown support out of the box.\n\n## Installation and Usage\n\nJust `go get github.com/enrichman/httpgrace` and replace `http` with `httpgrace` in your existing code:\n\n```go\n// Before\nhttp.ListenAndServe(\":8080\", handler)\n\n// After  \nhttpgrace.ListenAndServe(\":8080\", handler)\n```\n\nThat's it! Your server now gracefully shuts down on SIGINT/SIGTERM signals.\n\n## Features\n\n- API compatible with `net/http`'s `ListenAndServe`, `ListenAndServeTLS`, `Serve`, and `ServeTLS`  \n- Graceful shutdown on `SIGINT`/`SIGTERM` signals  \n- Configurable shutdown timeout (default 10s)  \n- Built-in structured logging via Go's `slog` package  \n- Minimal and dead-simple to integrate — just swap your import path!\n\n## API Reference\n\n### Simple Functions (Drop-in Replacements)\n\n```go\n// HTTP server\nhttpgrace.ListenAndServe(addr, handler, opts...)\n\n// HTTPS server  \nhttpgrace.ListenAndServeTLS(addr, certFile, keyFile, handler, opts...)\n\n// Custom listener\nhttpgrace.Serve(listener, handler, opts...)\nhttpgrace.ServeTLS(listener, certFile, keyFile, handler, opts...)\n```\n\n### Server Struct\n\nIf you need more granular control, you can use the Server struct directly:\n\n```go\nsrv := httpgrace.NewServer(handler)\nif err := srv.ListenAndServe(\":8080\"); err != nil {\n    log.Fatal(err)\n}\n```\n\n## Configuration Options\n\n### Shutdown Options\n\n```go\n// Set graceful shutdown timeout (default: 10 seconds)\nhttpgrace.WithTimeout(5*time.Second)\n\n// Customize shutdown signals (default: SIGINT, SIGTERM)\nhttpgrace.WithSignals(syscall.SIGTERM, syscall.SIGUSR1)\n\n// Provide custom logger (default: slog.Default())\nhttpgrace.WithLogger(customLogger)\n\n// Provide a function to run before shutdown\nhttpgrace.WithBeforeShutdown(func() {\n    time.Sleep(5 * time.Second)\n})\n```\n\n### Server Options\n\nYou can configure the underlying http.Server with the provided functions or custom ones:\n\n```go\nsrv := httpgrace.NewServer(handler,\n    httpgrace.WithServerOptions(\n        httpgrace.WithReadTimeout(10*time.Second),\n        httpgrace.WithWriteTimeout(10*time.Second),\n        httpgrace.WithIdleTimeout(120*time.Second),\n        // or with your custom ServerOption\n        func(srv *http.Server) {\n            srv.ErrorLog = log.New(os.Stdout, \"\", 0)\n        },\n    ),\n)\n\n// Start server\nif err := srv.ListenAndServe(\":8080\"); err != nil {\n    log.Fatal(err)\n}\n```\n\n## Graceful Shutdown Behavior\n\n`httpgrace` listens for `SIGINT` and `SIGTERM` signals. Upon receiving one, it stops accepting new connections and waits up to the configured shutdown timeout for active connections to finish before exiting.\n\nThis ensures your server shuts down cleanly without dropping in-flight requests abruptly.\n\n## Logging\n\n`httpgrace` logs key events such as server startup and shutdown progress using Go's `slog` package. By default, logs are output using `slog.Default()`. You can provide a custom logger with `WithLogger`.\n\nExample log messages:\n\n``` \ntime=2025-05-28T22:14:21.301+02:00 level=INFO msg=\"starting server\" mode=HTTP addr=[::]:8080 shutdown_timeout=10s\ntime=2025-05-28T22:14:28.258+02:00 level=INFO msg=\"shutdown signal received\" signal=interrupt\ntime=2025-05-28T22:14:28.258+02:00 level=INFO msg=\"server shutdown completed gracefully\" duration=204.273µs\n```\n\n## Requirements\n\n- Go 1.21+ (for slog package support)\n- No external dependencies!\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n## License\n\n[MIT License](./LICENSE) © Enrico Candino\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenrichman%2Fhttpgrace","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fenrichman%2Fhttpgrace","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fenrichman%2Fhttpgrace/lists"}