Documentation ¶
Index ¶
- Constants
- Variables
- func BadRequest(w http.ResponseWriter)
- func Error(w http.ResponseWriter, r *Request, err error)
- func FullRedirect(w http.ResponseWriter, r *Request, url string)
- func GenerateKeyId() (id string, err error)
- func HandlerInfo(h RequestHandler) (name string, file string, line int)
- func JSONResult(w http.ResponseWriter, r *Request, success bool, data interface{})
- func LogError(r *Request, err error)
- func MD5Sum(s string) string
- func NoContent(w http.ResponseWriter)
- func NotFound(w http.ResponseWriter)
- func RandomId(length int) (id string)
- func Redirect(w http.ResponseWriter, r *Request, redirectPath string)
- func Unauthorized(w http.ResponseWriter, err error)
- func XHRRedirect(w http.ResponseWriter, r *Request, url string)
- type GithubIssue
- type GithubResponse
- type HTTPMux
- func (h *HTTPMux) Delete(path string, handler RequestHandler)
- func (h *HTTPMux) FindHandler(r *Request) RequestHandler
- func (h *HTTPMux) Get(path string, handler RequestHandler)
- func (h *HTTPMux) Handle(method string, path string, handler RequestHandler)
- func (h *HTTPMux) Head(path string, handler RequestHandler)
- func (h *HTTPMux) Post(path string, handler RequestHandler)
- func (h *HTTPMux) Put(path string, handler RequestHandler)
- func (h *HTTPMux) Route(w http.ResponseWriter, r *Request)
- func (h *HTTPMux) Static(path string)
- func (h *HTTPMux) StaticHandler(w http.ResponseWriter, r *Request)
- func (h *HTTPMux) StaticPrefix() string
- type JSONResponse
- type Module
- type ModuleMux
- type Mux
- type PrettyMux
- func (pm *PrettyMux) Delete(path string, handler RequestHandler)
- func (pm *PrettyMux) FindHandler(r *Request) RequestHandler
- func (pm *PrettyMux) Get(expr string, handler RequestHandler)
- func (pm *PrettyMux) Handle(method string, expr string, handler RequestHandler)
- func (pm *PrettyMux) Head(path string, handler RequestHandler)
- func (pm *PrettyMux) Post(path string, handler RequestHandler)
- func (pm *PrettyMux) Put(path string, handler RequestHandler)
- func (pm *PrettyMux) Route(w http.ResponseWriter, r *Request)
- func (pm *PrettyMux) Static(path string)
- func (pm *PrettyMux) StaticHandler(w http.ResponseWriter, r *Request)
- func (pm *PrettyMux) StaticPrefix() string
- type PrivateKey
- type Profile
- type Request
- func (r *Request) BodyBytes(body io.ReadCloser) (result []byte, err error)
- func (r *Request) Cookie(name string) (value string, ok bool)
- func (r *Request) JSONBody(body io.ReadCloser, v interface{}) (err error)
- func (r *Request) ParseJSON(v interface{}) (err error)
- func (r *Request) Profile() (*Profile, error)
- func (r *Request) Session() (*Session, error)
- func (r *Request) SetSession(s *Session)
- func (r *Request) StringBody(body io.ReadCloser) (result string, err error)
- type RequestHandler
- type Router
- type Session
Constants ¶
const ( EC_P521 = 521 EC_P384 = 384 )
const ( TEMPLATE_DIR = "templates" TEMPLATE_EXT = ".html" )
const GITHUB_URL = "https://api.github.com/repos/vpetrov/survana/issues?access_token=a8202d411154a49dab3a42b6e46fc51549ce651a"
TODO: remove this or make it configurable
const (
ID_STRING = "AaBbCcDdEeFfGgHhJjKLMmNnoPpqRrSsTtUuVvWwXxYyZz3456789"
)
const (
SESSION_ID = "SSESSIONID"
)
Variables ¶
var ( ErrEmptyRequest = errors.New("Empty request") ErrNotFound = errors.New("Not found") ErrInvalidId = errors.New("Invalid id") ErrInvalidCollection = errors.New("Invalid collection") ErrNoSuchForm = errors.New("Form does not exist") )
var (
NID_STRING int = len(ID_STRING)
)
var (
SESSION_TIMEOUT time.Duration = time.Hour
)
Functions ¶
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 ¶
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 NotFound ¶
func NotFound(w http.ResponseWriter)
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 XHRRedirect ¶
func XHRRedirect(w http.ResponseWriter, r *Request, url string)
redirects to a full URL
Types ¶
type GithubIssue ¶
type GithubResponse ¶
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 (*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) StaticHandler ¶
func (h *HTTPMux) StaticHandler(w http.ResponseWriter, r *Request)
a request handler for static resources
func (*HTTPMux) StaticPrefix ¶
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 ¶
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
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) StaticHandler ¶
func (pm *PrettyMux) StaticHandler(w http.ResponseWriter, r *Request)
a request handler for static resources
func (*PrettyMux) StaticPrefix ¶
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) MarshalJSON ¶
func (key *PrivateKey) MarshalJSON() (data []byte, err error)
JSON.stringify
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 ¶
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 ¶
returns a new Request object
func (*Request) BodyBytes ¶
func (r *Request) BodyBytes(body io.ReadCloser) (result []byte, err error)
func (*Request) JSONBody ¶
func (r *Request) JSONBody(body io.ReadCloser, v interface{}) (err error)
func (*Request) SetSession ¶
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 (*Session) ExtendCookie ¶
func (session *Session) ExtendCookie(w http.ResponseWriter, r *Request)
func (*Session) RemoveCookie ¶
func (session *Session) RemoveCookie(w http.ResponseWriter, r *Request)