core

package module
v0.23.0 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2020 License: Apache-2.0 Imports: 9 Imported by: 0

README

License Go Report Card Build Status Documentation

Donguri by Cerys Evans Core (Go)

A collection of packages for building a Go microservice on the LUSH platform.

Quick start

Below there's an example for how to get running quickly with a service using the LUSHDigital core package.

package main

import (
	"context"
	"net/http"
	"time"

	"github.com/LUSHDigital/core"
	"github.com/LUSHDigital/core/middleware/metricsmw"
	"github.com/LUSHDigital/core/workers/httpsrv"
	"github.com/LUSHDigital/core/workers/keybroker"
	"github.com/LUSHDigital/core/workers/metricsrv"
	"github.com/LUSHDigital/core/workers/readysrv"
)

var service = &core.Service{
	Name:    "example",
	Type:    "service",
	Version: "1.0.0",
}

func main() {
	metrics := metricsrv.New(nil)
	broker := keybroker.NewPublicRSA(nil)
	readiness := readysrv.New(nil, readysrv.Checks{
		"public_rsa_key": broker,
	})

	handler := metricsmw.MeasureRequests(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
		w.WriteHeader(200)
		w.Write([]byte("hello world"))
	}))

	server := httpsrv.New(&http.Server{
		ReadTimeout: 10 * time.Second,
		Handler:     handler,
	})

	service.MustRun(context.Background(),
		server,
		metrics,
		broker,
		readiness,
	)
}

Documentation

Documentation and examples are provided in README files in each package.

Core Concepts

These packages contain functionality for the core concepts of our services.

Middlewares

These packages contain convenient middlewares for transport protocols like HTTP REST and gRPC.

Workers

These packages contain convenient workers things like network servers, background workers and message brokers.

Plugins

There are a few libraries that can be used in conjunction with the core library containing their own service workers, ready checks and/or middlewares.

Tools

There are a few tools that can be used with projects that use the core library.

Some libraries have been designed to work together with the core library and some are even dependencies. Consider using these if you need extended functionality for certain things.

  • LUSHDigital/scan: Scan database/sql rows directly to a struct, slice, or primitive any type. Originally forked from github.com/blockloop/scan
  • LUSHDigital/uuid: A UUID package originally forked from github.com/gofrs/uuid & github.com/satori/go.uuid
  • LUSHDigital/spew: A pretty-printer package originally forked from github.com/davecgh/go-spew/spew

Contributors

Documentation

Overview

Package core consists of a set of packages which are used in writing micro-service applications.

Each package defines conventional ways of handling common tasks, as well as a suite of tests to verify their behaviour.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithSignals added in v0.21.0

func ContextWithSignals(ctx context.Context) (context.Context, <-chan int, context.CancelFunc)

ContextWithSignals creates a new instance of signal context.

func SetupLogs

func SetupLogs()

SetupLogs will set up the appropriate log flags. DEPRECATED: Import github.com/LUSHDigital/core-lush/lushlog as a side-effect.

func WaitWithTimeout added in v0.21.0

func WaitWithTimeout(delta int, cancelled <-chan int, timeout time.Duration) (<-chan int, <-chan int, func())

WaitWithTimeout will wait for a number of pieces of work has finished and send a message on the completed channel.

Types

type Halter added in v0.21.0

type Halter interface {
	// Halt should tell the worker to stop doing work.
	Halt(context.Context) error
}

Halter represents the behaviour for stopping a service worker.

type Runner added in v0.21.0

type Runner interface {
	// Run should run start processing the worker and be a blocking operation.
	Run(context.Context) error
}

Runner represents the behaviour for running a service worker.

type Service

type Service struct {
	// Name represents the name of the service, typically the same as the github repository.
	Name string `json:"name"`
	// Type represents the type of the service, eg. service or aggregator.
	Type string `json:"type"`
	// Version represents the latest version or SVC tag of the service.
	Version string `json:"version"`
	// Revision represents the SVC revision or commit hash of the service.
	Revision string `json:"revision"`

	// GracePeriod represents the duration workers have to clean up before the process gets killed.
	GracePeriod time.Duration `json:"grace_period"`
}

Service represents the minimal information required to define a working service.

func NewService

func NewService(name, kind string) *Service

NewService creates a new service based on

Example
package main

import (
	"github.com/LUSHDigital/core"
)

func main() {
	core.NewService("example", "service")
}
Output:

func (*Service) MustRun added in v0.21.0

func (s *Service) MustRun(ctx context.Context, workers ...Worker)

MustRun will start the given service workers and block block indefinitely, until interupted. The process with an appropriate status code.

func (*Service) Run added in v0.21.0

func (s *Service) Run(ctx context.Context, workers ...Worker) int

Run will start the given service workers and block block indefinitely, until interupted.

func (*Service) StartWorkers

func (s *Service) StartWorkers(ctx context.Context, workers ...Worker)

StartWorkers will start the given service workers and block block indefinitely, until interupted. The process with an appropriate status code. DEPRECATED: Use MustRun in favour of StartWorkers.

Example
package main

import (
	"context"
	"net/http"

	"github.com/LUSHDigital/core"
	"github.com/LUSHDigital/core/workers/grpcsrv"
	"github.com/LUSHDigital/core/workers/httpsrv"
	"github.com/LUSHDigital/core/workers/keybroker"
	"github.com/LUSHDigital/core/workers/metricsrv"
)

var (
	ctx     context.Context
	handler http.Handler
)

func main() {
	service := core.NewService("example", "service")
	service.MustRun(ctx,
		grpcsrv.New(nil),
		httpsrv.NewDefault(handler),
		metricsrv.New(nil),
		keybroker.NewRSA(nil),
	)
}
Output:

type Worker added in v0.21.0

type Worker interface {
	Runner
	Halter
}

Worker represents the behaviour for a service worker.

Directories

Path Synopsis
Package auth provides functions for services to issue and sign api consumer tokens.
Package auth provides functions for services to issue and sign api consumer tokens.
Package env provides functionality for ensuring we retrieve an environment variable
Package env provides functionality for ensuring we retrieve an environment variable
examples
Package i18n provides functions for dealing with internationalisation of services.
Package i18n provides functions for dealing with internationalisation of services.
Package middleware is used to interact with HTTP & gRPC middlewares.
Package middleware is used to interact with HTTP & gRPC middlewares.
i18nmw
Package i18nmw provides transport middlewares for dealing with internationalisation.
Package i18nmw provides transport middlewares for dealing with internationalisation.
metricsmw
Package metricsmw is used to record and expose metrics for an application.
Package metricsmw is used to record and expose metrics for an application.
paginationmw
Package paginationmw provides transport middlewares for dealing with pagination.
Package paginationmw provides transport middlewares for dealing with pagination.
tracingmw
Package tracingmw allows setting and tracing a request by injecting an id as part of it's headers, when dealing with HTTP, or it's context, when dealing with GRPC.
Package tracingmw allows setting and tracing a request by injecting an id as part of it's headers, when dealing with HTTP, or it's context, when dealing with GRPC.
Package pagination defines a paginator able to return formatted responses enabling the API consumer to retrieve data in defined chunks
Package pagination defines a paginator able to return formatted responses enabling the API consumer to retrieve data in defined chunks
Package rest defines the how the default microservice response must look and behave like.
Package rest defines the how the default microservice response must look and behave like.
Package test contains helpers for aiding testing.
Package test contains helpers for aiding testing.
Package workers is used to setup gracefully terminating workers for services.
Package workers is used to setup gracefully terminating workers for services.
grpcsrv
Package grpcsrv provides a default set of configuration for hosting a grpc server in a service.
Package grpcsrv provides a default set of configuration for hosting a grpc server in a service.
httpsrv
Package httpsrv provides a default set of configuration for hosting a http server in a service.
Package httpsrv provides a default set of configuration for hosting a http server in a service.
keybroker
Package keybroker implements a background broker conmtinous retrieval of public keys from multiple different type of sources.
Package keybroker implements a background broker conmtinous retrieval of public keys from multiple different type of sources.
keybroker/keybrokermock
Package keybrokermock implements no-op mocks for the keys package
Package keybrokermock implements no-op mocks for the keys package
metricsrv
Package metricsrv provides a default set of configuration for hosting http prometheus metrics in a service.
Package metricsrv provides a default set of configuration for hosting http prometheus metrics in a service.
readysrv
Package readysrv is used to provide readiness checks for a service.
Package readysrv is used to provide readiness checks for a service.

Jump to

Keyboard shortcuts

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