{"id":21511984,"url":"https://github.com/strvcom/strv-backend-go-net","last_synced_at":"2025-04-09T18:21:10.265Z","repository":{"id":56854910,"uuid":"504135599","full_name":"strvcom/strv-backend-go-net","owner":"strvcom","description":"Go package facilitating writing API applications in a fast and easy manner.","archived":false,"fork":false,"pushed_at":"2024-11-14T13:03:30.000Z","size":111,"stargazers_count":6,"open_issues_count":1,"forks_count":0,"subscribers_count":19,"default_branch":"master","last_synced_at":"2025-03-23T20:22:03.232Z","etag":null,"topics":["go","golang","handler","http","net","server"],"latest_commit_sha":null,"homepage":"https://go.strv.io","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/strvcom.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-06-16T11:51:47.000Z","updated_at":"2024-11-14T13:02:43.000Z","dependencies_parsed_at":"2024-06-20T01:45:39.463Z","dependency_job_id":"03e8b2ce-f490-4fdc-a541-531c7f7e1399","html_url":"https://github.com/strvcom/strv-backend-go-net","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/strvcom%2Fstrv-backend-go-net","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/strvcom%2Fstrv-backend-go-net/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/strvcom%2Fstrv-backend-go-net/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/strvcom%2Fstrv-backend-go-net/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/strvcom","download_url":"https://codeload.github.com/strvcom/strv-backend-go-net/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248085452,"owners_count":21045164,"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","handler","http","net","server"],"created_at":"2024-11-23T22:24:46.896Z","updated_at":"2025-04-09T18:21:10.241Z","avatar_url":"https://github.com/strvcom.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# STRV net\n\n![Latest release][release]\n[![codecov][codecov-img]][codecov]\n![GitHub][license]\n\nGo package facilitating writing API applications in a fast and easy manner.\n\n## Available packages\n\n### errors\nDefinition of common errors.\n\n### net\nCommon functionality that comes in handy regardless of the used API architecture. `net` currently supports generating request IDs with some helper methods.\n\n### http\nWrapper around the Go native http server. `http` defines the `Server` that can be configured by the `ServerConfig`. Implemented features:\n- Started http server can be easily stopped by cancelling the context that is passed by the `Run` method.\n- The `Server` can be configured with a slog.Logger for logging important information during starting/ending of the server.\n- The `Server` listens for `SIGINT` and `SIGTERM` signals so it can be stopped by firing the signal.\n- By the `ServerConfig` can be configured functions to be called before the `Server` ends.\n\n`http` defines several helper consctructs:\n- Content types and headers which are frequently used by APIs.\n- Middlewares:\n\t- `RequestIDMiddleware` sets request id in to the context.\n\t- `RecoverMiddleware` recovers from panic and sets panic object into the response writer for logging.\n\t- `LoggingMiddleware` logs information about the request (method, path, status code, request id, duration of the request, error message and panic message).\n- Method `WriteResponse` for writing a http response and `WriteErrorResponse` for writing an error http response. Writing of responses can be configured by `ResponseOption`.\n\n## Examples\n### http\nStarting the server:\n```go\npackage main\n\nimport (\n\t...\n\n\thttpx \"go.strv.io/net/http\"\n)\n\nfunc main() {\n\t...\n\th := slog.NewJSONHandler(os.Stderr, \u0026slog.HandlerOptions{\n\t\tLevel: level,\n\t\tReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {\n\t\t\tif a.Key == slog.TimeKey {\n\t\t\t\ta.Value = slog.StringValue(a.Value.Time().Format(\"2006-01-02T15:04:05.000Z\"))\n\t\t\t}\n\t\t\treturn a\n\t\t},\n\t})\n\tl := slog.New(h)\n\tserverConfig := httpx.ServerConfig{\n\t\tAddr:    \":8080\",\n\t\tHandler: handler(), // define your http handler\n\t\tHooks: httpx.ServerHooks{\n\t\t\tBeforeShutdown: []httpx.ServerHookFunc{\n\t\t\t\tfunc(_ context.Context) {\n\t\t\t\t\tstorage.Close() // it may be useful for example to close a storage before the server ends\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t\tLimits: nil,\n\t\tLogger: l.WithGroup(\"httpx.Server\"), // the server expects *slog.Logger\n\t}\n\tserver := httpx.NewServer(\u0026serverConfig)\n\tif err = server.Start(ctx); err != nil {\n\t\tl.Error(\"HTTP server unexpectedly ended\", slog.Any(\"error\", err))\n\t}\n}\n```\n\nWriting http responses:\n```go\nfunc (h *Handler) GetUser(w http.ResponseWriter, r *http.Request) {\n\tuserID := userIDFromCtx(r.Context())\n\t\n\tuser, err := h.service.GetUser(r.Context(), userID)\n\tif err != nil {\n\t\t_ = httpx.WriteErrorResponse(w, http.StatusInternalServerError, httpx.WithErrorCode(\"ERR_UNKNOWN\"))\n\t\treturn\n\t}\n\t\n\tuserResp := model.ToUser(user)\n\t_ = httpx.WriteResponse(w, userResp, http.StatusOK)\n}\n```\n\n[release]: https://img.shields.io/github/v/release/strvcom/strv-backend-go-net\n[codecov]: https://codecov.io/gh/strvcom/strv-backend-go-net\n[codecov-img]: https://codecov.io/gh/strvcom/strv-backend-go-net/branch/master/graph/badge.svg?token=QI6YW1E4TC\n[license]: https://img.shields.io/github/license/strvcom/strv-backend-go-net\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstrvcom%2Fstrv-backend-go-net","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstrvcom%2Fstrv-backend-go-net","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstrvcom%2Fstrv-backend-go-net/lists"}