web

package
v0.0.0-...-2f2839f Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2024 License: MIT Imports: 26 Imported by: 15

Documentation

Overview

Web abstracts http request and response handling. Includes additional features like session handling, input data, and redirects.

Index

Examples

Constants

View Source
const CookieName = "session_id"

Name of cookie used for sessions.

View Source
const (
	DefaultPageSize = 25
)
View Source
const LogCompleteExtraMessageHelperKey = "LogCompleteExtraMessage"
View Source
const MaxRoutesDisplay = 3

Maximum number of routes to display when starting up.

View Source
const UnableToFindTemplateErrorMsg = "unable to find template"
View Source
const WriteTimeoutErrorMsgPart = "write: broken pipe"

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 Handler

type Handler struct {
	Handler func(http.ResponseWriter, *http.Request)
}

func (Handler) ServeHTTP

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type Menu struct {
	DefaultTitle string
	ItemGroups   [][]*MenuItem
}
func (m Menu) GetActiveTitle() string
func (m Menu) SetActive(url string)
type MenuItem struct {
	Url    string
	Title  string
	Active bool
}

type Page

type Page struct {
	Page   uint
	Offset uint
	Url    string
	Sel    string
	ItmCnt int
	Size   int
	Params map[string]string
	Hash   string
}

func GetPage

func GetPage(r *Response, url string) *Page

func GetPageWithSize

func GetPageWithSize(r *Response, url string, size int) *Page

func (Page) GetJsonParams

func (p Page) GetJsonParams() string

func (Page) GetNextUrl

func (p Page) GetNextUrl() string

func (Page) GetPageUrl

func (p Page) GetPageUrl(page uint) string

func (Page) GetParamUrl

func (p Page) GetParamUrl(key, value string) string

func (Page) GetPrevUrl

func (p Page) GetPrevUrl() string

func (Page) GetUrl

func (p Page) GetUrl(params map[string]string) string

func (Page) IsFirstPage

func (p Page) IsFirstPage() bool

func (Page) IsLastPage

func (p Page) IsLastPage() bool

func (Page) Needed

func (p Page) Needed() bool

func (Page) Next

func (p Page) Next() uint

func (Page) Prev

func (p Page) Prev() uint

type Renderer

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

func GetRenderer

func GetRenderer(directory string) (*Renderer, error)

func (*Renderer) Render

func (r *Renderer) Render(names []string, writer io.Writer, data interface{}) error

func (*Renderer) SetFuncMap

func (r *Renderer) SetFuncMap(funcMap map[string]interface{})

type Request

type Request struct {
	HttpRequest http.Request
}

func (*Request) GetBody

func (r *Request) GetBody() []byte

func (*Request) GetCookie

func (r *Request) GetCookie(key string) string

func (*Request) GetCookieBool

func (r *Request) GetCookieBool(key string) bool

func (*Request) GetCsrfToken

func (r *Request) GetCsrfToken() (string, error)

func (*Request) GetFormValue

func (r *Request) GetFormValue(key string) string

func (*Request) GetFormValueBool

func (r *Request) GetFormValueBool(key string) bool

func (*Request) GetFormValueFloat

func (r *Request) GetFormValueFloat(key string) float32

func (*Request) GetFormValueFloat64

func (r *Request) GetFormValueFloat64(key string) float64

func (*Request) GetFormValueInt

func (r *Request) GetFormValueInt(key string) int

func (*Request) GetFormValueInt64

func (r *Request) GetFormValueInt64(key string) int64

func (*Request) GetFormValueIntSlice

func (r *Request) GetFormValueIntSlice(key string) []int

func (*Request) GetFormValueSlice

func (r *Request) GetFormValueSlice(key string) []string

func (*Request) GetFormValueUint

func (r *Request) GetFormValueUint(key string) uint

func (*Request) GetFormValueUint64

func (r *Request) GetFormValueUint64(key string) uint64

func (*Request) GetHeader

func (r *Request) GetHeader(key string) string

func (*Request) GetPotentialFilename

func (r *Request) GetPotentialFilename() string

func (*Request) GetRaw

func (r *Request) GetRaw() string

func (*Request) GetSourceIP

func (r *Request) GetSourceIP() string

func (*Request) GetSourceIPAddr

func (r *Request) GetSourceIPAddr() net.IP

func (*Request) GetURI

func (r *Request) GetURI() string

func (*Request) GetUrlNamedQueryVariable

func (r *Request) GetUrlNamedQueryVariable(key string) string

func (*Request) GetUrlNamedQueryVariableInt

func (r *Request) GetUrlNamedQueryVariableInt(key string) int

func (*Request) GetUrlNamedQueryVariableUInt

func (r *Request) GetUrlNamedQueryVariableUInt(key string) uint

func (*Request) GetUrlParameter

func (r *Request) GetUrlParameter(key string) string

func (*Request) GetUrlParameterBool

func (r *Request) GetUrlParameterBool(key string) bool

func (*Request) GetUrlParameterInt

func (r *Request) GetUrlParameterInt(key string) int

func (*Request) GetUrlParameterSet

func (r *Request) GetUrlParameterSet(key string) bool

func (*Request) GetUrlParameterUInt

func (r *Request) GetUrlParameterUInt(key string) uint

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) Error

func (r *Response) Error(err error, responseCode int)

func (*Response) GetCsrfToken

func (r *Response) GetCsrfToken() string

func (*Response) GetResponseCode

func (r *Response) GetResponseCode() int

func (*Response) GetWebSocket

func (r *Response) GetWebSocket() (*Socket, error)

func (*Response) InitSession

func (r *Response) InitSession()

Either gets existing session token or creates a new one.

func (*Response) IsValidCsrf

func (r *Response) IsValidCsrf() bool

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) Render

func (r *Response) Render() error

func (*Response) RenderTemplate

func (r *Response) RenderTemplate(templateName string) error

func (*Response) ResetOrCreateSession

func (r *Response) ResetOrCreateSession()

Sets a new session cookie.

func (*Response) ResponseCodeSet

func (r *Response) ResponseCodeSet() bool

func (*Response) SetCookie

func (r *Response) SetCookie(key string, value string)

func (*Response) SetFuncMap

func (r *Response) SetFuncMap(funcMap map[string]interface{})

func (*Response) SetHeader

func (r *Response) SetHeader(key string, value string)

func (*Response) SetRedirect

func (r *Response) SetRedirect(location string)

func (*Response) SetResponseCode

func (r *Response) SetResponseCode(code int) error
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

func (r *Response) SetSessionCookie(cookie string)

func (*Response) Write

func (r *Response) Write(s string)

func (*Response) WriteJson

func (r *Response) WriteJson(i interface{}, pretty bool)

type Route

type Route struct {
	Pattern     string
	Name        string // Optional
	Handler     func(*Response)
	CsrfProtect bool
	NeedsLogin  bool
}

func Routes

func Routes(routeSets ...[]Route) []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

func (s *Server) GetCookieName() string

func (*Server) Run

func (s *Server) Run() error

type Session

type Session struct {
	CookieId string
}

func (*Session) GetCsrfToken

func (s *Session) GetCsrfToken() string

type Socket

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

func (*Socket) Close

func (s *Socket) Close() error

func (*Socket) OnClose

func (s *Socket) OnClose(closeHandler func())

OnClose event does not get triggered unless messages are read.

func (*Socket) Ping

func (s *Socket) Ping() error

func (*Socket) ReadAllUntilClose

func (s *Socket) ReadAllUntilClose(closeHandler chan error)

ReadAllUntilClose throws away all incoming messages. If you want to read messages, don't use this function.

func (*Socket) ReadMessage

func (s *Socket) ReadMessage() ([]byte, error)

func (*Socket) WriteJSON

func (s *Socket) WriteJSON(v interface{}) error

type UrlParam

type UrlParam struct {
	Id   string
	Type UrlParamType
}

func (UrlParam) UrlPart

func (u UrlParam) UrlPart() string

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"
)

Jump to

Keyboard shortcuts

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