https://github.com/gontainer/gontainer
YAML-based Dependency Injection container for GO
https://github.com/gontainer/gontainer
concurrent-safe dependency-injection dependency-injection-container go golang
Last synced: 7 months ago
JSON representation
YAML-based Dependency Injection container for GO
- Host: GitHub
- URL: https://github.com/gontainer/gontainer
- Owner: gontainer
- License: mit
- Created: 2023-09-26T15:27:43.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-08T08:32:23.000Z (over 1 year ago)
- Last Synced: 2024-07-31T20:52:21.473Z (over 1 year ago)
- Topics: concurrent-safe, dependency-injection, dependency-injection-container, go, golang
- Language: Go
- Homepage:
- Size: 382 KB
- Stars: 15
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go-cn - gontainer/gontainer - based Dependency Injection container for GO. It supports dependencies' scopes, and auto-detection of circular dependencies. Gontainer is concurrent-safe. [![godoc][D]](https://godoc.org/github.com/gontainer/gontainer) (杂项 / 依赖注入)
- awesome-go - gontainer/gontainer - A YAML-based Dependency Injection container for GO. It supports dependencies' scopes, and auto-detection of circular dependencies. Gontainer is concurrent-safe. (Miscellaneous / Dependency Injection)
- awesome-go-plus - gontainer/gontainer - A YAML-based Dependency Injection container for GO. It supports dependencies' scopes, and auto-detection of circular dependencies. Gontainer is concurrent-safe.  (Miscellaneous / Dependency Injection)
- awesome-go - gontainer/gontainer - A YAML-based Dependency Injection container for GO. It supports dependencies' scopes, and auto-detection of circular dependencies. Gontainer is concurrent-safe. (Miscellaneous / Dependency Injection)
- awesome-go-with-stars - gontainer/gontainer - A YAML-based Dependency Injection container for GO. It supports dependencies' scopes, and auto-detection of circular dependencies. Gontainer is concurrent-safe. (Miscellaneous / Dependency Injection)
- awesome-go - gontainer/gontainer - A YAML-based Dependency Injection container for GO. It supports dependencies' scopes, and auto-detection of circular dependencies. Gontainer is concurrent-safe. (Miscellaneous / Dependency Injection)
- awesome-go - gontainer/gontainer - based Dependency Injection container for GO ☆`16` (Miscellaneous / Dependency Injection)
- awesome-go - gontainer/gontainer - A YAML-based Dependency Injection container for GO. It supports dependencies' scopes, and auto-detection of circular dependencies. Gontainer is concurrent-safe. (Miscellaneous / Dependency Injection)
- fucking-awesome-go - gontainer/gontainer - A YAML-based Dependency Injection container for GO. It supports dependencies' scopes, and auto-detection of circular dependencies. Gontainer is concurrent-safe. (Miscellaneous / Dependency Injection)
- awesome-go-cn - gontainer/gontainer - based Dependency Injection container for GO. It supports dependencies' scopes, and auto-detection of circular dependencies. Gontainer is concurrent-safe. [![godoc][D]](https://godoc.org/github.com/gontainer/gontainer) (杂项 / 依赖注入)
README
[](https://github.com/avelino/awesome-go)
[](https://pkg.go.dev/github.com/gontainer/gontainer)
[](https://github.com/gontainer/gontainer/actions?query=workflow%3ATests)
[](https://coveralls.io/github/gontainer/gontainer?branch=main)
[](https://goreportcard.com/report/github.com/gontainer/gontainer)
[](https://sonarcloud.io/summary/new_code?id=gontainer_gontainer)
# Gontainer
A Dependency Injection container for GO. Gontainer is concurrent-safe, supports scopes and offers hot swapping.
If 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.
Using the bootstrapping technique, Gontainer uses itself to compile its dependencies.
1. [Configuration](internal/gontainer)
2. [Usage](internal/cmd/runner_builder.go)
## Docs
1. Documentation
1. [Version](docs/VERSION.md)
2. [Meta](docs/META.md)
3. [Parameters](docs/PARAMETERS.md)
4. [Services](docs/SERVICES.md)
5. [Decorators](docs/DECORATORS.md)
2. [Interface](docs/INTERFACE.md)
## Installation
**homebrew**
```bash
brew install gontainer/homebrew-tap/gontainer
```
**go install**
```bash
go install github.com/gontainer/gontainer@latest
```
**Manual compilation**
```bash
git clone git@github.com:gontainer/gontainer.git
cd gontainer
GONTAINER_BINARY=/usr/local/bin/gontainer make
```
## TL;DR
**Describe dependencies**
Either YAML or GO
YAML
File `gontainer/gontainer.yaml`:
```yaml
meta:
pkg: "gontainer"
constructor: "New"
imports:
mypkg: "github.com/user/repo/pkg"
parameters:
appPort: '%envInt("APP_PORT", 9090)%' # get the port from the ENV variable if it exists, otherwise, use the default one
services:
endpointHelloWorld:
constructor: "mypkg.NewHelloWorld"
serveMux:
constructor: '"net/http".NewServeMux' # serveMux := http.NewServerMux()
calls: #
- [ "Handle", [ "/hello-world", "@endpointHelloWorld" ] ] # serveMux.Handle("/hello-world", gontainer.Get("endpointHelloWorld"))
server:
getter: "GetServer" # func (*gontainer) GetServer() (*http.Server, error) { ... }
must_getter: true # func (*gontainer) MustGetServer() *http.Server { ... }
type: '*"net/http".Server' #
value: '&"net/http".Server{}' # server := &http.Server{}
fields: #
Addr: ":%appPort%" # server.Addr = ":" + gontainer.GetParam("appPort")
Handler: "@serveMux" # server.Handler = gontainer.Get("serverMux")
```
**Compile it**
```bash
gontainer build -i gontainer/gontainer.yaml -o gontainer/container.go
# it can read multiple configuration files, e.g.
# gontainer build -i gontainer/gontainer.yaml -i gontainer/dev/\*.yaml -o gontainer/container.go
```
GO
File `gontainer/gontainer.go`:
```go
package gontainer
import (
"net/http"
"os"
"github.com/gontainer/gontainer-helpers/v3/container"
"github.com/user/repo/pkg"
)
type gontainer struct {
*container.SuperContainer
}
func (g *gontainer) MustGetServer() *http.Server {
raw, err := g.Get("server")
if err != nil {
panic(err)
}
return raw.(*http.Server)
}
func New() *gontainer {
sc := &gontainer{
SuperContainer: container.NewSuperContainer(),
}
sc.OverrideParam("serverAddr", container.NewDependencyProvider(func() string {
if v, ok := os.LookupEnv("APP_PORT"); ok {
return ":" + v
}
return ":9090"
}))
endpointHelloWorld := container.NewService()
endpointHelloWorld.SetConstructor(pkg.NewHelloWorld)
sc.OverrideService("endpointHelloWorld", endpointHelloWorld)
serveMux := container.NewService()
serveMux.SetConstructor(http.NewServeMux)
serveMux.AppendCall(
"Handle",
container.NewDependencyValue("/hello-world"),
container.NewDependencyService("endpointHelloWorld"),
)
sc.OverrideService("serveMux", serveMux)
server := container.NewService()
server.SetConstructor(func() *http.Server {
return &http.Server{}
})
server.SetField("Addr", container.NewDependencyProvider(func() (interface{}, error) {
return sc.GetParam("serverAddr")
}))
server.SetField("Handler", container.NewDependencyService("serveMux"))
sc.OverrideService("server", server)
return sc
}
```
**Voilà!**
File `main.go`:
```go
package main
import (
"github.com/user/repo/gontainer"
)
func main() {
c := gontainer.New()
s := c.MustGetServer()
err := s.ListenAndServe()
if err != nil {
panic(err)
}
}
```