servo

package module
v2.8.18 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2022 License: MIT Imports: 10 Imported by: 0

README

Go

servo for serving!

servo is a simple service initializer/finalizer

usage

implement Service interface and register your service

package broker

import (
	"context"

	"github.com/okian/servo"
)

type Broker struct {
}

func (b Broker) Name() string {
	// it MUST be unique otherwise Register will return error
	return "msg broker"
}

func (b Broker) Initialize(ctx context.Context) error {
	// setup broker and connect to server and if every when well return nil
	return nil
}

func (b Broker) Finalize() error {
	// cleanup and close connection
	return nil
}

func (b Broker) Healthy(ctx context.Context) (interface{}, error) {
	// check your connection and status first return value is optional report of
	// your service
	return struct {
		Status                            string
		AnswerToTheUltimateQuestionOfLife int
	}{
		Status:                            "OK",
		AnswerToTheUltimateQuestionOfLife: 42,
	}, nil
}

func (b Broker) Ready(ctx context.Context) (interface{}, error) {
	// Are you ready to serve? if so return optional report and nil for error
	return nil, nil
}

func init() {
	// order is for when you have services that depend on each other
	// servo will initialize services from smallest order to the biggest and
	// finalize will do it in opposite order
	// services with same order number will initiate concurrently
	if err := servo.Register(Broker{}, 20); err != nil {
		// you will get error when you have more then one service with same name
		panic(err)
	}
}

in main.go import the service and run initialize

package main

import (
	"context"
	"time"

	"github.com/okian/servo"
	// DONT FORGET THIS (needed for invoking init function)
	_ "github.com/okian/servo/example/broker"
)

func main() {
	ctx, cl := context.WithTimeout(context.Background(), time.Minute)
	defer cl()
	err := servo.Initialize(ctx)
	if err != nil {
		panic(err)
	}

	// do your things
	// ...

	err = servo.Finalize(ctx)
	if err != nil {
		panic(err)
	}
}

Documentation

Index

Constants

View Source
const (
	ALL report = iota
	NONE
	NOTNIL
)
View Source
const (
	Start runMode = 1 << iota
	Stop
)

Variables

View Source
var (
	ErrorTimeout        = errors.New("timeout")
	ErrorInitialized    = errors.New("services are already initialized")
	ErrorNotInitialized = errors.New("services are not initialized")
	ErrorCancelled      = errors.New("context canceled")
	ErrorFinalized      = errors.New("services are already finalized")
)

Functions

func Health

func Health(ctx context.Context) (map[string]interface{}, error)

func HealthHandler

func HealthHandler(w http.ResponseWriter, _ *http.Request)

func Initialize

func Initialize(ctx context.Context) func()

func ReadinessHandler

func ReadinessHandler(w http.ResponseWriter, _ *http.Request)

func Ready

func Ready(ctx context.Context) (map[string]interface{}, error)

func Register

func Register(service Service, order int)

Types

type Healthiness

type Healthiness interface {
	Healthy(context.Context) (interface{}, error)
}

type Readiness

type Readiness interface {
	Ready(context.Context) (interface{}, error)
}

type Service

type Service interface {
	Name() string
	Initialize(context.Context) error
	Finalize() error
}

Directories

Path Synopsis
sqs
internal
kv
mem
monitoring
vol
mem

Jump to

Keyboard shortcuts

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