view

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

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

Go to latest
Published: Jul 15, 2014 License: MIT Imports: 23 Imported by: 0

README

view

Documentation

Index

Constants

View Source
const URLArgPlaceholder = "(*)"

Variables

View Source
var Port80 = Server{
	Address:      "0.0.0.0:80",
	BaseDirs:     []string{"."},
	StaticDirs:   []string{"static"},
	TemplateDirs: []string{"templates"},
}
View Source
var Port8080 = Server{
	Address:      "0.0.0.0:8080",
	BaseDirs:     []string{"."},
	StaticDirs:   []string{"static"},
	TemplateDirs: []string{"templates"},
}
View Source
var SessionIDCookie = "session"

Functions

func HTTPHandler

func HTTPHandler(server *Server, view View, urlArgs ...string) http.Handler

Types

type AESBase64Cipher

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

func NewAESBase64Cipher

func NewAESBase64Cipher(key []byte) (*AESBase64Cipher, error)

func (*AESBase64Cipher) Decrypt

func (c *AESBase64Cipher) Decrypt(cryptoText string) []byte

decrypt from base64 to decrypted string

func (*AESBase64Cipher) Encrypt

func (c *AESBase64Cipher) Encrypt(plaintext []byte) string

encrypt string to base64 crypto using AES

type Bytes

type Bytes []byte

func (Bytes) Render

func (bytes Bytes) Render(ctx *Context) error

type Context

type Context struct {
	Server   *Server
	Request  *Request
	Response *Response
	Session  *Session

	// Arguments parsed from the URL path
	URLArgs []string

	// Custom response wide data that can be set by the application
	Data      interface{}
	DebugData interface{}
}

func NewContext

func NewContext(server *Server, responseWriter http.ResponseWriter, httpRequest *http.Request, urlArgs ...string) *Context

func (*Context) ForURLArgs

func (self *Context) ForURLArgs(urlArgs ...string) *Context

ForURLArgs returns an altered Context copy where Context.URLArgs is set to urlArgs. Can be used for calling the the URL() method of a URL interface to get the URL of another view, defined by urlArgs.

The following example gets the URL of MyPage with the first URL argument is that of the current page and the second URL argument is "second-arg":

MyPage.URL(ctx.ForURLArgs(ctx.URLArgs[0], "second-arg"))

func (*Context) ForURLArgsConvert

func (self *Context) ForURLArgsConvert(urlArgs ...interface{}) *Context

type CookieSessionDataStore

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

func (*CookieSessionDataStore) Delete

func (sessionDataStore *CookieSessionDataStore) Delete(ctx *Context) (err error)

func (*CookieSessionDataStore) Get

func (sessionDataStore *CookieSessionDataStore) Get(ctx *Context, data interface{}) (ok bool, err error)

func (*CookieSessionDataStore) Set

func (sessionDataStore *CookieSessionDataStore) Set(ctx *Context, data interface{}) (err error)

type CookieSessionTracker

type CookieSessionTracker struct {
}

http://en.wikipedia.org/wiki/HTTP_cookie

func (*CookieSessionTracker) DeleteID

func (*CookieSessionTracker) DeleteID(ctx *Context)

func (*CookieSessionTracker) ID

func (*CookieSessionTracker) SetID

func (*CookieSessionTracker) SetID(ctx *Context, id string)

type Error

type Error struct {
	Err error
}

Error returns Err when Render is called. Error does not implement ViewError to prevent endless loops.

func (Error) Render

func (err Error) Render(ctx *Context) error

type GetViewFunc

type GetViewFunc func(ctx *Context) View

GetViewFunc is a function pointer that implements View. At every Render method call, the function will be called and Render will be called at the result View if it is not nil.

See also ViewFunc

func (GetViewFunc) Render

func (getView GetViewFunc) Render(ctx *Context) (err error)

type Pointer

type Pointer struct {
	Ptr *View
}

func (Pointer) Render

func (pointer Pointer) Render(ctx *Context) error

type Request

type Request struct {
	*http.Request

	Params map[string]string
	// contains filtered or unexported fields
}

func (*Request) AddProtocolAndHostToURL

func (request *Request) AddProtocolAndHostToURL(url string) string

AddProtocolAndHostToURL adds the protocol (http:// or https://) and request host (domain or IP) to an URL if not present.

func (*Request) Port

func (request *Request) Port() uint16

func (*Request) SiteCookie

func (request *Request) SiteCookie(name string) (string, bool)

func (*Request) SiteCookieBytes

func (request *Request) SiteCookieBytes(name string) ([]byte, bool)

func (*Request) URLString

func (request *Request) URLString() string

URL returns the complete URL of the request including protocol and host.

type Response

type Response struct {
	bytes.Buffer
	// contains filtered or unexported fields
}

func (*Response) DeleteSiteCookie

func (response *Response) DeleteSiteCookie(name string)

func (*Response) GetBytesAndReset

func (response *Response) GetBytesAndReset() []byte

GetBytesAndReset() returns a copy of the response buffer and resets it.

func (*Response) GetStringAndReset

func (response *Response) GetStringAndReset() string

GetStringAndReset() returns a copy of the response buffer and resets it.

func (*Response) Header

func (response *Response) Header() http.Header

func (*Response) Out

func (response *Response) Out(s string) *Response

Out does the same as WriteString, except that it returns *Response to allow call chaining.

func (*Response) Print

func (response *Response) Print(a ...interface{}) (n int, err error)

func (*Response) Printf

func (response *Response) Printf(format string, a ...interface{}) (n int, err error)

func (*Response) Println

func (response *Response) Println(a ...interface{}) (n int, err error)

func (*Response) RequireHeadScript

func (response *Response) RequireHeadScript(script string, priority int)

RequireHeadScript adds dynamic JavaScript to the page. Multiple dynamic entries will be sorted by priority. The dynamic JavaScript will be inserted after the regular head-scripts of the page. If script does not start with "<script", then the script string will be wrapped with a script tag.

Use this feature to dynamically add JavaScript to the page if the HTML content requires it.

func (*Response) RequireHeadScriptURL

func (response *Response) RequireHeadScriptURL(url string, priority int)

RequireHeadScriptURL adds a dynamic JavaScript link to the page. Multiple dynamic entries will be sorted by priority. The dynamic JavaScript will be inserted after the regular head-scripts of the page.

Use this feature to dynamically add JavaScript to the page if the HTML content requires it.

func (*Response) RequireScript

func (response *Response) RequireScript(script string, priority int)

RequireScript adds dynamic JavaScript to the page. Multiple dynamic entries will be sorted by priority. The dynamic JavaScript will be inserted after the regular scripts near the end of the page. If script does not start with "<script", then the script string will be wrapped with a script tag.

Use this feature to dynamically add JavaScript to the page if the HTML content requires it.

func (*Response) RequireScriptURL

func (response *Response) RequireScriptURL(url string, priority int)

RequireScriptURL adds a dynamic JavaScript link to the page. Multiple dynamic entries will be sorted by priority. The dynamic JavaScript will be inserted after the regular scripts near the end of the page.

Use this feature to dynamically add JavaScript to the page if the HTML content requires it.

func (*Response) RequireStyle

func (response *Response) RequireStyle(css string, priority int)

RequireStyle adds dynamic CSS content to the page. Multiple dynamic entries will be sorted by priority. Dynamic CSS will be inserted after the regular CSS of the page. If css does not start with "<style", then the css string will be wrapped with a style tag.

Use this feature to dynamically add CSS to the page if the HTML content requires it.

func (*Response) RequireStyleURL

func (response *Response) RequireStyleURL(url string, priority int)

RequireStyleURL adds a dynamic CSS link to the page. Multiple dynamic entries will be sorted by priority. Dynamic CSS will be inserted after the regular CSS of the page.

Use this feature to dynamically add CSS to the page if the HTML content requires it.

func (*Response) Respond

func (response *Response) Respond(code int, body string)

func (*Response) RespondPlainText

func (response *Response) RespondPlainText(code int, body string)

func (*Response) SetContentType

func (response *Response) SetContentType(contentType string)

func (*Response) SetContentTypeAttachment

func (response *Response) SetContentTypeAttachment(filename string)

SetContentTypeAttachment makes the webbrowser open a "Save As.." dialog for the response.

func (*Response) SetContentTypeByExt

func (response *Response) SetContentTypeByExt(ext string)

func (*Response) SetContentTypeHTML

func (response *Response) SetContentTypeHTML()

func (*Response) SetContentTypeJSON

func (response *Response) SetContentTypeJSON()

func (*Response) SetContentTypePlainText

func (response *Response) SetContentTypePlainText()

func (*Response) SetContentTypeXML

func (response *Response) SetContentTypeXML()

func (*Response) SetSiteCookie

func (response *Response) SetSiteCookie(name, value string)

func (*Response) SetSiteCookieBytes

func (response *Response) SetSiteCookieBytes(name string, value []byte)

func (*Response) SetSiteCookieBytesExpires

func (response *Response) SetSiteCookieBytesExpires(name string, value []byte, expires time.Time)

func (*Response) SetSiteCookieExpires

func (response *Response) SetSiteCookieExpires(name, value string, expires time.Time)

func (*Response) WriteHeader

func (response *Response) WriteHeader(code int)

type Server

type Server struct {
	Address             string
	ReadTimeoutMs       int
	WriteTimeoutMs      int
	NoKeepAlives        bool
	TLSCertFile         string
	TLSKeyFile          string
	IsProductionServer  bool // IsProductionServer will be set to true if localhost resolves to one of ProductionServerIPs
	ProductionServerIPs []string
	DisableCachedViews  bool
	BaseDirs            []string
	StaticDirs          []string
	TemplateDirs        []string
	RedirectSubdomains  []string // Exapmle: "www"
	SiteName            string
	CookieSecret        []byte           // nil to disable cookie-encryption, or AES key 16, 24, or 32 bytes to select AES-128, AES-192, or AES-256
	CookieCipher        *AESBase64Cipher // will get initialized when when len(CookieSecret) > 0
	TrackSessions       bool
	SessionTracker      SessionTracker
	SessionDataStore    SessionDataStore

	Debug struct {
		ListenAndServeAt string
		Mode             bool // Will be set to true if IsProductionServer is false
		LogPaths         bool
		LogRedirects     bool
	}
	// contains filtered or unexported fields
}

func (*Server) Deploy

func (server *Server) Deploy(targetDir string) error

Deploy copies all known resources (static and template files) to targetDir

func (*Server) FindStaticFile

func (server *Server) FindStaticFile(filename string) (filePath string, found bool, modified time.Time)

func (*Server) FindTemplateFile

func (server *Server) FindTemplateFile(filename string) (filePath string, found bool, modified time.Time)

func (*Server) Init

func (server *Server) Init() (err error)

type Session

type Session struct {
	Tracker   SessionTracker
	DataStore SessionDataStore
	Ctx       *Context
	// contains filtered or unexported fields
}

func (*Session) Data

func (session *Session) Data(out interface{}) (ok bool, err error)

SessionData returns all session data in out.

func (*Session) DeleteData

func (session *Session) DeleteData() (err error)

DeleteSessionData deletes all session data.

func (*Session) DeleteID

func (session *Session) DeleteID()

func (*Session) ID

func (session *Session) ID() string

ID returns the id of the session or an empty string. It's valid to call this method on a nil pointer.

func (*Session) SetData

func (session *Session) SetData(data interface{}) (err error)

SetSessionData sets all session data.

func (*Session) SetID

func (session *Session) SetID(id string)

type SessionDataStore

type SessionDataStore interface {
	Get(ctx *Context, data interface{}) (ok bool, err error)
	Set(ctx *Context, data interface{}) (err error)
	Delete(ctx *Context) (err error)
}

func NewCookieSessionDataStore

func NewCookieSessionDataStore() SessionDataStore

type SessionTracker

type SessionTracker interface {
	ID(ctx *Context) (id string)
	SetID(ctx *Context, id string)
	DeleteID(ctx *Context)
}

type StaticFile

type StaticFile struct {
	// ViewWithURLBase
	Filename string
	// Will be set automatically from Filename if empty
	ContentTypeExt string
	// contains filtered or unexported fields
}

StaticFile renders a static file. The output is cached in memory but changes to the file on the filesystem cause the the cache to be rebuilt.

func NewStaticFile

func NewStaticFile(filename string) *StaticFile

func (*StaticFile) Render

func (self *StaticFile) Render(ctx *Context) (err error)

type String

type String string

func Print

func Print(a ...interface{}) String

func Printf

func Printf(format string, a ...interface{}) String

func Println

func Println(a ...interface{}) String

func (String) Render

func (str String) Render(ctx *Context) (err error)

type Template

type Template struct {
	Filename       string // Will set file extension at ContentType
	Text           string
	ContentTypeExt string
}

func (*Template) Render

func (template *Template) Render(ctx *Context) (err error)

type URL

type URL interface {
	GetURL(ctx *Context) string
}

URL is an interface to return URL strings depending on the request path args.

type URLFunc

type URLFunc func(ctx *Context) string

func (URLFunc) GetURL

func (urlFunc URLFunc) GetURL(ctx *Context) string

type URLString

type URLString string

URLString implements the URL interface for a URL string.

func (URLString) GetURL

func (self URLString) GetURL(ctx *Context) string

type URLView

type URLView struct {
	URL
}

URLView renders an URL.

func (URLView) Render

func (urlView URLView) Render(ctx *Context) (err error)

type View

type View interface {
	Render(ctx *Context) (err error)
}

View is the basic interface for all types in the view package.

func AsView

func AsView(values ...interface{}) View

func NonProductionServerView

func NonProductionServerView(server *Server, view View) View

NonProductionServerView returns view if server.IsProductionServer is false, else nil which is a valid value for a View.

func ProductionServerView

func ProductionServerView(server *Server, view View) View

ProductionServerView returns view if server.IsProductionServer is true, else nil which is a valid value for a View.

type ViewError

type ViewError interface {
	View
	error
}
var (
	ErrNotModified304           ViewError = &response{304, "not modfied"}
	ErrAuthorizationRequired401 ViewError = &plainTextResponse{401, "401 authorization required"}
	ErrForbidden403             ViewError = &plainTextResponse{403, "403 forbidden"}
	ErrNotFound404              ViewError = &plainTextResponse{404, "404 page not found"}
)

func ErrInternalServerError500

func ErrInternalServerError500(err error) ViewError

func ErrRedirectPermanently301

func ErrRedirectPermanently301(url URL) ViewError

func ErrRedirectTemporary302

func ErrRedirectTemporary302(url URL) ViewError

type ViewFunc

type ViewFunc func(ctx *Context) error

See also GetViewFunc

func (ViewFunc) Render

func (viewFunc ViewFunc) Render(ctx *Context) error

type Views

type Views []View

Views implements the View interface for a slice of views.

func AsViews

func AsViews(values ...interface{}) Views

func (*Views) Insert

func (views *Views) Insert(index int, view View)

func (*Views) Remove

func (views *Views) Remove(index int)

func (Views) Render

func (views Views) Render(ctx *Context) (err error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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