hasher

package
v0.0.0-...-393b749 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateHash

func GenerateHash(s string) string

GenerateHash calculate SHA512 hash of a string encode the resulting hash string as Base64

func ParseUrlId

func ParseUrlId(path string) (int, error)

ParseURLId Use regex to parse the id from "/hash/:id" properly and convert it to an int

Types

type HashServer

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

HashServer has internal http.Server type so it can ListenAndServe by itself Utilizes wait group for making go routines finish processing on shutdown Hasher for tracking generated hashes and stats Has a shutdown channel for, you know, shutting down

func NewHashServer

func NewHashServer(address string, port string) (*HashServer, <-chan struct{})

NewHashServer HashServer's state is halfway built after initializing with wait group, newHasher and shutdown channel. Second half is building a newServeMux and setting handlers from newHashServer's current state of handler functions that wrap http.HandlerFunc. Store newMuxServer in a new http.Server as its handler along with constructed addressPort string (from user input) which allows us to call ListenAndServe on it. Return our new HashServer with our receive only shutdown channel to the caller (usually main)

func (*HashServer) GetHashHandler

func (hs *HashServer) GetHashHandler() http.HandlerFunc

GetHashHandler GET /hash/:id enpoint wrapping the get call on Hasher with the id parsed from the url path and convert to int for hash lookup

func (*HashServer) GetShutdownHandler

func (hs *HashServer) GetShutdownHandler() http.HandlerFunc

GetShutdownHandler GET /shutdown endpoint. first, close all idle connections, then wait indefinitely for connections to return to idle and then shut down the server. second, wait for all go routines to finish processing lastly, close the shutdown channel so a thread running HashServer (usually a main thread) will terminate

func (*HashServer) GetStatsHandler

func (hs *HashServer) GetStatsHandler() http.HandlerFunc

GetStatsHandler GET /stats endpoint wrapping hasher call to generate current stats on the fly and returning json

func (*HashServer) ListenAndServe

func (hs *HashServer) ListenAndServe() error

ListenAndServe delegate ListenAndServe down to our own http.Server

func (*HashServer) PostHashHandler

func (hs *HashServer) PostHashHandler() http.HandlerFunc

PostHashHandler POST /hash endpoint requiring a password paramter parse password from request, get the next id, immediately return it, generate hash and add id, hash, startTime to the Hasher after 5 seconds in the background

type HashStats

type HashStats struct {
	Total   int64 `json:"total"`
	Average int64 `json:"average"`
}

HashStats is used to store caclulated total and average stats according to the state of Hasher's internal stats slice only used for json marshalling

type Hasher

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

Hasher is what keeps track of id mappings to hash strings uses RWMutex for concurrency safety and uses a counter for id generation stats is a slice of time.Durations so that you can do conversion on read. Use HashStats to set calculated stats and json marshalling

func NewHasher

func NewHasher() *Hasher

NewHasher life begins explicitly on allocation

func (*Hasher) Add

func (h *Hasher) Add(id int, password string, startTime time.Time)

Add Makes sense to store id and hash into map and startTime into a separate time.Duration slice for stats calculation later

func (*Hasher) GenerateStats

func (h *Hasher) GenerateStats() HashStats

GenerateStats on the fly

func (*Hasher) Get

func (h *Hasher) Get(id int) (string, error)

Get wrapper for pulling hashes out the map went with returning error instead of return ok

func (*Hasher) NextId

func (h *Hasher) NextId() int

NextId Increment counter for next id

Jump to

Keyboard shortcuts

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