anansi

package module
v0.16.0 Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2024 License: MIT Imports: 17 Imported by: 0

README

anansi

Helper tools for go-chi

Features

  • API Errors
  • Logging Middleware
  • Session Store
  • Encryption Helpers
  • JSON request and body parsing
  • Extensions of faker

Install

go get -u github.com/noxecane/anansi/

Example


// embed basic env in your env struct
type AppEnv struct {
    anansi.BasicEnv
}

type Book struct {
    Name `json:"book_nme"`
}

func main() {
    // BasicEnv is only compatible with envconfig ATM
    env := loadEnv()
    logs := NewLogger(env.Name)

    router := chi.NewRouter()
    chix.DefaultMiddleware(env, log, router)
    router.Get("/api/v1/books", func(w http.ResponseWriter, r *http.Request) {
        // return all books from the DB, or at least some
    })

}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DumpLog = regexp.MustCompile("(?i)kube-probe|prometheus")
View Source
var ISO_FORMATS = [3]string{"2006-01-02T15:04:05.000Z", "2006-01-02", "2006-01-02T15:04:05"}

Functions

func Decrypt

func Decrypt(secret []byte, encrypted string) ([]byte, error)

Decrypt decrypts a string using secretbox

func Encrypt

func Encrypt(secret []byte, value []byte) (string, error)

Encrypt encrypts a string using secretbox

func LoadEnv

func LoadEnv(env interface{}) error

LoadEnv loads environment variables into env

func NewLogger

func NewLogger(service string) zerolog.Logger

func ParseISO

func ParseISO(date string) (time.Time, error)

func RandomBytes

func RandomBytes(n int) ([]byte, error)

RandomBytes returns securely generated random bytes. It will return an error if the system's secure random number generator fails to function correctly, in which case the caller should not continue.

func RandomString

func RandomString(s int) (string, error)

RandomString returns a securely generated random hex string. It will return an error if the system's secure random number generator fails to function correctly, in which case the caller should not continue. Keep in mind that s is the length of the returned string, not the number of bytes to be produced

func SimpleHeaders added in v0.16.0

func SimpleHeaders(headers http.Header) map[string]interface{}

SimpleHeaders is a transformer for headers. It:

- converts all header keys to lowercase

- all single value headers to strings

func Slugify

func Slugify(s string) string

func WithCancel

func WithCancel(parent context.Context) (context.Context, context.CancelFunc)

WithCancel replicates context.WithCancel but listens for Interrupt and SIGTERM signals

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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