handlers

package
v0.3.8 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2023 License: BSD-3-Clause Imports: 12 Imported by: 0

Documentation

Overview

Package handlers provides the HTTP handling logic for the URL shortener service. It includes handlers for creating, retrieving, updating, and deleting shortened URLs, with storage backed by Google Cloud Datastore. The package also offers middleware for access control, ensuring that certain operations are restricted to internal use.

The handlers are designed to work with the Gin web framework and are registered to the Gin router, establishing the service's RESTful API. Each handler function is responsible for processing specific types of HTTP requests, validating input, interacting with the datastore, and formatting the HTTP response.

Structured logging is employed throughout the package via the `logmonitor` package, ensuring that operational events are recorded in a consistent and searchable format. This facilitates debugging and monitoring of the service.

Usage example:

func main() {
    router := gin.Default()
    dsClient := datastore.NewClient() // Assuming a function to create a new datastore client
    handlers.SetLogger(logmonitor.Logger) // Set the logger for the handlers package
    handlers.RegisterHandlersGin(router, dsClient)
    router.Run(":8080")
}

The package defines various types to represent request payloads and middleware functions. The `InternalOnly` middleware function enforces access control by requiring a secret value in the request header, which is compared against an environment variable.

Handler functions such as `getURLHandlerGin` and `postURLHandlerGin` serve as endpoints for fetching and storing URL mappings, respectively. The `editURLHandlerGin` and `deleteURLHandlerGin` functions provide the logic for updating and deleting mappings.

The `RegisterHandlersGin` function is the entry point for setting up the routes and associating them with their handlers. It ensures that all routes are prefixed with a base path that can be configured via an environment variable.

Copyright (c) 2023 H0llyW00dzZ

Index

Constants

This section is empty.

Variables

View Source
var Logger *zap.Logger

Logger is a package-level variable to access the zap logger throughout the handlers package. It is intended to be used by other functions within the package for logging purposes.

Functions

func InternalOnly

func InternalOnly() gin.HandlerFunc

InternalOnly creates a middleware that restricts access to a route to internal services only. It checks for a specific header containing a secret value that should match an environment variable to allow the request to proceed. If the secret does not match or is not provided, the request is aborted with a 403 Forbidden status.

func LogBadRequestError added in v0.3.8

func LogBadRequestError(context string, err error)

LogBadRequestError logs a message indicating a bad request error.

func LogDeletionError added in v0.3.8

func LogDeletionError(id string, err error)

LogDeletionError logs a message indicating that there was an error during deletion.

func LogError added in v0.3.8

func LogError(context string, fields ...zap.Field)

LogError logs an error message with given context fields.

func LogInfo added in v0.3.8

func LogInfo(context string, fields ...zap.Field)

LogInfo logs an informational message with given context fields.

func LogInternalError added in v0.3.8

func LogInternalError(context string, id string, err error)

LogInternalError logs an internal server error.

func LogInvalidURLFormat added in v0.3.8

func LogInvalidURLFormat(url string)

LogInvalidURLFormat logs a message indicating that the URL format is invalid.

func LogMismatchError added in v0.3.8

func LogMismatchError(id string)

LogMismatchError logs a message indicating that there is a mismatch error.

func LogURLDeletionSuccess added in v0.3.8

func LogURLDeletionSuccess(id string)

LogURLDeletionSuccess logs a message indicating that a URL has been successfully deleted.

func LogURLNotFound added in v0.3.8

func LogURLNotFound(id string, err error)

LogURLNotFound logs a "URL not found" error.

func LogURLRetrievalSuccess added in v0.3.8

func LogURLRetrievalSuccess(id string)

LogURLRetrievalSuccess logs a successful URL retrieval.

func LogURLShortened added in v0.3.8

func LogURLShortened(id string)

LogURLShortened logs a message indicating that a URL has been successfully shortened.

func RegisterHandlersGin

func RegisterHandlersGin(router *gin.Engine, datastoreClient *datastore.Client)

RegisterHandlersGin registers the HTTP handlers for the URL shortener service using the Gin web framework. It sets up the routes for retrieving, creating, and updating shortened URLs. The InternalOnly middleware is applied to the POST and PUT routes to protect them from public access.

func SetLogger added in v0.1.7

func SetLogger(logger *zap.Logger)

SetLogger sets the logger instance for the package.

Types

type CreateURLPayload added in v0.1.9

type CreateURLPayload struct {
	URL string `json:"url" binding:"required,url"`
}

CreateURLPayload defines the structure for the JSON payload when creating a new URL. It contains a single field, URL, which is the original URL to be shortened.

type DeleteURLPayload added in v0.1.10

type DeleteURLPayload struct {
	ID  string `json:"id" binding:"required"`
	URL string `json:"url" binding:"required,url"`
}

DeleteURLPayload defines the structure for the JSON payload when deleting a URL.

type UpdateURLPayload added in v0.1.9

type UpdateURLPayload struct {
	ID     string `json:"id" binding:"required"`
	OldURL string `json:"old_url" binding:"required,url"`
	NewURL string `json:"new_url" binding:"required,url"`
}

UpdateURLPayload defines the structure for the JSON payload when updating an existing URL.

Fixed a bug potential leading to Exploit CWE-284 / IDOR in the json payloads, Now It's safe A long With ID.

Jump to

Keyboard shortcuts

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