Categorygithub.com/rdbell/echo-cookie
modulepackage
1.0.0
Repository: https://github.com/rdbell/echo-cookie.git
Documentation: pkg.go.dev

# README

Echo Cookie

A simple package designed for managing browser cookies within the Echo web framework. It offers functionalities to set, get, and clear cookies, providing a streamlined approach to handling user sessions and preferences in Go web applications.

Features

  • Set Cookies: Easily set cookies with customizable names, values, and expiration times.
  • Get Cookies: Retrieve the value of a specified cookie from a browser request.
  • Clear Cookies: Invalidate a cookie by setting its expiration to a past date.

Usage Example

package main

import (
	"log"
	"net/http"

	"github.com/labstack/echo/v4"
	cookie "github.com/rdbell/echo-cookie"
)

func setHandler(c echo.Context) error {
	// Set a cookie
	cookie.Set(c, "exampleCookie", "Hello, Echo!", nil)

	return c.String(http.StatusOK, "Cookie set")
}

func getHandler(c echo.Context) error {
	// Get the cookie
	value := cookie.Get(c, "exampleCookie")
	if value == "" {
		return c.String(http.StatusNotFound, "Cookie not found")
	}

	return c.String(http.StatusOK, "Cookie value: "+value)
}

func clearHandler(c echo.Context) error {
	// Clear the cookie
	cookie.Clear(c, "exampleCookie")

	return c.String(http.StatusOK, "Cookie cleared")
}

func main() {
	e := echo.New()

	// Set routes
	e.GET("/set", setHandler)
	e.GET("/get", getHandler)
	e.GET("/clear", clearHandler)

	// Start server
	go func() {
		err := e.Start(":8080")
		if err != nil {
			log.Fatalf("Failed to start server: %v", err)
		}
	}()

	// Wait for server to start
	for {
		_, err := http.Get("http://localhost:8080/")
		if err == nil {
			break
		}
	}
}

Demo

Navigate to the example folder within this repository and run go run main.go in your terminal. This command will launch an Echo server demonstrating cookie setting, getting, and clearing functionalities.

Contribution

Contributions are welcome! If you'd like to improve PrettyLogger or suggest new features, feel free to fork the repository, make your changes, and submit a pull request.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

# Packages

No description provided by the author

# Functions

Clear unsets a browser cookie.
Get gets a browser cookie.
Set sets a browser cookie.

# Constants

DefaultCookieExpiration determines how long a cookie is valid by default.