backend

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2025 License: GPL-3.0 Imports: 8 Imported by: 0

README

About Backend

Backend is a set of interfaces used to abstract the backend implementation of services. The goal is to make service implementation backend agnostic, and very easy to convert between different implementations. This is aiming to include common methods for net/http implementations and fiber implementations.

Session

Session is an interface which describes and manages access to user sessions, which can either be stored client side in user cookies, or server side.

Documentation

Overview

Package backend provides a unified interface for handling HTTP requests and managing user sessions.

The `Handler` interface abstracts common HTTP operations, such as accessing form values, redirecting requests, and retrieving context. Concrete implementations are provided for Fiber and net/http frameworks.

The `Session` interface defines a standardized way to manage user sessions, including setting expiration times, storing and retrieving data, and invalidating sessions. Implementations for Fiber-based and Gorilla sessions are included.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FiberHandler

type FiberHandler struct {
	Ctx *fiber.Ctx
}

FiberHandler is a fiber based implementation of the Handler interface.

NOTE: FiberHandler uses FiberSessions and stores them in client side cookies which should be encrypted.

func (*FiberHandler) Context

func (h *FiberHandler) Context() context.Context

Context implements the Handler Context method by calling the *fiber.Ctx.Context method.

func (*FiberHandler) FormValue

func (h *FiberHandler) FormValue(key string) string

FormValue implements the Handler FormValue method by calling the FormValue method of the attached *fiber.Ctx.

func (*FiberHandler) LoadSession

func (h *FiberHandler) LoadSession(id string) (Session, error)

Load implements the SessionStore interface for the FiberSessionStore type.

func (*FiberHandler) Redirect

func (h *FiberHandler) Redirect(location string, status int) error

Redirect implements the Handler Redirect method by calling the Redirect method of the attached *fiber.Ctx.

func (*FiberHandler) SaveSession

func (h *FiberHandler) SaveSession(session Session) error

Save implements the SessionStore interface for the FiberSessionStore type.

type FiberSession

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

FiberSession implements the Session interface using a Fiber Cookie based storage method.

func NewFiberSession

func NewFiberSession(id, value string) (*FiberSession, error)

NewFiberSession creates a new empty FiberSession with the given id, and value. NOTE: The passed value should be the stored value of the cookie, which may be empty.

func (*FiberSession) Get

func (s *FiberSession) Get(key string, dst any) error

Get implements the Get method of the Session interface by getting the for the given key of a key value pair stored in the session.

func (*FiberSession) Invalidate

func (s *FiberSession) Invalidate() error

Invalidate implements the Invalidate method of the Session interface by setting the Max Age of the cookie to -1.

func (*FiberSession) Set

func (s *FiberSession) Set(key string, value any) error

Set implements the Set method of the Session interface by encoding a query escaped map in JSON format to the cookie value.

func (*FiberSession) SetMaxAge

func (s *FiberSession) SetMaxAge(age int) error

SetMaxAge implements the SetMaxAge method of the Session interface by setting the maximum age of the cookie in seconds.

type GorillaSession

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

GorillaSession implements the Session interface using Gorilla Sessions.

func NewGorillaSession

func NewGorillaSession(session *sessions.Session) *GorillaSession

func (*GorillaSession) Get

func (s *GorillaSession) Get(key string, dst any) error

Get implements the Get method of the Session interface by getting the for the given key of a key value pair stored in the session.

func (*GorillaSession) Invalidate

func (s *GorillaSession) Invalidate() error

Invalidate implements the Invalidate method of the Session interface by setting the Max Age of the cookie to -1.

func (*GorillaSession) Set

func (s *GorillaSession) Set(key string, value interface{}) error

Set implements the Set method of the Session interface by adding the key, value pair to the gorilla session's Values map.

func (*GorillaSession) SetMaxAge

func (s *GorillaSession) SetMaxAge(maxAge int) error

SetMaxAge implements the SetMaxAge method of the Session interface by setting the maximum age of the cookie.

type Handler

type Handler interface {
	// FormValue returns the value for the given field in a http form if it exists.
	FormValue(string) string

	// Redirect creates a redirect to the specified location, with the given status code.
	Redirect(string, int) error

	// Context returns a context value which implements the context.Context interface.
	Context() context.Context

	// LoadSession returns a Session based on the given id.
	LoadSession(string) (Session, error)

	// Save saves the passed Session to the session store.
	SaveSession(Session) error
}

Handler is an interface used to abstract the functionality of different HTTP frameworks.

func NewFiberHandler

func NewFiberHandler(c *fiber.Ctx) Handler

NewFiberHandler creates a new FiberHandler.

func NewNetHandler

func NewNetHandler(w http.ResponseWriter, r *http.Request, store *sessions.CookieStore) Handler

NewNetHandler creates a new NetHandler.

type NetHandler

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

NetHandler is a net/http based implementation of the Handler interface.

NOTE: NetHandler uses GorillaSessions.

func (*NetHandler) Context

func (h *NetHandler) Context() context.Context

Context implements the Handler Context method by calling the *http.Request.Context method.

func (*NetHandler) FormValue

func (h *NetHandler) FormValue(key string) string

FormValue implements the Handler FormValue method by calling the FormValue method of the attached *http.Request

func (*NetHandler) LoadSession

func (h *NetHandler) LoadSession(id string) (Session, error)

Get implements the SessionStore interface for the GorillaSessionStore type.

func (*NetHandler) Redirect

func (h *NetHandler) Redirect(location string, status int) error

Redirect implements the Handler Redirect method by calling the Redirect method of the attached *http.Request.

func (*NetHandler) SaveSession

func (h *NetHandler) SaveSession(session Session) error

Save implements the Save method of the SessionStore interface using GorillaSessions.

type Session

type Session interface {
	// SetMaxAge sets the Max Age of the session in seconds, after which the session is
	// no longer valid.
	SetMaxAge(age int) error

	// Set sets a key value store in the session.
	Set(key string, value any) error

	// Get retrieves the value for a given key in the session and stores it in the destination.
	Get(key string, dst any) error

	// Invalidate immediately invalidates the session and marks it as no
	// longer valid.
	Invalidate() error
}

Session defines an interface for a session to keep track of user authenticated sessions.

Jump to

Keyboard shortcuts

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