scribo

package
v0.0.0-...-815751c Latest Latest
Warning

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

Go to latest
Published: May 13, 2016 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package scribo provides the library code for a lightweight, fast RESTful microservice.

This service is an uptime collection mechanism associated with the Mora project. Mora contains three pieces: Oro and Scio which ping each other to measure latency inside of the network, then report those pings to Scribo, which is simply a RESTful API designed to record experimental data.

The package is implemented by three commands: scribo, scribo-migrate, and scribo-register. To run the application locally:

$ scribo-migrate --all
$ scribo -port 8080

So long as you have environment variables configured correctly, the database should be created and the web application will run. See the README for more information on getting started.

Index

Constants

View Source
const (
	GET    = "GET"
	POST   = "POST"
	PUT    = "PUT"
	DELETE = "DELETE"
)

Magic strings referring to HTTP methods managed by the Resource.

View Source
const (
	CTKEY  = "Content-Type"
	CTJSON = "application/json;charset=UTF-8"
)

Default header keys and values that are sent by Resources.

View Source
const (
	StatusUnprocessableEntity = 422
)

Missing status codes that aren't supplied by net/http

View Source
const Version = "1.0"

Version specifies what version of Scribo we're on.

Variables

This section is empty.

Functions

func Authenticate

func Authenticate(app *App, inner http.Handler) http.Handler

Authenticate is decorator that implements Hawk authorization.

func ConnectDB

func ConnectDB() *sql.DB

ConnectDB establishes a connection to the PostgreSQL database

func Debugger

func Debugger(app *App, inner http.Handler) http.Handler

Debugger is a decorator for http handlers to print out the incomming request

func Index

func Index(app *App) http.HandlerFunc

Index handles the root route by rendering a small web page that uses the API to display information about the ping status.

func Logger

func Logger(app *App, inner http.Handler) http.Handler

Logger is a decorator for http handlers to record requests in dev format.

func NodeExists

func NodeExists(db *sql.DB, id int64) (bool, error)

NodeExists tests if the given ID is associated with a node.

func NodeExistsByName

func NodeExistsByName(db *sql.DB, name string) (bool, error)

NodeExistsByName tests if the given name is associated with a node.

func PingExists

func PingExists(db *sql.DB, id int64) (bool, error)

PingExists tests if the given ID is associated with a ping.

Types

type App

type App struct {
	StaticDir   string
	TemplateDir string
	Templates   *template.Template
	Router      *mux.Router
	DB          *sql.DB
}

App defines the complete structure of a web application including a router for multiplexing, storages, assets, templates, cookies, and more. This should be the primary interface for working with Scribo.

func CreateApp

func CreateApp() *App

CreateApp allows you to easily instantiate an App instance.

func (*App) Abort

func (app *App) Abort(w http.ResponseWriter, statusCode int)

Abort is a handler to terminate the request with no error message

func (*App) AddRoute

func (app *App) AddRoute(route Route)

AddRoute allows you to add a handler for a specific route to the router.

func (*App) AddStatic

func (app *App) AddStatic(staticDir string)

AddStatic creates a handler to serve static files.

func (*App) Error

func (app *App) Error(w http.ResponseWriter, err error, statusCode int)

Error is a handler to terminate the request with an error message.

func (*App) JSONAbort

func (app *App) JSONAbort(w http.ResponseWriter, statusCode int)

JSONAbort is a handler to terminate the request with a JSON response

func (*App) JSONError

func (app *App) JSONError(w http.ResponseWriter, err error, statusCode int)

JSONError is a handler to terminate the request with a JSON response

func (*App) Run

func (app *App) Run(port int)

Run the web application via the associated router.

type Dashboard

type Dashboard struct {
	Nodes Nodes // A limited, ordered collection of nodes for display
	Pings Pings // A limited, ordered collection of pings for display
}

Dashboard is a collection of nodes and pings for display.

type DeleteNotSupported

type DeleteNotSupported struct{}

DeleteNotSupported allows the creation of Resources with no Delete method.

func (DeleteNotSupported) Delete

func (r DeleteNotSupported) Delete(app *App, request *http.Request) (int, interface{}, error)

Delete returns method not allowed (405) on DeleteNotSupported types.

type GetNotSupported

type GetNotSupported struct{}

GetNotSupported allows the creation of Resources with no Get method.

func (GetNotSupported) Get

func (r GetNotSupported) Get(app *App, request *http.Request) (int, interface{}, error)

Get returns method not allowed (405) on GetNotSupported types.

type HandlerFunc

type HandlerFunc func(app *App) http.HandlerFunc

HandlerFunc is wrapper for creating handler functions with app callbacks.

type Node

type Node struct {
	ID      int64     `json:"id"`      // Unique ID of the node
	Name    string    `json:"name"`    // Name/DNS of the node
	Address string    `json:"address"` // IP Address of the node
	DNS     string    `json:"dns"`     // DNS Lookup for the node
	Key     string    `json:"-"`       // Authentication key of the node
	Created time.Time `json:"created"` // Datetime the node was created
	Updated time.Time `json:"updated"` // Datetime the node was updated
}

Node is a model that represents a participant in the network

func GetNode

func GetNode(db *sql.DB, id int64) (Node, error)

GetNode by ID, attempts to return the node or an error otherwise.

func GetNodeByName

func GetNodeByName(db *sql.DB, name string) (Node, error)

GetNodeByName attempts to return the node from a name or an error otherwise.

func (*Node) Delete

func (node *Node) Delete(db *sql.DB) (bool, error)

Delete a node from the database. This method is obviously destructive and returns true if the number of rows affected is 1 or false otherwise.

func (*Node) Save

func (node *Node) Save(db *sql.DB) (bool, error)

Save a node struct to the database. This function checks if the node has an ID or not. If it does, it will execute a SQL UPDATE, otherwise it will execute a SQL INSERT. Returns a boolean if the node was created (INSERT) or False if the node was simply updated in the normal manner. This method also handles setting the Created and Updated timestamps on the node. TODO: Transform this into a prepared statement that we can run.

func (*Node) UpdateKey

func (node *Node) UpdateKey()

UpdateKey is the key creation mechanism for the node. It combines the Node name, address, and dns fields with the current time and a simple secret that is loaded from the environment. This method then sets the base64 encoded sha256 hash of the generated string as the key on the node. Note: this method does not update the database!

type NodeCollection

type NodeCollection struct {
	PutNotSupported
	DeleteNotSupported
}

NodeCollection is a RESTful resource for listing and creating nodes.

func (NodeCollection) Get

func (r NodeCollection) Get(app *App, request *http.Request) (int, interface{}, error)

Get returns the listing of nodes

func (NodeCollection) Post

func (r NodeCollection) Post(app *App, request *http.Request) (int, interface{}, error)

Post handles the creation of a node from JSON in the request body

type NodeDetail

type NodeDetail struct {
	PostNotSupported
}

NodeDetail is a RESTful resource for updating and deleting nodes.

func (NodeDetail) Delete

func (r NodeDetail) Delete(app *App, request *http.Request) (int, interface{}, error)

Delete a node from the database

func (NodeDetail) Get

func (r NodeDetail) Get(app *App, request *http.Request) (int, interface{}, error)

Get returns a single node from the database.

func (NodeDetail) Put

func (r NodeDetail) Put(app *App, request *http.Request) (int, interface{}, error)

Put updates a node in the database

type Nodes

type Nodes []Node

Nodes is a collection of node items for use elsewhere.

func FetchNodes

func FetchNodes(db *sql.DB, limit int) (Nodes, error)

FetchNodes returns a collection of nodes, ordered by the updated timestamp. This function expects you to limit the size of the collection by specifying the maximum number of nodes to return in the Nodes collection.

type Ping

type Ping struct {
	ID      int64     `json:"id"`      // Unique ID of the ping
	Source  int64     `json:"source"`  // The ID of the source node
	Target  int64     `json:"target"`  // The ID of the target node
	Payload int       `json:"payload"` // The size in bytes of the payload
	Latency float64   `json:"latency"` // The time in ms of the round trip
	Timeout bool      `json:"timeout"` // Whether or not the request timed out
	Created time.Time `json:"created"` // Datetime the ping was created
	Updated time.Time `json:"updated"` // Datetime the ping was updated
}

Ping is a model that represents a latency report.

func GetPing

func GetPing(db *sql.DB, id int64) (Ping, error)

GetPing by ID, attempts to return the ping or an error otherwise.

func (*Ping) Delete

func (ping *Ping) Delete(db *sql.DB) (bool, error)

Delete a ping from the database. This method is obviously destructive and returns true if the number of rows affected is 1 or false otherwise.

func (*Ping) Save

func (ping *Ping) Save(db *sql.DB) (bool, error)

Save a ping struct to the database. This function checks if the ping has an ID or not. If it does, it will execute a SQL UPDATE, otherwise it will execute a SQL INSERT. Returns a boolean if the ping was created (INSERT) or False if the ping was simply updated in the normal manner. This method also handles setting the Created and Updated timestamps on the ping. TODO: Transform this into a prepared statement that we can run.

type PingCollection

type PingCollection struct {
	PutNotSupported
	DeleteNotSupported
}

PingCollection is a RESTful resource for listing and creating pings.

func (PingCollection) Get

func (r PingCollection) Get(app *App, request *http.Request) (int, interface{}, error)

Get returns the listing of pings

func (PingCollection) Post

func (r PingCollection) Post(app *App, request *http.Request) (int, interface{}, error)

Post handles the creation of a ping from JSON in the request body

type PingDetail

type PingDetail struct {
	PostNotSupported
}

PingDetail is a RESTful resource for updating and deleting pings.

func (PingDetail) Delete

func (r PingDetail) Delete(app *App, request *http.Request) (int, interface{}, error)

Delete a ping from the database

func (PingDetail) Get

func (r PingDetail) Get(app *App, request *http.Request) (int, interface{}, error)

Get returns a single ping from the database.

func (PingDetail) Put

func (r PingDetail) Put(app *App, request *http.Request) (int, interface{}, error)

Put updates a ping in the database

type Pings

type Pings []Ping

Pings is a collection of latency reports for use elsewhere.

func FetchPings

func FetchPings(db *sql.DB, limit int) (Pings, error)

FetchPings returns a collection of pings, ordered by the created timestamp. This function expects you to limit the size of the collection by specifying the maximum number of pings to return in the Pings collection.

type PostNotSupported

type PostNotSupported struct{}

PostNotSupported allows the creation of Resources with no Post method.

func (PostNotSupported) Post

func (r PostNotSupported) Post(app *App, request *http.Request) (int, interface{}, error)

Post returns method not allowed (405) on PostNotSupported types.

type PutNotSupported

type PutNotSupported struct{}

PutNotSupported allows the creation of Resources with no Put method.

func (PutNotSupported) Put

func (r PutNotSupported) Put(app *App, request *http.Request) (int, interface{}, error)

Put returns method not allowed (405) on PutNotSupported types.

type Resource

type Resource interface {
	Get(app *App, request *http.Request) (int, interface{}, error)
	Post(app *App, request *http.Request) (int, interface{}, error)
	Put(app *App, request *http.Request) (int, interface{}, error)
	Delete(app *App, request *http.Request) (int, interface{}, error)
}

Resource defines a REST endpoint with the standard methods. The standard methods should return an int (the status code) as well as any other data that is required, e.g. the interface second argument.

type Route

type Route struct {
	Name      string
	Methods   []string
	Pattern   string
	Handler   HandlerFunc
	Authorize bool
}

Route allows easy definition of our API

func CreateResourceRoute

func CreateResourceRoute(resource Resource, name string, pattern string) Route

CreateResourceRoute returns a Route object, constructing the Handler and Method from the Resource definition. This allows you to quickly add Resources to the routes object for inclusion with the web app.

type Routes

type Routes []Route

Routes defines the complete route set for various non-API handlers.

Notes

Bugs

  • relative import for static/template directories needs to be configured rather than guessed.

Jump to

Keyboard shortcuts

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