Skip to content

Service Context manages components (such as Consumers, DB Connections, Configuration)

Notifications You must be signed in to change notification settings

viettranx/service-context

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Service-Context

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).

Examples

How to use it

1. Install it with this cmd:

go get -u github.com/viettranx/service-context

2. Define your component:

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
}

3. Use the component with Service-Context:

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
}

4. Run your code with ENV

go build -o app
SIMPLE_VALUE="Hello World" ./app

You will see Hello World on your console.

Why should I use the ServiceContext?

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!

About

Service Context manages components (such as Consumers, DB Connections, Configuration)

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages