Documentation ¶
Overview ¶
Web abstracts http request and response handling. Includes additional features like session handling, input data, and redirects.
Index ¶
- Constants
- func CreateToken() string
- func GetDefaultAllowedFileExtensions() []string
- func SetPageSize(size int)
- type Handler
- type Menu
- type MenuItem
- type Page
- func (p Page) GetJsonParams() string
- func (p Page) GetNextUrl() string
- func (p Page) GetPageUrl(page uint) string
- func (p Page) GetParamUrl(key, value string) string
- func (p Page) GetPrevUrl() string
- func (p Page) GetUrl(params map[string]string) string
- func (p Page) IsFirstPage() bool
- func (p Page) IsLastPage() bool
- func (p Page) Needed() bool
- func (p Page) Next() uint
- func (p Page) Prev() uint
- type Renderer
- type Request
- func (r *Request) GetBody() []byte
- func (r *Request) GetCookie(key string) string
- func (r *Request) GetCookieBool(key string) bool
- func (r *Request) GetCsrfToken() (string, error)
- func (r *Request) GetFormValue(key string) string
- func (r *Request) GetFormValueBool(key string) bool
- func (r *Request) GetFormValueFloat(key string) float32
- func (r *Request) GetFormValueFloat64(key string) float64
- func (r *Request) GetFormValueInt(key string) int
- func (r *Request) GetFormValueInt64(key string) int64
- func (r *Request) GetFormValueIntSlice(key string) []int
- func (r *Request) GetFormValueSlice(key string) []string
- func (r *Request) GetFormValueUint(key string) uint
- func (r *Request) GetFormValueUint64(key string) uint64
- func (r *Request) GetHeader(key string) string
- func (r *Request) GetPotentialFilename() string
- func (r *Request) GetSourceIP() string
- func (r *Request) GetSourceIPAddr() net.IP
- func (r *Request) GetURI() string
- func (r *Request) GetUrlNamedQueryVariable(key string) string
- func (r *Request) GetUrlNamedQueryVariableInt(key string) int
- func (r *Request) GetUrlNamedQueryVariableUInt(key string) uint
- func (r *Request) GetUrlParameter(key string) string
- func (r *Request) GetUrlParameterBool(key string) bool
- func (r *Request) GetUrlParameterInt(key string) int
- func (r *Request) GetUrlParameterSet(key string) bool
- func (r *Request) GetUrlParameterUInt(key string) uint
- type Response
- func (r *Response) Error(err error, responseCode int)
- func (r *Response) GetCsrfToken() string
- func (r *Response) GetResponseCode() int
- func (r *Response) GetWebSocket() (*Socket, error)
- func (r *Response) InitSession()
- func (r *Response) IsValidCsrf() bool
- func (r *Response) LogComplete()
- func (r *Response) Render() error
- func (r *Response) RenderTemplate(templateName string) error
- func (r *Response) ResetOrCreateSession()
- func (r *Response) ResponseCodeSet() bool
- func (r *Response) SetCookie(key string, value string)
- func (r *Response) SetFuncMap(funcMap map[string]interface{})
- func (r *Response) SetHeader(key string, value string)
- func (r *Response) SetRedirect(location string)
- func (r *Response) SetResponseCode(code int) error
- func (r *Response) SetSessionCookie(cookie string)
- func (r *Response) Write(s string)
- func (r *Response) WriteJson(i interface{}, pretty bool)
- type Route
- type Server
- type Session
- type Socket
- type UrlParam
- type UrlParamType
Examples ¶
Constants ¶
const CookieName = "session_id"
Name of cookie used for sessions.
const (
DefaultPageSize = 25
)
const MaxRoutesDisplay = 3
Maximum number of routes to display when starting up.
const UnableToFindTemplateErrorMsg = "unable to find template"
Variables ¶
This section is empty.
Functions ¶
func CreateToken ¶
func CreateToken() string
Example ¶
package main import ( "github.com/jchavannes/jgo/web" ) func main() { token := web.CreateToken() println(token) }
Output:
func GetDefaultAllowedFileExtensions ¶
func GetDefaultAllowedFileExtensions() []string
Default extensions allowed for static files.
func SetPageSize ¶
func SetPageSize(size int)
Types ¶
type Menu ¶
func (Menu) GetActiveTitle ¶
type Page ¶
type Page struct { Page uint Offset uint Url string Sel string ItmCnt int Size int Params map[string]string Hash string }
func (Page) GetJsonParams ¶
func (Page) GetNextUrl ¶
func (Page) GetPageUrl ¶
func (Page) GetParamUrl ¶
func (Page) GetPrevUrl ¶
func (Page) IsFirstPage ¶
func (Page) IsLastPage ¶
type Renderer ¶
type Renderer struct {
// contains filtered or unexported fields
}
func GetRenderer ¶
func (*Renderer) SetFuncMap ¶
type Request ¶
func (*Request) GetCookieBool ¶
func (*Request) GetCsrfToken ¶
func (*Request) GetFormValue ¶
func (*Request) GetFormValueBool ¶
func (*Request) GetFormValueFloat ¶
func (*Request) GetFormValueFloat64 ¶
func (*Request) GetFormValueInt ¶
func (*Request) GetFormValueInt64 ¶
func (*Request) GetFormValueIntSlice ¶
func (*Request) GetFormValueSlice ¶
func (*Request) GetFormValueUint ¶
func (*Request) GetFormValueUint64 ¶
func (*Request) GetPotentialFilename ¶
func (*Request) GetSourceIP ¶
func (*Request) GetSourceIPAddr ¶
func (*Request) GetUrlNamedQueryVariable ¶
func (*Request) GetUrlNamedQueryVariableInt ¶
func (*Request) GetUrlNamedQueryVariableUInt ¶
func (*Request) GetUrlParameter ¶
func (*Request) GetUrlParameterBool ¶
func (*Request) GetUrlParameterInt ¶
func (*Request) GetUrlParameterSet ¶
func (*Request) GetUrlParameterUInt ¶
type Response ¶
type Response struct { Helper map[string]interface{} StartTs time.Time Pattern string Request Request Server *Server Session Session Static bool Writer http.ResponseWriter // contains filtered or unexported fields }
Response objects are passed to handlers to respond to requests. Includes abstracted access to request and session information
func (*Response) GetCsrfToken ¶
func (*Response) GetResponseCode ¶
func (*Response) GetWebSocket ¶
func (*Response) InitSession ¶
func (r *Response) InitSession()
Either gets existing session token or creates a new one.
func (*Response) IsValidCsrf ¶
Checks that CSRF token in request matches one for session. Tokens are kept in memory and do not persist between instances or restarts.
func (*Response) LogComplete ¶
func (r *Response) LogComplete()
func (*Response) RenderTemplate ¶
func (*Response) ResetOrCreateSession ¶
func (r *Response) ResetOrCreateSession()
Sets a new session cookie.
func (*Response) ResponseCodeSet ¶
func (*Response) SetFuncMap ¶
func (*Response) SetRedirect ¶
func (*Response) SetResponseCode ¶
Example ¶
package main import ( "github.com/jchavannes/jgo/web" "net/http" ) func main() { web.Route{ Pattern: "/", Handler: func(r *web.Response) { r.SetResponseCode(http.StatusNotFound) }, } }
Output:
func (*Response) SetSessionCookie ¶
type Route ¶
type Server ¶
type Server struct { AllowedExtensions []string IsLoggedIn func(*Response) bool NotFoundHandler func(*Response) Port int PreHandler func(*Response) PostHandler func(*Response) ErrorHandler func(*Response, error) GetCsrfToken func(string) string Routes []Route SessionKey string StaticFilesDir string StrictSlash bool TemplatesDir string UseAutoRender bool UseSessions bool InsecureCookie bool CookiePrefix string NoLogStatic bool // contains filtered or unexported fields }
func (*Server) GetCookieName ¶
type Socket ¶
type Socket struct {
// contains filtered or unexported fields
}
func (*Socket) OnClose ¶
func (s *Socket) OnClose(closeHandler func())
OnClose event does not get triggered unless messages are read.
func (*Socket) ReadAllUntilClose ¶
ReadAllUntilClose throws away all incoming messages. If you want to read messages, don't use this function.
func (*Socket) ReadMessage ¶
type UrlParam ¶
type UrlParam struct { Id string Type UrlParamType }
type UrlParamType ¶
type UrlParamType string
const ( UrlParamInteger UrlParamType = "integer" UrlParamString UrlParamType = "string" UrlParamString2 UrlParamType = "string2" UrlParamString3 UrlParamType = "string3" UrlParamAlphaNumeric UrlParamType = "alpha-numeric" UrlParamAny UrlParamType = "any" )