server

package
v0.0.0-...-453881e Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2018 License: MIT Imports: 27 Imported by: 1

Documentation

Overview

Package server provides an HTTP interface for the job queue/scheduler.

Example
package main

import (
	"net/http"

	"github.com/Shyp/rest"
)

type auther struct{}

func (a *auther) Authorize(userId, token string) *rest.Error {
	// Implement your auth scheme here.
	return nil
}

func main() {
	// Get all server routes using your authorization handler, then listen on
	// port 9090
	handler := Get(&auther{})
	http.ListenAndServe(":9090", handler)
}
Output:

Index

Examples

Constants

View Source
const MAX_ENQUEUE_DATA_SIZE = 100 * 1024

The maximum data size that can be sent in the body of a HTTP request.

Variables

View Source
var DefaultAuthorizer = NewSharedSecretAuthorizer()
View Source
var DefaultServer http.Handler

DefaultServer serves every route using the DefaultAuthorizer for authentication.

Functions

func AddUser

func AddUser(user string, password string)

AddUser tells the DefaultAuthorizer that a given user and password is allowed to access the API.

func Get

func Get(a Authorizer) http.Handler

Get returns a http.Handler with all routes initialized using the given Authorizer.

Types

type Authorizer

type Authorizer interface {
	// Authorize returns nil if the user and token are allowed to access the
	// API, and a rest.Error otherwise. The rest.Error will be returned as the
	// body of a 401 HTTP response.
	Authorize(user string, token string) *rest.Error
}

The Authorizer interface can be used to authorize a given user and token to access the API.

type CreateJobRequest

type CreateJobRequest struct {
	Name             string                  `json:"name"`
	Attempts         uint8                   `json:"attempts"`
	Concurrency      uint8                   `json:"concurrency"`
	DeliveryStrategy models.DeliveryStrategy `json:"delivery_strategy"`
}

CreateJobRequest is a struct of data sent in the body of a request to /v1/jobs

type EnqueueJobRequest

type EnqueueJobRequest struct {
	// Job data to enqueue.
	Data json.RawMessage `json:"data"`
	// The earliest time we can run this job. If not specified, defaults to the
	// current time.
	RunAfter types.NullTime `json:"run_after"`
	// The latest time we can run this job. If not specified, defaults to null
	// (never expires).
	ExpiresAt types.NullTime `json:"expires_at"`
}

An EnqueueJobRequest is sent in the body of a request to PUT /v1/jobs/:job-name/:job-id.

type JobStatusRequest

type JobStatusRequest struct {
	// Should be "succeeded" or "failed".
	Status models.JobStatus `json:"status"`

	// Attempt is sent to ensure we don't attempt a null write.
	Attempt *uint8 `json:"attempt"` // pointer to distinguish between null/omitted value and 0.

	// Retryable indicates whether a failure is retryable. The default is true.
	// Set to false to avoid retrying a particular failure.
	Retryable *bool `json:"retryable"` // pointer to distinguish between null value and false.
}

The body of a POST request to /v1/jobs/:job-name/:job-id, recording the status of a job.

type RegexpHandler

type RegexpHandler struct {
	// contains filtered or unexported fields
}

A RegexpHandler is a simple http.Handler that can match regular expressions for routes.

Example
// GET /v1/jobs/:job-name
route := regexp.MustCompile(`^/v1/jobs/(?P<JobName>[^\s\/]+)$`)

h := new(RegexpHandler)
h.HandleFunc(route, []string{"GET", "POST"}, func(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte("Hello World!"))
})
Output:

func (*RegexpHandler) HandleFunc

func (h *RegexpHandler) HandleFunc(pattern *regexp.Regexp, methods []string, handler func(http.ResponseWriter, *http.Request))

Handler calls the provided HandlerFunc for requests whose URL matches the given pattern and HTTP method. The first matching route will get called.

func (*RegexpHandler) Handler

func (h *RegexpHandler) Handler(pattern *regexp.Regexp, methods []string, handler http.Handler)

Handler calls the provided handler for requests whose URL matches the given pattern and HTTP method. The first matching route will get called.

func (*RegexpHandler) ServeHTTP

func (h *RegexpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP checks all registered routes in turn for a match, and calls ServeHTTP on the first matching handler. If no routes match, StatusMethodNotAllowed will be rendered.

type SharedSecretAuthorizer

type SharedSecretAuthorizer struct {
	// contains filtered or unexported fields
}

SharedSecretAuthorizer uses an in-memory map of usernames and passwords to authenticate incoming requests.

func NewSharedSecretAuthorizer

func NewSharedSecretAuthorizer() *SharedSecretAuthorizer

NewSharedSecretAuthorizer creates a SharedSecretAuthorizer ready for use.

func (*SharedSecretAuthorizer) AddUser

func (ssa *SharedSecretAuthorizer) AddUser(userId string, password string)

AddUser authorizes a given user and password to access the API.

func (*SharedSecretAuthorizer) Authorize

func (c *SharedSecretAuthorizer) Authorize(userId string, token string) *rest.Error

Authorize returns nil if the userId and token have been added to c, and a rest.Error if they are not allowed to access the API.

type UnsafeBypassAuthorizer

type UnsafeBypassAuthorizer struct{}

Use this if you need to bypass the API authorization scheme.

func (*UnsafeBypassAuthorizer) Authorize

func (u *UnsafeBypassAuthorizer) Authorize(userId string, token string) *rest.Error

Jump to

Keyboard shortcuts

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