{"id":13413447,"url":"https://github.com/gontainer/gontainer","last_synced_at":"2025-05-14T14:32:13.087Z","repository":{"id":196667247,"uuid":"696868635","full_name":"gontainer/gontainer","owner":"gontainer","description":"YAML-based Dependency Injection container for GO","archived":false,"fork":false,"pushed_at":"2024-07-08T08:32:23.000Z","size":391,"stargazers_count":15,"open_issues_count":5,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-07-31T20:52:21.473Z","etag":null,"topics":["concurrent-safe","dependency-injection","dependency-injection-container","go","golang"],"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/gontainer.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-09-26T15:27:43.000Z","updated_at":"2024-03-19T07:57:20.000Z","dependencies_parsed_at":"2023-10-16T11:48:39.625Z","dependency_job_id":"9b2819db-586c-4059-aac7-b20e9b27dd84","html_url":"https://github.com/gontainer/gontainer","commit_stats":null,"previous_names":["gontainer/gontainer"],"tags_count":48,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gontainer%2Fgontainer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gontainer%2Fgontainer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gontainer%2Fgontainer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gontainer%2Fgontainer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gontainer","download_url":"https://codeload.github.com/gontainer/gontainer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254160648,"owners_count":22024574,"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":["concurrent-safe","dependency-injection","dependency-injection-container","go","golang"],"created_at":"2024-07-30T20:01:40.612Z","updated_at":"2025-05-14T14:32:12.763Z","avatar_url":"https://github.com/gontainer.png","language":"Go","funding_links":[],"categories":["Miscellaneous","杂项","Microsoft Office"],"sub_categories":["Dependency Injection","依赖注入"],"readme":"[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go)\n[![Go Reference](https://pkg.go.dev/badge/github.com/gontainer/gontainer.svg)](https://pkg.go.dev/github.com/gontainer/gontainer)\n[![Build Status](https://github.com/gontainer/gontainer/actions/workflows/tests.yaml/badge.svg?branch=main)](https://github.com/gontainer/gontainer/actions?query=workflow%3ATests)\n[![Coverage Status](https://coveralls.io/repos/github/gontainer/gontainer/badge.svg?branch=main)](https://coveralls.io/github/gontainer/gontainer?branch=main)\n[![Go Report Card](https://goreportcard.com/badge/github.com/gontainer/gontainer)](https://goreportcard.com/report/github.com/gontainer/gontainer)\n[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=gontainer_gontainer\u0026metric=alert_status)](https://sonarcloud.io/summary/new_code?id=gontainer_gontainer)\n\n# Gontainer\n\nA Dependency Injection container for GO. Gontainer is concurrent-safe, supports scopes and offers hot swapping.\n\nIf the code generation is not for you, [see](https://github.com/gontainer/gontainer-helpers/blob/main/container/README.md) how to manually build a container.\n\nUsing the bootstrapping technique, Gontainer uses itself to compile its dependencies.\n1. [Configuration](internal/gontainer)\n2. [Usage](internal/cmd/runner_builder.go)\n\n## Docs\n\n1. Documentation\n   1. [Version](docs/VERSION.md)\n   2. [Meta](docs/META.md)\n   3. [Parameters](docs/PARAMETERS.md)\n   4. [Services](docs/SERVICES.md)\n   5. [Decorators](docs/DECORATORS.md)\n2. [Interface](docs/INTERFACE.md)\n\n## Installation\n\n**homebrew**\n\n```bash\nbrew install gontainer/homebrew-tap/gontainer\n```\n\n**go install**\n\n```bash\ngo install github.com/gontainer/gontainer@latest\n```\n\n**Manual compilation**\n\n```bash\ngit clone git@github.com:gontainer/gontainer.git\ncd gontainer\nGONTAINER_BINARY=/usr/local/bin/gontainer make\n```\n\n## TL;DR\n\n**Describe dependencies**\n\nEither YAML or GO\n\n\u003cdetails\u003e\n  \u003csummary\u003eYAML\u003c/summary\u003e\n\nFile `gontainer/gontainer.yaml`:\n\n```yaml\nmeta:\n  pkg: \"gontainer\"\n  constructor: \"New\"\n  imports:\n     mypkg: \"github.com/user/repo/pkg\"\n\nparameters:\n  appPort: '%envInt(\"APP_PORT\", 9090)%' # get the port from the ENV variable if it exists, otherwise, use the default one\n\nservices:\n  endpointHelloWorld:\n    constructor: \"mypkg.NewHelloWorld\"\n\n  serveMux:\n    constructor: '\"net/http\".NewServeMux'                       # serveMux := http.NewServerMux()\n    calls:                                                      #\n      - [ \"Handle\", [ \"/hello-world\", \"@endpointHelloWorld\" ] ] # serveMux.Handle(\"/hello-world\", gontainer.Get(\"endpointHelloWorld\"))\n\n  server:\n    getter: \"GetServer\"           # func (*gontainer) GetServer() (*http.Server, error) { ... }\n    must_getter: true             # func (*gontainer) MustGetServer() *http.Server { ... }\n    type: '*\"net/http\".Server'    # \n    value: '\u0026\"net/http\".Server{}' # server := \u0026http.Server{}\n    fields:                       #\n      Addr: \":%appPort%\"          # server.Addr = \":\" + gontainer.GetParam(\"appPort\")\n      Handler: \"@serveMux\"        # server.Handler = gontainer.Get(\"serverMux\")\n```\n\n**Compile it**\n\n```bash\ngontainer build -i gontainer/gontainer.yaml -o gontainer/container.go\n\n# it can read multiple configuration files, e.g.\n# gontainer build -i gontainer/gontainer.yaml -i gontainer/dev/\\*.yaml -o gontainer/container.go\n```\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eGO\u003c/summary\u003e\n\nFile `gontainer/gontainer.go`:\n\n```go\npackage gontainer\n\nimport (\n   \"net/http\"\n   \"os\"\n\n   \"github.com/gontainer/gontainer-helpers/v3/container\"\n   \"github.com/user/repo/pkg\"\n)\n\ntype gontainer struct {\n   *container.SuperContainer\n}\n\nfunc (g *gontainer) MustGetServer() *http.Server {\n   raw, err := g.Get(\"server\")\n   if err != nil {\n      panic(err)\n   }\n   return raw.(*http.Server)\n}\n\nfunc New() *gontainer {\n   sc := \u0026gontainer{\n      SuperContainer: container.NewSuperContainer(),\n   }\n\n   sc.OverrideParam(\"serverAddr\", container.NewDependencyProvider(func() string {\n      if v, ok := os.LookupEnv(\"APP_PORT\"); ok {\n         return \":\" + v\n      }\n      return \":9090\"\n   }))\n\n   endpointHelloWorld := container.NewService()\n   endpointHelloWorld.SetConstructor(pkg.NewHelloWorld)\n   sc.OverrideService(\"endpointHelloWorld\", endpointHelloWorld)\n\n   serveMux := container.NewService()\n   serveMux.SetConstructor(http.NewServeMux)\n   serveMux.AppendCall(\n      \"Handle\",\n      container.NewDependencyValue(\"/hello-world\"),\n      container.NewDependencyService(\"endpointHelloWorld\"),\n   )\n   sc.OverrideService(\"serveMux\", serveMux)\n\n   server := container.NewService()\n   server.SetConstructor(func() *http.Server {\n      return \u0026http.Server{}\n   })\n   server.SetField(\"Addr\", container.NewDependencyProvider(func() (interface{}, error) {\n      return sc.GetParam(\"serverAddr\")\n   }))\n   server.SetField(\"Handler\", container.NewDependencyService(\"serveMux\"))\n   sc.OverrideService(\"server\", server)\n\n   return sc\n}\n```\n\u003c/details\u003e\n\n**Voilà!**\n\nFile `main.go`:\n\n```go\npackage main\n\nimport (\n\t\"github.com/user/repo/gontainer\"\n)\n\nfunc main() {\n\tc := gontainer.New()\n\ts := c.MustGetServer()\n\n\terr := s.ListenAndServe()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgontainer%2Fgontainer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgontainer%2Fgontainer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgontainer%2Fgontainer/lists"}