Service Context runs as heart of our services, helps us manage components (such as Consumers, DB Connections, Configuration).
It offers:
- Logger component (using logrus).
- Manage ENV and Flag variables dynamically (with flagenv, godotenv).
- Output env and flag with .env formatted.
- Easily plug-and-play component (as plugin).
- Demo with simple GIN-HTTP
- Custom component
- Simple CLI (run service and output all env command)
go get -u github.com/viettranx/service-context
Component can be anything but implements this interface:
type Component interface {
ID() string
InitFlags()
Activate(ServiceContext) error
Stop() error
}
Simple demo custom component:
package main
import (
"flag"
sctx "github.com/viettranx/service-context"
)
type simpleComponent struct {
id string
value string
}
func NewSimpleComponent(id string) *simpleComponent {
return &simpleComponent{id: id}
}
func (s *simpleComponent) ID() string {
return s.id
}
func (s *simpleComponent) InitFlags() {
flag.StringVar(&s.value, "simple-value", "demo", "Value in string")
}
func (s *simpleComponent) Activate(_ sctx.ServiceContext) error {
return nil
}
func (s *simpleComponent) Stop() error {
return nil
}
func (s *simpleComponent) GetValue() string {
return s.value
}
package main
import (
"github.com/viettranx/service-context"
"log"
)
func main() {
const compId = "foo" // identity your component
// Init service-context, you can put components as much as you can
serviceCtx := sctx.NewServiceContext(
sctx.WithComponent(NewSimpleComponent(compId)),
)
// Load() will iterate registered components
// It does parse flags and make some configurations if you defined
// in Activate() method of the components
if err := serviceCtx.Load(); err != nil {
log.Fatal(err)
}
type CanGetValue interface {
GetValue() string
}
// Get the component from ServiceContext by its ID
comp := serviceCtx.MustGet(compId).(CanGetValue)
log.Println(comp.GetValue())
_ = serviceCtx.Stop() // can be omitted
}
go build -o app
SIMPLE_VALUE="Hello World" ./app
You will see Hello World
on your console.
When your service growths, a lot of components are required. More components more configurations and more ENVs to manage. That's why ServiceContext plays its role.
Service-Context also offers a helpful feature: output all ENV with dot-env (.env) formatted with:
serviceContext.Outenv()
. Please check this example.
Hope it helps and enjoy coding!