storehttp

package
v0.0.6-dev Latest Latest
Warning

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

Go to latest
Published: Feb 8, 2017 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

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

It serves the following routes:

GET /
	Renders information about the fossilizer.

POST /segments
	Saves then renders a segment.
	Body should be a JSON encoded segment.

GET /segments/:linkHash
	Renders a segment.

DELETE /segments/:linkHash
	Deletes then renders a segment.

GET /segments?[offset=offset]&[limit=limit]&[mapId=mapId]&[prevLinkHash=prevLinkHash]&[tags=list+of+tags]
	Finds and renders segments.

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

GET /websocket
	A web socket that broadcasts messages when a segment is saved:
		{ "type": "didSave", "data": [segment] }
Example

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

package main

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

	"time"

	"github.com/stratumn/go/dummystore"
	"github.com/stratumn/go/jsonhttp"
	"github.com/stratumn/go/jsonws"
	"github.com/stratumn/go/store/storehttp"
)

func main() {
	// Create a dummy adapter.
	a := dummystore.New(&dummystore.Config{Version: "0.1.0", Commit: "abc"})
	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, httpConfig, basicConfig, bufConnConfig)
	go s.Start()
	defer s.Shutdown()

	// 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":"dummy","description":"Stratumn Dummy Store","version":"0.1.0","commit":"abc"}}

Index

Examples

Constants

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

	// DefaultWebSocketReadBufferSize is the default size of the web socket
	// read buffer in bytes.
	DefaultWebSocketReadBufferSize = 1024

	// DefaultWebSocketWriteBufferSize is the default size of the web socket
	// write buffer in bytes.
	DefaultWebSocketWriteBufferSize = 1024

	// DefaultWebSocketWriteChanSize is the default size of a web socket
	// buffered connection channel.
	DefaultWebSocketWriteChanSize = 256

	// DefaultWebSocketWriteTimeout is the default timeout of a web socket
	// write.
	DefaultWebSocketWriteTimeout = 10 * time.Second

	// DefaultWebSocketPongTimeout is the default timeout of a web socket
	// expected pong.
	DefaultWebSocketPongTimeout = time.Minute

	// DefaultWebSocketPingInterval is the default interval between web
	// socket pings.
	DefaultWebSocketPingInterval = (DefaultWebSocketPongTimeout * 9) / 10

	// DefaultWebSocketMaxMsgSize is the default maximum size of a web
	// socke received message in in bytes.
	DefaultWebSocketMaxMsgSize = 32 * 1024
)
View Source
const (
	// DidSave means a segment was saved.
	DidSave = "didSave"
)

Web socket message types.

Variables

This section is empty.

Functions

func Run

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

Run launches a HTTP Store

Types

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 stores.

func New

func New(
	a store.Adapter,
	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() 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