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
- func Authenticate(app *App, inner http.Handler) http.Handler
- func ConnectDB() *sql.DB
- func Debugger(app *App, inner http.Handler) http.Handler
- func Index(app *App) http.HandlerFunc
- func Logger(app *App, inner http.Handler) http.Handler
- func NodeExists(db *sql.DB, id int64) (bool, error)
- func NodeExistsByName(db *sql.DB, name string) (bool, error)
- func PingExists(db *sql.DB, id int64) (bool, error)
- type App
- func (app *App) Abort(w http.ResponseWriter, statusCode int)
- func (app *App) AddRoute(route Route)
- func (app *App) AddStatic(staticDir string)
- func (app *App) Error(w http.ResponseWriter, err error, statusCode int)
- func (app *App) JSONAbort(w http.ResponseWriter, statusCode int)
- func (app *App) JSONError(w http.ResponseWriter, err error, statusCode int)
- func (app *App) Run(port int)
- type Dashboard
- type DeleteNotSupported
- type GetNotSupported
- type HandlerFunc
- type Node
- type NodeCollection
- type NodeDetail
- type Nodes
- type Ping
- type PingCollection
- type PingDetail
- type Pings
- type PostNotSupported
- type PutNotSupported
- type Resource
- type Route
- type Routes
- Bugs
Constants ¶
const ( GET = "GET" POST = "POST" PUT = "PUT" DELETE = "DELETE" )
Magic strings referring to HTTP methods managed by the Resource.
const ( CTKEY = "Content-Type" CTJSON = "application/json;charset=UTF-8" )
Default header keys and values that are sent by Resources.
const (
StatusUnprocessableEntity = 422
)
Missing status codes that aren't supplied by net/http
const Version = "1.0"
Version specifies what version of Scribo we're on.
Variables ¶
This section is empty.
Functions ¶
func Authenticate ¶
Authenticate is decorator that implements Hawk authorization.
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 NodeExists ¶
NodeExists tests if the given ID is associated with a node.
func NodeExistsByName ¶
NodeExistsByName tests if the given name is associated with a node.
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 (*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) 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
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.
type GetNotSupported ¶
type GetNotSupported struct{}
GetNotSupported allows the creation of Resources with no Get method.
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 GetNodeByName ¶
GetNodeByName attempts to return the node from a name or an error otherwise.
func (*Node) Delete ¶
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 ¶
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.
type NodeDetail ¶
type NodeDetail struct {
PostNotSupported
}
NodeDetail is a RESTful resource for updating and deleting nodes.
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 (*Ping) Delete ¶
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 ¶
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.
type PingDetail ¶
type PingDetail struct {
PostNotSupported
}
PingDetail is a RESTful resource for updating and deleting pings.
type PostNotSupported ¶
type PostNotSupported struct{}
PostNotSupported allows the creation of Resources with no Post method.
type PutNotSupported ¶
type PutNotSupported struct{}
PutNotSupported allows the creation of Resources with no Put method.
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 ¶
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.
Notes ¶
Bugs ¶
relative import for static/template directories needs to be configured rather than guessed.