fossilizerhttp

package
v0.4.8 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2020 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package fossilizerhttp is used to create an HTTP server from a fossilizer adapter.

It serves the following routes:

GET /
	Renders information about the fossilizer.

POST /fossils
	Requests data to be fossilized.
	Body should be a JSON object containing:
	{
		data: "hex-encoded string",
		meta: "human-readable string"
	}
Example

This example shows how to create a server from a dummyfossilizer. It also tests the root route of the server using net/http/httptest.

package main

import (
	"context"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/http/httptest"
	"time"

	"github.com/stratumn/go-core/dummyfossilizer"
	"github.com/stratumn/go-core/fossilizer/fossilizerhttp"
	"github.com/stratumn/go-core/jsonhttp"
	"github.com/stratumn/go-core/jsonws"
)

func main() {
	// Create a dummy adapter.
	a := dummyfossilizer.New(&dummyfossilizer.Config{Version: "x.x.x", Commit: "abc"})
	config := &fossilizerhttp.Config{
		MaxDataLen: 64,
	}
	httpConfig := &jsonhttp.Config{
		Address: ":6000",
	}
	basicConfig := &jsonws.BasicConfig{}
	bufConfig := &jsonws.BufferedConnConfig{
		Size:         256,
		WriteTimeout: 10 * time.Second,
		PongTimeout:  70 * time.Second,
		PingInterval: time.Minute,
		MaxMsgSize:   1024,
	}

	// Create a server.
	s := fossilizerhttp.New(a, config, httpConfig, basicConfig, bufConfig)
	go s.Start()
	ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
	defer s.Shutdown(ctx)
	defer cancel()

	// Create a test server.
	ts := httptest.NewServer(s)
	defer ts.Close()

	// Test the root route.
	res, err := http.Get(ts.URL)
	if err != nil {
		log.Fatal(err)
	}

	info, err := ioutil.ReadAll(res.Body)
	res.Body.Close()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("%s", info)
}
Output:

{"adapter":{"name":"dummyfossilizer","description":"Stratumn's Dummy Fossilizer","version":"x.x.x","commit":"abc"}}

Index

Examples

Constants

View Source
const (
	// DefaultAddress is the default address of the server.
	DefaultAddress = ":6000"

	// DefaultMinDataLen is the default minimum fossilize data length.
	DefaultMinDataLen = 32

	// DefaultMaxDataLen is the default maximum fossilize data length.
	DefaultMaxDataLen = 64

	// DefaultFossilizerEventChanSize is the default size of the fossilizer event channel.
	DefaultFossilizerEventChanSize = 256
)
View Source
const (
	// Component name for monitoring.
	Component = "fossilizer"
)

Variables

This section is empty.

Functions

func RegisterFlags

func RegisterFlags()

RegisterFlags register the flags used by RunWithFlags.

func Run

func Run(
	ctx context.Context,
	a fossilizer.Adapter,
	config *Config,
	monitoringConfig *monitoring.Config,
	httpConfig *jsonhttp.Config,
	basicConfig *jsonws.BasicConfig,
	bufConnConfig *jsonws.BufferedConnConfig,
	shutdownTimeout time.Duration,
)

Run launches a fossilizerhttp server.

func RunWithFlags

func RunWithFlags(ctx context.Context, a fossilizer.Adapter)

RunWithFlags should be called after RegisterFlags and flag.Parse to launch a fossilizerhttp server configured using flag values.

Types

type Config

type Config struct {
	// The minimum fossilize data length.
	MinDataLen int

	// The maximum fossilize data length.
	MaxDataLen int

	// The size of the EventChan channel.
	FossilizerEventChanSize int
}

Config contains configuration options for the server.

type Info

type Info struct {
	Adapter interface{} `json:"adapter"`
}

Info is the info returned by the root route.

type Server

type Server struct {
	*jsonhttp.Server
	// contains filtered or unexported fields
}

Server is an HTTP server for fossilizers.

func New

func New(
	a fossilizer.Adapter,
	config *Config,
	httpConfig *jsonhttp.Config,
	basicConfig *jsonws.BasicConfig,
	bufConnConfig *jsonws.BufferedConnConfig,
) *Server

New create an instance of a server.

func (*Server) ListenAndServe

func (s *Server) ListenAndServe() (err error)

ListenAndServe starts the server.

func (*Server) Shutdown

func (s *Server) Shutdown(ctx context.Context) error

Shutdown stops the server.

func (*Server) Start

func (s *Server) Start()

Start starts the main loops. You do not need to call this if you call ListenAndServe().

Jump to

Keyboard shortcuts

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