httputils

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2023 License: MIT Imports: 4 Imported by: 0

README

HTTPUTILS

This package holds a list of simple yet useful functions for Go standard HTTP package.

Caching package

This package holds a simple caching system that can be used to cache any kind of data. It is thread safe and can be used in a concurrent environment. The prymary use case of teh package is to solve react useEffect issues where the useEffect hook is called multiple times therefore making multiple requests to the server. This package can be used to cache the response of the request and return the cached response instead of making a new request avoid same request multiple times.

It caches the method, the url, the body and some headers (if specified) and returns the cached response if the request is the same.

It currently supports those types of caching:

  • In memory caching
  • File caching (not implemented yet)

Usage:

package main

import (
    "fmt"
    "net/http"
    "time"

    "github.com/Polo4444/httputils/caching"
    "github.com/gorilla/mux"

    // create http router

    router := mux.NewRouter()
    router.use(caching.NewCaching([]string{"token"}).Middleware)
)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReturnBytes

func ReturnBytes(w http.ResponseWriter, v []byte)

func ReturnBytesWithCode

func ReturnBytesWithCode(w http.ResponseWriter, code int, v []byte)

func ReturnError

func ReturnError(w http.ResponseWriter, code int, message string, err ...error) error

func ReturnJSON

func ReturnJSON(w http.ResponseWriter, v interface{}) error

func ReturnJSONWithCode

func ReturnJSONWithCode(w http.ResponseWriter, code int, v interface{}) error

func ReturnOK

func ReturnOK(w http.ResponseWriter) error

func ReturnReader

func ReturnReader(w http.ResponseWriter, v io.Reader)

func ReturnReaderWithCode

func ReturnReaderWithCode(w http.ResponseWriter, code int, v io.Reader)

func ReturnString

func ReturnString(w http.ResponseWriter, v string)

func ReturnStringWithCode

func ReturnStringWithCode(w http.ResponseWriter, code int, v string)

Types

type RespResult

type RespResult struct {
	Result  string   `json:"result"`
	Details []string `json:"details"`
}

type ResponseWriter

type ResponseWriter struct {
	http.ResponseWriter
	Body       *bytes.Buffer
	StatusCode int
}

func NewResponseWriter

func NewResponseWriter(w http.ResponseWriter) *ResponseWriter

func (*ResponseWriter) Write

func (crw *ResponseWriter) Write(data []byte) (int, error)

func (*ResponseWriter) WriteHeader

func (crw *ResponseWriter) WriteHeader(statusCode int)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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