middlewares

package
v0.2.12 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2024 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultFileMiddlewareConfig = FileMiddlewareConfig{
	UploadLimit:      209715200,
	UploadFormKey:    "files",
	AllowedMimeTypes: defaultAllowedMimeTypes,
	IdRouteKey:       "id",
	HashRouteKey:     "hash",
}

Functions

func IsLoggedIn

func IsLoggedIn(ctx fiber.Ctx) bool

func NewLoginCheckMiddleware

func NewLoginCheckMiddleware(skipRoutes ...[]string) fiber.Handler

NewLoginCheckMiddleware creates a middleware function that checks if a user is logged in. If the skipRoutes parameter is provided, the middleware will skip the check for those routes. The middleware checks if the user is logged in using the IsLoggedIn function. If the user is logged in, the middleware calls the next handler. If the user is not logged in, the middleware returns an Unauthorized response. The IsLoggedIn function checks if a session exists with a valid ID and keys. The isSkippedRoute function checks if the method and path match any of the patterns in skipRoutes. The parsePattern function parses a pattern string into a routePattern. The Unauthorized function creates an Unauthorized error response. The UseSession function retrieves the session from the context. The routePattern struct represents a route pattern with an HTTP method and path pattern. The initSessionStore function initializes the session store. This middleware is used to protect routes that require authentication, can be used as global middleware.

func NewLoginInfoMiddleware

func NewLoginInfoMiddleware() fiber.Handler

NewLoginInfoMiddleware creates a middleware function that checks if a user is logged in. The middleware checks if the user is logged in using the IsLoggedIn function. If the user is logged in, the middleware retrieves the user data from the session. It then unmarshals the user data into a map[string]interface{}. If unmarshaling fails, it returns a SystemBusy error with the unmarshal error. Otherwise, it returns a success response with the user info as the data. If the user data is nil, it returns a success response without any data. If the user is not logged in, the middleware returns an Unauthorized response. This middleware is used when the client needs to retrieve the user info.

func NewRateLimiter

func NewRateLimiter(qpm int) fiber.Handler

func NewRequestLoggingMiddleware

func NewRequestLoggingMiddleware(skipStaticRec ...bool) fiber.Handler

func UseSession

func UseSession(ctx fiber.Ctx) *session.Session

Types

type FileMiddlewareConfig

type FileMiddlewareConfig struct {
	// MaxUploadLimit is the maximum file size allowed for upload in bytes. Default is 200MB.
	UploadLimit int64

	// UploadFormKey is the key used to access the uploaded file in the form data. Default is "files".
	UploadFormKey string

	// AllowedMimeTypes is a list of allowed MIME types for file upload. Default all common file types.
	AllowedMimeTypes []string

	// FileIdRouteKey is the key used to access the file ID in the route parameters. Default is "id".
	IdRouteKey string

	// HashRouteKey is the hash used to access the file hash in the route parameters. Default is "hash".
	HashRouteKey string
}

type FileMiddlewares

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

func NewFileMiddlewares

func NewFileMiddlewares(config ...FileMiddlewareConfig) *FileMiddlewares

func (*FileMiddlewares) HandleDelete

func (m *FileMiddlewares) HandleDelete() fiber.Handler

HandleDelete handles the file delete request. Route recommendation: DELETE /file/delete/:id It gets the file ID from the route and validates it. Then, it finds the file info from the database using the ID. Next, it deletes the file from the storage by providing the file path. Finally, it deletes the file info from the database. The function returns an error if the file ID is invalid, file is not found, there's an error deleting the file from storage, or error deleting the file info from the database.

func (*FileMiddlewares) HandleMatch

func (m *FileMiddlewares) HandleMatch() fiber.Handler

HandleMatch handles the file match request. Route recommendation: GET /file/match/:hash It gets the file MD5 hash from the route parameter. Then, it finds the file info from the database using the hash. The function returns the file info if found, otherwise it returns an error.

func (*FileMiddlewares) HandleUpload

func (m *FileMiddlewares) HandleUpload() fiber.Handler

HandleUpload handles the file upload request. Route recommendation: POST /file/upload It parses the multipart form and retrieves all files from the "files" key. Then, it performs form validation on each file, checking the size and MIME type. The function processes each file, calculating its hash, determining the ideal filename, and saving the file to the storage and its info to the database. The function returns an error if the multipart form data is invalid or any other error occurs.

func (*FileMiddlewares) HandleView

func (m *FileMiddlewares) HandleView() fiber.Handler

HandleView handles the file view request. Route recommendation: GET /file/view/:id It checks if the request is for downloading the file and sets the appropriate download flag. Then, it gets the file ID from the route and finds the file info from the database. It retrieves the file content from the storage and sends it as the response. If the download flag is set, it sets the Content-Disposition header to suggest downloading. The function returns an error if the file is not found or any other error occurs.

type OAuthClaimDataProcessor

type OAuthClaimDataProcessor func(claimData map[string]any) (any, error)

OAuthClaimDataProcessor is a function type to process claim data from OIDC token, and return the data that will be stored in session as user info. The function can be used to check user permission, roles, or fetch user data from database. If the function returns an error, the login process will be failed.

type OIDCMiddleware

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

func NewOIDCMiddleware

func NewOIDCMiddleware(config ...OIDCMiddlewareConfig) *OIDCMiddleware

func (*OIDCMiddleware) HandleLogin

func (m *OIDCMiddleware) HandleLogin() fiber.Handler

func (*OIDCMiddleware) HandleLoginCallback

func (m *OIDCMiddleware) HandleLoginCallback(claimDataProcFunc ...OAuthClaimDataProcessor) fiber.Handler

type OIDCMiddlewareConfig

type OIDCMiddlewareConfig struct {
	CallbackRedirectUri string
}

type SPAMiddleware

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

func NewSPAServingMiddleware

func NewSPAServingMiddleware(rootPath string, indexFile string) *SPAMiddleware

NewSPAServingMiddleware create a new instance of the SPAMiddleware struct. It takes the rootPath and indexFile as parameters and initializes the struct with these values. IMPORTANT: It's really important to handle both static files and index file in a SPA application. Usage example: middleware := NewSPAServingMiddleware("./web/ui/dist", "index.html") app.Use("/", middleware.HandleStaticFiles()) app.Get("/*", middleware.HandleIndexFile())

func (*SPAMiddleware) HandleIndexFile

func (s *SPAMiddleware) HandleIndexFile() fiber.Handler

HandleIndexFile returns a middleware handler for serving the index file. It creates a new handler that sends the index file specified in the SPAMiddleware struct. Usage example: app.Get("/*", spm.HandleIndexFile())

func (*SPAMiddleware) HandleStaticFiles

func (s *SPAMiddleware) HandleStaticFiles() fiber.Handler

HandleStaticFiles returns a middleware handler for serving static files. It creates a new static file server using the root path specified in the SPAMiddleware struct. Usage example: app.Use("/", spm.HandleStaticFiles())

Jump to

Keyboard shortcuts

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