go-core: github.com/stratumn/go-core/store/storehttp Index | Examples | Files

package storehttp

import "github.com/stratumn/go-core/store/storehttp"

Package storehttp is used to create an HTTP server from a store adapter.

It serves the following routes:

GET /
	Renders information about the store.

POST /links
	Saves then renders a link.
	Body should be a JSON encoded link.

POST /batch/links
	Atomically saves then renders a collection of links.
	If any of the links is invalid, the whole batch is dropped.
	Body should be a JSON encoded array of links.

POST /evidences/:linkHash
	Adds evidence to a link.
	Body should be a JSON encoded evidence.

GET /segments/:linkHash
	Renders a segment.

GET /segments?[offset=offset]&[limit=limit]&[mapIds[]=id1]&[mapIds[]=id2]&[prevLinkHash=prevLinkHash]&[tags[]=tag1]&[tags[]=tag2]
	Finds and renders segments.

GET /maps?[offset=offset]&[limit=limit]
	Finds and renders map IDs.

GET /websocket
	A web socket that broadcasts messages from the store:
		{ "type": "SavedLink", "data": [link] }
		{ "type": "SavedEvidence", "data": [evidence] }

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

Code:

// Create a dummy adapter.
a := dummystore.New(&dummystore.Config{Version: "x.x.x", Commit: "abc"})
config := &storehttp.Config{
    StoreEventsChanSize: 8,
}
httpConfig := &jsonhttp.Config{
    Address: "5555",
}
basicConfig := &jsonws.BasicConfig{}
bufConnConfig := &jsonws.BufferedConnConfig{
    Size:         256,
    WriteTimeout: 10 * time.Second,
    PongTimeout:  70 * time.Second,
    PingInterval: time.Minute,
    MaxMsgSize:   1024,
}

// Create a server.
s := storehttp.New(a, config, httpConfig, basicConfig, bufConnConfig)
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":"dummystore","description":"Stratumn's Dummy Store","version":"x.x.x","commit":"abc"}}

Index

Examples

Package Files

cmd.go errors.go metrics.go storehttp.go util.go

Constants

const (
    // DefaultStoreEventsChanSize is the default size of the store events channel.
    DefaultStoreEventsChanSize = 256

    // DefaultAddress is the default address of the server.
    DefaultAddress = ":5000"
)

func RegisterFlags Uses

func RegisterFlags()

RegisterFlags register the flags used by RunWithFlags.

func Run Uses

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

Run launches a storehttp server.

func RunWithFlags Uses

func RunWithFlags(a store.Adapter)

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

type Config Uses

type Config struct {
    // The size of the store event channel.
    StoreEventsChanSize int
}

Config contains configuration options for the server.

type Info Uses

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

Info is the info returned by the root route.

type Server Uses

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

Server is an HTTP server for stores.

func New Uses

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

New create an instance of a server.

func (*Server) ListenAndServe Uses

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

ListenAndServe starts the server.

func (*Server) Shutdown Uses

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

Shutdown stops the server.

func (*Server) Start Uses

func (s *Server) Start()

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

Package storehttp imports 20 packages (graph) and is imported by 7 packages. Updated 2019-04-28. Refresh now. Tools for package owners.