controller

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HandleRequest

func HandleRequest[T any, K IObject](ctx *fiber.Ctx, useInfo func(T) (error,
	*ObjWrapper[K]), onSuccess func(*ObjWrapper[K])) error

HandleRequest is a generic function designed to handle HTTP requests within a fiber context. This function is flexible and allows you to provide specific logic via higher-order functions and handle different types of objects and errors.

This function takes four parameters:

ctx: A pointer to a fiber context which holds all request information and methods to control the flow of a HTTP request.

useInfo: A function that takes an object of given type T and returns an error and a pointer to an ObjWrapper of type K. This function encapsulates how we want to use the provided information, specifying both the potential error that could arise and the wrapped object we may get.

errToReason: A mapping from error messages to string reasons. This map provides a clean way to handle possible errors and respond with appropriate reasons.

onSuccess: a function that takes a pointer to an ObjWrapper of type K. This function executes if there are no errors from useInfo function. The ObjWrapper that is passed to this function is the one that is returned from the useInfo function.

This function will return an error if something goes wrong during the processing of the HTTP request, otherwise, it will return nil.

func HandleView

func HandleView(ctx *fiber.Ctx, controller ViewController,
	writer func(buf io.Writer, userId string) error) error

HandleView is a function handling view logic for a certain page. It first retrieves the user session from the store, using the given context. If session retrieval is successful, it gets the API instance and extracts the user ID from the session. It then checks if the user has access to the requested path and if not, it returns a 403 Forbidden error. If a user has access, it creates a new bytes.Buffer and calls the provided writer function. The writer function is supposed to write the required data into the provided buffer. If writing is successful, it sets the context's content type to 'text/html; charset=UTF-8' and sends the buffer's bytes as the response. If writing fails, it returns the error.

Parameters:

- ctx: It's a fiber.Ctx pointer, the context of the request.

- controller: It's an instance of ViewController having the store to get the session.

- writer: It's a custom function to write into the provided io.Writer. It should handle the writing logic based on the provided user ID.

Returns:

- error: It returns an error in case something goes wrong. It would be 'nil' for successful execution.

Types

type Controller

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

func NewController

func NewController(router fiber.Router, store *Store) Controller

Jump to

Keyboard shortcuts

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