perfect

package module
v0.0.0-...-eaf0aa9 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2014 License: BSD-3-Clause Imports: 31 Imported by: 5

README

perfect Build Status

The Perfect Framework for Go is in development.

Documentation

Index

Constants

View Source
const (
	EC_P521 = 521
	EC_P384 = 384
)
View Source
const (
	TEMPLATE_DIR = "templates"
	TEMPLATE_EXT = ".html"
)
View Source
const GITHUB_URL = "https://api.github.com/repos/vpetrov/survana/issues?access_token=a8202d411154a49dab3a42b6e46fc51549ce651a"

TODO: remove this or make it configurable

View Source
const (
	ID_STRING = "AaBbCcDdEeFfGgHhJjKLMmNnoPpqRrSsTtUuVvWwXxYyZz3456789"
)
View Source
const (
	SESSION_ID = "SSESSIONID"
)

Variables

View Source
var (
	ErrEmptyRequest      = errors.New("Empty request")
	ErrNotFound          = errors.New("Not found")
	ErrInvalidId         = errors.New("Invalid id")
	ErrInvalidCollection = errors.New("Invalid collection")
	ErrUnauthorized      = errors.New("Unauthorized request")
	ErrNoSuchForm        = errors.New("Form does not exist")
)
View Source
var (
	NID_STRING int = len(ID_STRING)
)
View Source
var (
	SESSION_TIMEOUT time.Duration = time.Hour
)

Functions

func BadRequest

func BadRequest(w http.ResponseWriter)

returns 401 Bad Request

func Error

func Error(w http.ResponseWriter, r *Request, err error)

returns 500 Internal Server Error, and prints the error to the server log

func FullRedirect

func FullRedirect(w http.ResponseWriter, r *Request, url string)

redirects an external resource

func GenerateKeyId

func GenerateKeyId() (id string, err error)

generates a new Id by concatenating the current time in nanoseconds with 16 random bytes

func HandlerInfo

func HandlerInfo(h RequestHandler) (name string, file string, line int)

For debugging purposes only

func JSONResult

func JSONResult(w http.ResponseWriter, r *Request, success bool, data interface{})

sends a { 'success': <bool>, 'message': <custom data> } response to the client

func LogError

func LogError(r *Request, err error)

func MD5Sum

func MD5Sum(s string) string

func NoContent

func NoContent(w http.ResponseWriter)

returns 204 No Content

func NotFound

func NotFound(w http.ResponseWriter)

func RandomId

func RandomId(length int) (id string)

func Redirect

func Redirect(w http.ResponseWriter, r *Request, redirectPath string)

redirects a request to a path relative to the module if the request is an

func Unauthorized

func Unauthorized(w http.ResponseWriter, err error)

returns 401 Unauthorized

func XHRRedirect

func XHRRedirect(w http.ResponseWriter, r *Request, url string)

redirects to a full URL

Types

type GithubIssue

type GithubIssue struct {
	Title  string   `json:"title"`
	Body   string   `json:"body"`
	Labels []string `json:"labels,omitempty"`
}

type GithubResponse

type GithubResponse struct {
	Url         string `json:"url"`
	LabelsUrl   string `json:"labels_url"`
	CommentsUrl string `json:"comments_url"`
	HtmlUrl     string `json:"html_url"`
	IssueNumber int    `json:"number"`
}

type HTTPMux

type HTTPMux struct {
	Handlers map[string]routeHandlers

	HasStaticFiles bool
	// contains filtered or unexported fields
}

This Request Mux does not use a mutex because we're not anticipating the need to change the routes at run time, as requests are served. Even if modules are re-mounted, they should be first instantiated, then enabled. Should this change, an RWMutex will be necessary.

func NewHTTPMux

func NewHTTPMux() *HTTPMux

returns a new Mux

func (*HTTPMux) Delete

func (h *HTTPMux) Delete(path string, handler RequestHandler)

registers a DELETE request handler

func (*HTTPMux) FindHandler

func (h *HTTPMux) FindHandler(r *Request) RequestHandler

returns a static/dynamic request handler for the given request

func (*HTTPMux) Get

func (h *HTTPMux) Get(path string, handler RequestHandler)

registers a GET request handler

func (*HTTPMux) Handle

func (h *HTTPMux) Handle(method string, path string, handler RequestHandler)

generic method that registers a handler for a path and http method

func (*HTTPMux) Head

func (h *HTTPMux) Head(path string, handler RequestHandler)

registers a HEAD request handler

func (*HTTPMux) Post

func (h *HTTPMux) Post(path string, handler RequestHandler)

registers a POST request handler

func (*HTTPMux) Put

func (h *HTTPMux) Put(path string, handler RequestHandler)

registers a PUT request handler

func (*HTTPMux) Route

func (h *HTTPMux) Route(w http.ResponseWriter, r *Request)

finds and invokes the Handlers for the given request

func (*HTTPMux) Static

func (h *HTTPMux) Static(path string)

sets the static path

func (*HTTPMux) StaticHandler

func (h *HTTPMux) StaticHandler(w http.ResponseWriter, r *Request)

a request handler for static resources

func (*HTTPMux) StaticPrefix

func (h *HTTPMux) StaticPrefix() string

type JSONResponse

type JSONResponse struct {
	Success bool        `json:"success"`
	Message interface{} `json:"message,omitempty"`
}

type Module

type Module struct {
	Mux
	Name           string
	MountPoint     string
	Path           string
	SessionTimeout time.Duration

	Db  orm.Database
	Log *log.Logger

	Templates *template.Template
}

A Perfect Module is a standalone component that can be mounted on URL paths and can decide how requests are routed.

func (*Module) ParseTemplates

func (m *Module) ParseTemplates() error

parses all template files from the 'templates' folder of the module

func (*Module) RenderTemplate

func (m *Module) RenderTemplate(w http.ResponseWriter, r *Request, path string, data interface{})

renders a template file

type ModuleMux

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

decides which Module handles which Request

var Modules *ModuleMux = NewModuleMux()

the default module mux

func NewModuleMux

func NewModuleMux() *ModuleMux

Returns a new ModuleMux

func (*ModuleMux) GetModule

func (mux *ModuleMux) GetModule(path string) (module, mpath string)

searches for a module by the path it's been mounted on

func (*ModuleMux) Mount

func (mux *ModuleMux) Mount(m *Module, path string)

Registers a new Module for a URL path

func (*ModuleMux) ServeHTTP

func (mux *ModuleMux) ServeHTTP(w http.ResponseWriter, r *http.Request)

Finds the requested module and hands off the request to its router

func (*ModuleMux) Unmount

func (mux *ModuleMux) Unmount(path string)

Unregisters the module that handles the path

type Mux

type Mux interface {
	Router
	Handle(method, path string, handler RequestHandler)
	Get(path string, handler RequestHandler)
	Post(path string, handler RequestHandler)
	Put(path string, handler RequestHandler)
	Delete(path string, handler RequestHandler)
	Head(path string, handler RequestHandler)

	Static(path string)
	StaticPrefix() string
}

type PrettyMux

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

func NewPrettyMux

func NewPrettyMux() *PrettyMux

func (*PrettyMux) Delete

func (pm *PrettyMux) Delete(path string, handler RequestHandler)

registers a DELETE request handler

func (*PrettyMux) FindHandler

func (pm *PrettyMux) FindHandler(r *Request) RequestHandler

returns a static/dynamic request handler for the given request

func (*PrettyMux) Get

func (pm *PrettyMux) Get(expr string, handler RequestHandler)

registers a GET request handler

func (*PrettyMux) Handle

func (pm *PrettyMux) Handle(method string, expr string, handler RequestHandler)

generic method that registers a handler for a path and http method

func (*PrettyMux) Head

func (pm *PrettyMux) Head(path string, handler RequestHandler)

registers a HEAD request handler

func (*PrettyMux) Post

func (pm *PrettyMux) Post(path string, handler RequestHandler)

registers a POST request handler

func (*PrettyMux) Put

func (pm *PrettyMux) Put(path string, handler RequestHandler)

registers a PUT request handler

func (*PrettyMux) Route

func (pm *PrettyMux) Route(w http.ResponseWriter, r *Request)

finds and invokes the Handlers for the given request

func (*PrettyMux) Static

func (pm *PrettyMux) Static(path string)

sets the static path

func (*PrettyMux) StaticHandler

func (pm *PrettyMux) StaticHandler(w http.ResponseWriter, r *Request)

a request handler for static resources

func (*PrettyMux) StaticPrefix

func (pm *PrettyMux) StaticPrefix() string

type PrivateKey

type PrivateKey struct {
	Id   string
	Type int
	*ecdsa.PrivateKey
}

func GeneratePrivateKey

func GeneratePrivateKey(key_type int) (private_key *PrivateKey, err error)

generate a new Elliptical private key using the P521 curve and /dev/urandom

func NewPrivateKey

func NewPrivateKey(key_type int) *PrivateKey

func (*PrivateKey) Equals

func (key *PrivateKey) Equals(key2 *PrivateKey) bool

func (*PrivateKey) GetBSON

func (key *PrivateKey) GetBSON() (interface{}, error)

BSON

func (*PrivateKey) MarshalJSON

func (key *PrivateKey) MarshalJSON() (data []byte, err error)

JSON.stringify

func (*PrivateKey) SetBSON

func (key *PrivateKey) SetBSON(raw bson.Raw) (err error)

func (*PrivateKey) UnmarshalJSON

func (key *PrivateKey) UnmarshalJSON(data []byte) (err error)

JSON.parse

type Profile

type Profile struct {
	orm.Object `bson:",inline,omitempty" json:"-"`
	Id         *string   `bson:"id,omitempty" json:"id,omitempty"`
	Name       *string   `bson:"name,omitempty" json:"name,omitempty"`
	Groups     *[]string `bson:"groups,omitempty" json:"groups,omitempty"`
	AuthType   *string   `bson:"auth_type,omitempty" json:"auth_type,omitempty"`
}

func NewProfile

func NewProfile(email, name string) *Profile

type Request

type Request struct {
	*http.Request
	URL    *url.URL
	Module *Module // the module that's handling the request

	Values url.Values
	// contains filtered or unexported fields
}

A struct that wraps http.Request and provides additional fields for all RequestHandlers to use. All methods from http.Request should be promoted for use with survana.Request.

func NewRequest

func NewRequest(r *http.Request, path string, module *Module) *Request

returns a new Request object

func (*Request) BodyBytes

func (r *Request) BodyBytes(body io.ReadCloser) (result []byte, err error)

func (*Request) Cookie

func (r *Request) Cookie(name string) (value string, ok bool)

returns the value of the cookie by name

func (*Request) JSONBody

func (r *Request) JSONBody(body io.ReadCloser, v interface{}) (err error)

func (*Request) ParseJSON

func (r *Request) ParseJSON(v interface{}) (err error)

Parses the request body as a JSON-encoded string

func (*Request) Profile

func (r *Request) Profile() (*Profile, error)

returns nil, nil if the profile was not found

func (*Request) Session

func (r *Request) Session() (*Session, error)

returns either an existing session, or a new session

func (*Request) SetSession

func (r *Request) SetSession(s *Session)

func (*Request) StringBody

func (r *Request) StringBody(body io.ReadCloser) (result string, err error)

type RequestHandler

type RequestHandler func(http.ResponseWriter, *Request)

An HTTP request to a Survana component.

func NotLoggedIn

func NotLoggedIn(handler RequestHandler) RequestHandler

type Router

type Router interface {
	Route(w http.ResponseWriter, r *Request)
}

An interface for any type that can route Survana requests

type Session

type Session struct {
	orm.Object    `bson:",inline,omitempty" json:"-"`
	Id            *string            `bson:"id,omitempty" json:"id"`           //the publicly visible session id
	ProfileId     *string            `bson:"profile_id,omitempty" json:"-"`    //the profile id this session is associated with
	Authenticated *bool              `bson:"authenticated,omitempty" json:"-"` //whether the user has logged in or not
	Values        *map[string]string `bson:"values,omitempty" json:"-"`        //all other values go here
}

Represents a user's session. Id and _id are kept separate so that in the future, Id's can be regenerated on every request. Id and Authenticated are aliases for Values['id'] and Values['authenticated']

func NewSession

func NewSession(id string) *Session

creates a new Session object with no Id.

func (*Session) ExtendCookie

func (session *Session) ExtendCookie(w http.ResponseWriter, r *Request)

func (*Session) RemoveCookie

func (session *Session) RemoveCookie(w http.ResponseWriter, r *Request)

func (*Session) SetCookie

func (session *Session) SetCookie(w http.ResponseWriter, r *Request)

Directories

Path Synopsis
Package json implements encoding and decoding of JSON objects as defined in RFC 4627.
Package json implements encoding and decoding of JSON objects as defined in RFC 4627.
orm

Jump to

Keyboard shortcuts

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