spine

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2022 License: MIT Imports: 31 Imported by: 0

README

Spine

Spine is a backend building block written in Go.

Packages

  1. Background
  2. Cache
  3. Config
  4. Context
  5. Crypto
  6. Disco
  7. Log
  8. Net
  9. Schedule
  10. Stats
  11. Testing
  12. Tracing

Demo

Start a simple HTTP server

$ git clone https://github.com/deixis/spine.git
$ cd spine/example
$ CONFIG_URI=file://${PWD}/config.toml go run http_server.go

Send a request

$ curl -v http://127.0.0.1:3000/ping

Example

Simple HTTP server

This code creates a SPINE instance and attach and HTTP handler to it with one route /ping.

package main

import (
	"context"
	"fmt"
	"os"

	"github.com/deixis/spine"
	"github.com/deixis/spine/log"
	"github.com/deixis/spine/net/http"
)

var (
	version = "dirty"
	date    = "now"
)

type Config struct {
	Foo string `toml:"foo"`
}

func main() {
	// Create spine
	appConfig := &Config{}
	app, err := spine.New("demo", appConfig)
	if err != nil {
		return errors.Wrap(err, "error initialising spine")
	}
	app.Config().Version = version

	// Register HTTP handler
	httpServer := http.NewServer()
	httpServer.HandleFunc("/ping", http.GET, Ping)
	app.RegisterService(&spine.ServiceRegistration{
		Name:   "http.demo",
		Host:   os.Getenv("IP"),
		Port:   8080,
		Server: httpServer,
		Tags:   []string{"http"},
	})

	// Start serving requests
	err = app.Serve()
	if err != nil {
		fmt.Println("Problem serving requests", err)
		os.Exit(1)
	}
}

// Ping handler example
func Ping(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	log.Trace(ctx, "action.ping", "Simple request", log.String("ua", r.HTTP.UserAgent()))
	w.Head(http.StatusOK)
}

Config

Example of a configuration file

node = "$HOSTNAME"
version = "1.1"

[request]
  timeout_ms = 1000
  allow_context = false

[disco.consul]
  address = "localhost:7500"
  dc = "local"

[log.printer.stdout]

[cache.local]

[app.demo]
  foo = "bar"

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 the core structure for a new service

func New

func New(service string, appConfig interface{}) (*App, error)

New creates a new App and returns it

func NewWithConfig

func NewWithConfig(
	service string, r io.Reader, appConfig interface{},
) (a *App, err error)

NewWithConfig creates a new App with a custom configuration

func (*App) BG

func (a *App) BG() *bg.Reg

func (*App) Cache

func (a *App) Cache() cache.Cache

func (*App) Close

func (a *App) Close() error

Close immediately closes the server and any in-flight request or background job will be left unfinished. For a graceful shutdown, use Shutdown.

func (*App) Config

func (a *App) Config() *config.Config

func (*App) ConfigTree

func (a *App) ConfigTree() config.Tree

func (*App) Deadline

func (a *App) Deadline() (deadline time.Time, ok bool)

Deadline implements context.Context

func (*App) Disco

func (a *App) Disco() disco.Agent

Disco returns the active service discovery agent.

When service discovery is disabled, it will return a local agent that acts like a regular service discovery agent, expect that it only registers local services.

func (*App) Done

func (a *App) Done() <-chan struct{}

Done implements context.Context

func (*App) Drain

func (a *App) Drain() bool

Drain notify all handlers to enter in draining mode. It means they are no longer accepting new requests, but they can finish all in-flight requests

func (*App) Err

func (a *App) Err() error

Err implements context.Context

func (*App) Error

func (a *App) Error(tag, msg string, fields ...log.Field)

Error implements log.Logger

func (*App) L

func (a *App) L() log.Logger

func (*App) Ready

func (a *App) Ready()

Ready holds the callee until the app is fully operational

func (*App) RegisterDrainHandler

func (a *App) RegisterDrainHandler(h func(ctx context.Context))

RegisterDrainHandler registers a handler that is called when the app starts draining

func (*App) RegisterServer

func (a *App) RegisterServer(addr string, s net.Server)

RegisterServer adds the given server to the list of managed servers

func (*App) RegisterService

func (a *App) RegisterService(r *ServiceRegistration)

RegisterService adds the server to the list of managed servers and registers it to service discovery

func (*App) Scheduler

func (a *App) Scheduler() schedule.Scheduler

func (*App) Serve

func (a *App) Serve() error

Serve allows handlers to serve requests and blocks the call

func (*App) Service

func (a *App) Service() string

func (*App) Shutdown

func (a *App) Shutdown()

Shutdown gracefully shuts down the server without interrupting any active connections. Shutdown works by first draining all handlers, then draining the main context, and finally shut down. If the provided context expires before the shutdown is complete, Shutdown returns the context's error, otherwise it returns any error returned from closing the Server's underlying Listener(s).

func (*App) Stats

func (a *App) Stats() stats.Stats

func (*App) Trace

func (a *App) Trace(tag, msg string, fields ...log.Field)

Trace implements log.Logger

func (*App) Tracer

func (a *App) Tracer() tracing.Tracer

func (*App) Value

func (a *App) Value(key interface{}) interface{}

Value implements context.Context

func (*App) Warning

func (a *App) Warning(tag, msg string, fields ...log.Field)

Warning implements log.Logger

type ServiceRegistration

type ServiceRegistration struct {
	// ID is the service instance unique identifier (optional)
	ID string
	// Name is the service identifier
	Name string
	// Host is the interface on which the server runs.
	// Service discovery can override this value.
	Host string
	// Port is the port number
	Port uint16
	// Server is the server that provides the registered service
	Server net.Server
	// Tags for that service (versioning, blue-green, whatever)
	Tags []string
}

ServiceRegistration contains info to register a service

Directories

Path Synopsis
Package bg aims to manage all background jobs run on spine.
Package bg aims to manage all background jobs run on spine.
Package cache is a caching and cache-filling library
Package cache is a caching and cache-filling library
adapter/local
Package local provides an LRU cache and cache-filling library that only runs on the local instance.
Package local provides an LRU cache and cache-filling library that only runs on the local instance.
lru
Package lru implements a LRU cache.
Package lru implements a LRU cache.
Package config implements logic to load dynamic configuration from various sources
Package config implements logic to load dynamic configuration from various sources
adapter/consul
Package consul reads configuration from the Consul KV store
Package consul reads configuration from the Consul KV store
adapter/file
Package file reads configuration from a JSON file
Package file reads configuration from a JSON file
Package context defines the context types, which carry information defined for a specific scope (application, request, ...) A context can be passed across API boundaries and between processes.
Package context defines the context types, which carry information defined for a specific scope (application, request, ...) A context can be passed across API boundaries and between processes.
Package crypto provides security tools
Package crypto provides security tools
adapter/consul
Package consul is a wrapper around the Hashicorp Consul service discovery functionnality
Package consul is a wrapper around the Hashicorp Consul service discovery functionnality
example
cache
Package main is a distributed-cache example
Package main is a distributed-cache example
disco
Package main is a service discovery example
Package main is a service discovery example
stats
Package main is a tracing example with Jaeger
Package main is a tracing example with Jaeger
tracing
Package main is a tracing example with Jaeger
Package main is a tracing example with Jaeger
log
Package log defines the logging facade
Package log defines the logging facade
formatter/json
Package json is a JSON log formatter.
Package json is a JSON log formatter.
formatter/logf
Package logf is a human friendly log formatter.
Package logf is a human friendly log formatter.
printer/file
Package file prints log lines to a file.
Package file prints log lines to a file.
printer/stackdriver
Package stackdriver send log lines to Google Stackdriver.
Package stackdriver send log lines to Google Stackdriver.
printer/stdout
Package stdout prints log lines into the standard output.
Package stdout prints log lines into the standard output.
net
Package net contains the main logic to send and receive requests
Package net contains the main logic to send and receive requests
http
Package http is an extra layer on to of the standard go net/http package.
Package http is an extra layer on to of the standard go net/http package.
naming
Package naming is a library that converts a target to one or multiple network addresses.
Package naming is a library that converts a target to one or multiple network addresses.
stream
Package stream offers an abstraction layer for stream-processing platforms, such as NATS streaming.
Package stream offers an abstraction layer for stream-processing platforms, such as NATS streaming.
adapter/local
Package local implements a scheduler that persists jobs on a local storage.
Package local implements a scheduler that persists jobs on a local storage.
Package stats provides functions for reporting metrics to a service.
Package stats provides functions for reporting metrics to a service.
Package testing is an extension to the standard go testing package tailored for testing the spine framework.
Package testing is an extension to the standard go testing package tailored for testing the spine framework.
adapter/jaeger
Package jaeger wraps the Jaeger tracer
Package jaeger wraps the Jaeger tracer

Jump to

Keyboard shortcuts

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