gsvc

package module
v0.0.0-...-928f7a2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2020 License: MIT Imports: 1 Imported by: 0

README

gsvc

Collection of Golang packages to build amazing microservices ❤️

  • Simple
  • Easy to use

Installation

To install Gsvc package, you need to install Go and set your Go workspace first.

  1. The first need Go installed (version 1.15+ is required), then you can use the below Go command to install Gsvc.
$ go get -u github.com/rodsher/gsvc
  1. Import it in your code:
import "github.com/rodsher/gsvc"

Packages

  • logger - configurator for the Zap logger
  • metrics - configurator Prometheus metrics
  • tracer - configurator for the OpenTracing tracer using Jaeger Go client

Basic usage

package main

import (
	"github.com/rodsher/gsvc"
)

type MyComponent struct {
	Message string
}

func (c *MyComponent) Init() error {
	c.Message = "Hello, Gsvc!"

	return nil
}

func (c *MyComponent) Run() error {
	println(c.Message)

	return nil
}

func main() {
	app := gsvc.NewApp()
	app.Conn(
		[]gsvc.Component{
			&MyComponent{},
		},
		[]gsvc.AsyncComponent{},
	)
	app.Init()
	app.Run()
}

See other examples:

  • Async component
  • Metrics, logger and tracer components

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App struct {
	// contains filtered or unexported fields
}

App is a primary microservice abstraction.

func NewApp

func NewApp() *App

NewApp is a constructor for the App structure.

func (*App) Conn

func (a *App) Conn(components []Component, asyncComponents []AsyncComponent)

Conn connects louse coupled Components and AsyncComponents with an application. Later components will initialize and run/asyncRun in the order of place in a slice.

Example:

var application App

application.Conn(
	[]Component{
		logger.NewComponent(),
		config.NewComponent(),
	},
	[]AsyncComponent{
		metrics.NewComponent(),
	},
)

func (App) Init

func (a App) Init()

Init calls the Init() method for all Components and AsyncComponents. Components can return an error when something goes bad. Errors returned by components do not handling and crash the entire application. You should use recover() func to prevent the application crash.

Example:

var application App

application.Conn(

	[]Component{
		logger.NewComponent(),
		tracer.NewComponent(),
	},
	[]AsyncComponent{
		handler.NewComponent(),
		metrics.NewComponent(),
	},
)

application.Init()

Example with recovering:

var application App

defer func() {
		err := recover()
		if err != nil {
			fmt.Println("Microservice will not crash!")
		}
}()

application.Conn(

	[]Component{
		logger.NewComponent(),
		tracer.NewComponent(),
	},
	[]AsyncComponent{
		handler.NewComponent(),
		metrics.NewComponent(),
	},
)

application.Init() .

func (App) Run

func (a App) Run()

Run calls the Run() method for all Components and AsyncComponents. Components can return an error when something goes bad. Errors returned by components do not handling and crash the entire application. You should use recover() func to prevent the application crash.

Example:

var application App

application.Conn(

	[]Component{
		logger.NewComponent(),
		tracer.NewComponent(),
	},
	[]AsyncComponent{
		handler.NewComponent(),
		metrics.NewComponent(),
	},
)

application.Init() application.Run()

Example with recovering:

var application App

defer func() {
		err := recover()
		if err != nil {
			fmt.Println("Microservice will not crash!")
		}
}()

application.Conn(

	[]Component{
		logger.NewComponent(),
		tracer.NewComponent(),
	},
	[]AsyncComponent{
		handler.NewComponent(),
		metrics.NewComponent(),
	},
)

application.Init() application.Run() .

type AsyncComponent

type AsyncComponent interface {
	Initializer
	AsyncRunner
}

type AsyncRunner

type AsyncRunner interface {
	RunAsync(*sync.WaitGroup)
}

type Component

type Component interface {
	Initializer
	Runner
}

type Initializer

type Initializer interface {
	Init() error
}

type Runner

type Runner interface {
	Run() error
}

Directories

Path Synopsis
example
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL