boot-go accentuate component-based development (CBD).
This is an opinionated view of writing modular and cohesive Go code. It emphasizes the separation of concerns by loosely coupled components, which communicate with each other via interfaces and events. The goal is to support writing maintainable code on the long run, while taking the little more complexity compared to the well-defined standard library into account.
boot-go provided features are:
- code decoupling
- configuration handling
- dependency injection
boot-go was primarily designed to build opinionated frameworks and bundle them as a stack. So every developer or company can choose to use the default stack, a shared stack or rather create a new one. Stacks should be build with one specific purpose in mind for building a microservice, ui application, web application, data analytics application and so on. As an example, a web application boot stack could contain a http server component, a sql database component, a logging and a web application framework.
Everything in boot-go starts with a component. They are key fundamental in the development and can be considered as an elementary build block. The essential concept is to get all the necessary components functioning with as less effort as possible. Therefore, components must always provide a default configuration, which uses the most common settings. As an example, a http server should always start using port 8080, unless the developer specifies it. Or a postgres component should try to connect to localhost:5432 when there is no database url provided.
A component should be fail tolerant, recoverable, agnostic and decent.
| Facet | Meaning | Example |
|---|---|---|
| fail tolerant | Don't stop processing on errors. | A http request can still be processed, even when the metrics server is not available anymore. |
| recoverable | Try to recover from errors. | A database component should try to reconnect after losing the connection. |
| agnostic | Behave the same in any environment. | A key-value store component should work on a local development machine the same way as in a containerized environment. |
| decent | Don't overload the developer with complexity. | Keep the interface and events as simple as possible. It's better to build three smaller but specific components then one general with increased complexity. Less is often more. |
The hello component is a very basic example. It contains no fields or provides any interface to interact with other components. The component will just print the 'Hello World' message to the console.
package main
import (
"github.com/boot-go/boot"
"log"
)
// init() registers a factory method, which creates a hello component.
func init() {
boot.Register(func() boot.Component {
return &hello{}
})
}
// hello is the simplest component.
type hello struct{}
// Init is the initializer of the component.
func (c *hello) Init() {
log.Printf("boot-go says > 'Hello World'\n")
}
// Start the example and exit after the component was completed.
func main() {
boot.Go()
}This example shows how components get wired automatically with dependency injection. The server component starts at :8080 by default, but the port is configurable by setting the environment variable HTTP_SERVER_PORT.
package main
import (
"github.com/boot-go/boot"
"github.com/boot-go/stack/server/httpcmp"
"io"
"net/http"
)
// init() registers a factory method, which creates a hello component
func init() {
boot.Register(func() boot.Component {
return &hello{}
})
}
// hello is a very simple http server example.
// It requires the Eventbus and the httpcmp.Server component. Both components
// are injected by the boot framework automatically
type hello struct {
Eventbus boot.EventBus `boot:"wire"`
Server httpcmp.Server `boot:"wire"`
}
// Init is the constructor of the component. The handler registration takes place here.
func (h *hello) Init() {
// Subscribe to the registration event
h.Eventbus.Subscribe(func(event httpcmp.InitializedEvent) {
h.Server.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
io.WriteString(writer, "boot-go says: 'Hello World'\n")
})
})
}
// Start the example and test with 'curl localhost:8080'
func main() {
boot.Go()
}Configuration values can also be automatically injected with arguments or environment variables at start time. The value from USER will be used in this example. If the argument --USER madpax is not set and the environment variable is not defined, it is possible to specify the reaction whether the execution should stop with a panic or continue with a warning.
package main
import (
"github.com/boot-go/boot"
"log"
)
// hello is still a simple component.
type hello struct{
Out string `boot:"config,key:USER,default:madjax"` // get the value from the argument list or environment variable. If no value could be determined, then use the default value `madjax`.
}
// init() registers a factory method, which creates a hello component and returns a reference to it.
func init() {
boot.Register(func() boot.Component {
return &hello{}
})
}
// Init is the initializer of the component.
func (c *hello) Init() {
log.Printf("boot-go says > 'Hello %s'\n", c.Out)
}
// Start the example and exit after the component was completed
func main() {
boot.Go()
}More examples can be found in the tutorial repository.