core

package module
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2019 License: Apache-2.0 Imports: 5 Imported by: 0

README

License Go Report Card Build Status GoDoc

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/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() {
	core.SetupLogs()

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

	server := httpsrv.New(&http.Server{
		ReadTimeout: 10 * time.Second,
		Handler: http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
			w.WriteHeader(200)
			w.Write([]byte("hello world"))
		}),
	})

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

Documentation

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

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.

Service Workers

These packages contain convenient service 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.

  • core-redis: Libraries for connecting to, and working with a Redis store.
  • core-sql: Libraries for connecting to, and working with an SQL database.

Tools

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

  • jwtl: A command line tool to help generating JWTs during development.

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 structs, slices, and primitive types, originally forked from github.com/blockloop/scan
  • LUSHDigital/uuid: A UUID package originally forked from github.com/gofrs/uuid & github.com/satori/go.uuid

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 SetupLogs

func SetupLogs()

SetupLogs will set up the appropriate log flags.

Types

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"`
}

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

func NewService

func NewService() *Service

NewService reads a service definition from the environment.

Example
package main

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

func main() {
	core.NewService()
}
Output:

func (*Service) StartWorkers

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

StartWorkers will start the given service workers and block block indefinitely.

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() {
	svc := core.NewService()
	svc.StartWorkers(ctx,
		grpcsrv.New(nil),
		httpsrv.NewDefault(handler),
		metricsrv.New(nil),
		keybroker.NewRSA(nil),
	)
}
Output:

type ServiceWorker

type ServiceWorker interface {
	// Run should be a blocking operation
	Run(context.Context, io.Writer) error
}

ServiceWorker represents the behaviour for running 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.
workers
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