Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateHash ¶
GenerateHash calculate SHA512 hash of a string encode the resulting hash string as Base64
func ParseUrlId ¶
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 ¶
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 (*Hasher) Add ¶
Add Makes sense to store id and hash into map and startTime into a separate time.Duration slice for stats calculation later