request

package
v3.0.8 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2023 License: GPL-2.0 Imports: 14 Imported by: 2

Documentation

Index

Constants

View Source
const MESSAGE_COOKIE_NAME = "messages"
View Source
const NEXT_COOKIE_NAME = "next"

Variables

View Source
var DEFAULT_DATA_FUNC func(r *Request)
View Source
var TEMPLATE_MANAGER *templates.Manager

Functions

func AddHeader

func AddHeader(w http.ResponseWriter, name, value string)

Add a header onto the request. This will append the value to the current value. If the value already exists, it will not be added.

func GetHost

func GetHost[T RequestConstraint](r T) string

Types

type CSRFToken

type CSRFToken struct {
	Token string
}

func NewCSRFToken

func NewCSRFToken(token string) *CSRFToken

func (*CSRFToken) Input

func (ct *CSRFToken) Input() template.HTML

func (*CSRFToken) String

func (ct *CSRFToken) String() string

type JSONResponse

type JSONResponse struct {
	Next   string         `json:"next,omitempty"`
	Detail string         `json:"detail,omitempty"`
	Status ResponseStatus `json:"status"`
	Data   interface{}    `json:"data"`
}

Default json response which gets returned when using (j).Encode().

type Logger

type Logger interface {
	// Write a critical error message
	// This message should be handled differently
	// than the other ways of reporting.
	Critical(err error)
	// Write an error message, loglevel error
	Error(args ...any)
	Errorf(format string, args ...any)
	// Write a warning message, loglevel warning
	Warning(args ...any)
	Warningf(format string, args ...any)
	// Write an info message, loglevel info
	Info(args ...any)
	Infof(format string, args ...any)
	// Write a debug message, loglevel debug
	Debug(args ...any)
	Debugf(format string, args ...any)
	// Write a test message, loglevel test
	Test(args ...any)
	Testf(format string, args ...any)
}

Default logger interface, can be used to set a logger on the request. This logger can be set in for example, the middleware, and then be used in the views by the request.

type Message

type Message struct {
	Type string
	Text string
}

type Messages

type Messages []Message

func (*Messages) Decode

func (m *Messages) Decode(data string) error

func (Messages) Encode

func (m Messages) Encode() string

type Request

type Request struct {
	// Underlying http response writer.
	Response http.ResponseWriter

	// Underlying http request.
	Request *http.Request

	// Default data to be passed to any possible templates.
	Data *TemplateData

	// URL Parameters set inside of the router.
	URLParams URLParams

	// The request JSON object, which handles returning json responses.
	// This is mostly for semantic reasons.
	JSON *_json

	// Interfaces which can be set using the right middlewares.
	// These interfaces are not set by default, but can be set by middleware.
	User    User
	Session Session
	Logger  Logger
	URL     func(method, name string) routevars.URLFormatter
	// contains filtered or unexported fields
}

Default request to be passed around the router.

func NewRequest

func NewRequest(writer http.ResponseWriter, request *http.Request, params URLParams) *Request

Initialize a new request.

func (*Request) AddHeader

func (r *Request) AddHeader(name, value string)

Add a header.

func (*Request) DeleteCookie

func (r *Request) DeleteCookie(name string)

Delete cookies.

func (*Request) Error

func (r *Request) Error(code int, err string)

Raise an error.

func (*Request) Form

func (r *Request) Form() url.Values

Parse the form, and return the form values.

func (*Request) FormFileBuffer

func (r *Request) FormFileBuffer(name string) (*bytes.Buffer, error)

Get a form file as a buffer.

func (*Request) GetCookie

func (r *Request) GetCookie(name string) (*http.Cookie, error)

Get cookies.

func (*Request) GetData

func (r *Request) GetData(key string) interface{}

Get a data value.

func (*Request) GetHeader

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

Get a header.

func (*Request) IP

func (r *Request) IP() string

IP address of the request.

func (*Request) Method

func (r *Request) Method() string

Get the request method.

func (*Request) Next

func (r *Request) Next() string

Get the Next url. This is the url that was set in the session/cookies. This is used to redirect back to the same page.

func (*Request) ReSetNext added in v3.0.6

func (r *Request) ReSetNext()

Reset the next data.

func (*Request) Redirect

func (r *Request) Redirect(redirectURL string, statuscode int, next ...string)

Redirect to a URL. If the session is defined, the messages will be set in the session. If the `next` argument is given, it will be added to session, unless the session is not defined, the `next` parameter will be added to cookies. This means they will be carried across when rendering with request.Render(). This will be set again after the redirect, when rendering in the default Data. Optionally you could obtain this by calling request.Next().

func (*Request) Render

func (r *Request) Render(templateName string) error

Template configuration must be set before calling this function! See the templates package for more information.

func (*Request) RenderString

func (r *Request) RenderString(templateString string) error

Render a string as a template

func (*Request) RenderTemplate

func (r *Request) RenderTemplate(t *template.Template, name string) error

Render a template with the given name

func (*Request) SetCookies

func (r *Request) SetCookies(cookies ...*http.Cookie)

Set cookies.

func (*Request) SetData

func (r *Request) SetData(key string, value interface{})

Set a data value.

func (*Request) SetHeader

func (r *Request) SetHeader(name, value string)

Set a header.

func (*Request) Write

func (r *Request) Write(b []byte) (int, error)

Write to the response.

func (*Request) WriteString

func (r *Request) WriteString(s string) (int, error)

Write a string to the response.

type RequestConstraint

type RequestConstraint interface {
	*Request | *http.Request
}

This constraint is used to retrieve the request host.

type ResponseStatus

type ResponseStatus string
const (
	ResponseStatusOK       ResponseStatus = "ok"
	ResponseStatusError    ResponseStatus = "error"
	ResponseStatusRedirect ResponseStatus = "redirect"
)

type Session

type Session interface {
	Set(key string, value interface{})
	Get(key string) interface{}
	Exists(key string) bool
	Delete(key string)
	Destroy() error
	RenewToken() error
}

This interface will be set on the request, but is only useful if any middleware is using it. If no middleware has set it, it will remain unused.

type TemplateData

type TemplateData struct {
	Data      map[string]any
	Messages  Messages
	CSRFToken *CSRFToken
	User      interface{}
	Next      string
	// contains filtered or unexported fields
}

func NewTemplateData

func NewTemplateData() *TemplateData

func (*TemplateData) AddMessage

func (td *TemplateData) AddMessage(messageType, message string)

func (*TemplateData) Delete

func (td *TemplateData) Delete(key string)

func (*TemplateData) Get

func (td *TemplateData) Get(key string) interface{}

func (*TemplateData) Has

func (td *TemplateData) Has(key string) bool

func (*TemplateData) Set

func (td *TemplateData) Set(key string, value interface{})

func (*TemplateData) URL

func (td *TemplateData) URL(path string, args ...interface{}) string

type URLParams

type URLParams map[string]string

func (URLParams) Get

func (u URLParams) Get(key string, def ...string) string

func (URLParams) GetInt

func (u URLParams) GetInt(key string, def ...int) int

func (URLParams) Has

func (u URLParams) Has(key string) bool

type User

type User interface {
	// Check if the user is authenticated
	IsAuthenticated() bool

	// Check if the user is an administator
	IsAdmin() bool
}

Default request user interface. This interface is used to check if a user is authenticated. This interface is used by the LoginRequiredMiddleware and LogoutRequiredMiddleware. If you want to use these middlewares, you should implement this interface. And set the GetRequestUserFunc function to return a user.

Jump to

Keyboard shortcuts

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