item

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2019 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRedisClient

func NewRedisClient(addr string) (*redis.Client, error)

NewRedisClient creates a new go-redis/redis client according to passed options. Address needs to be a valid redis URL, e.g. redis://127.0.0.1:6379/0 or redis://:qwerty@localhost:6379/1

func UnmarshalRedis

func UnmarshalRedis(key string, data map[string]string, i *Item) error

UnmarshalRedis parses a passed string and map into an Item.

Types

type Item

type Item struct {
	Name string `json:"name"`
	ID   string `json:"id"`
	Desc string `json:"desc"`
	Qty  int    `json:"qty"`
}

Item defines a shop item with attributes. ID should be a HashID of the name. // See https://hashids.org for more info.

func DataToItems added in v0.2.0

func DataToItems(data []byte) ([]*Item, error)

DataToItems takes a JSON-encoded byte array and marshals it into a list of item.Items

func NewItem

func NewItem(name, desc string, qty int) (*Item, error)

NewItem creates a new item where the ID becomes the HashID of the lowercase name.

func (*Item) MarshalRedis

func (i *Item) MarshalRedis() (string, map[string]string)

MarshalRedis marshalls and Item to hand over to go-redis. Item.ID will be the key (as string), where the other fields will be a map[string]string.

func (*Item) SetID

func (i *Item) SetID(ctx context.Context) error

SetID creates a HashID from an already set Name field.

func (*Item) String added in v0.2.0

func (i *Item) String() string

type Response added in v0.2.0

type Response struct {
	Status  int     `json:"status"`
	Message string  `json:"message"`
	Count   int     `json:"count"`
	Data    []*Item `json:"data"`
}

Response defines an API response.

func NewResponse added in v0.2.0

func NewResponse(s int, m string, c int, d []*Item) (Response, error)

NewResponse returns a Response with a passed message string and slice of Data. TODO: make d variadic

func (Response) SendJSON added in v0.2.0

func (r Response) SendJSON(w http.ResponseWriter) error

SendJSON encodes a Response as JSON and sends it on a passed http.ResponseWriter.

type Server

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

Server is a wrapper for a HTTP server, with dependencies attached.

func NewServer

func NewServer(options ...ServerOptions) (*Server, error)

NewServer creates a new Server according to options.

func (*Server) InitPromReg added in v0.2.1

func (s *Server) InitPromReg()

InitPromReg initializes a custom Prometheus registry with Collectors.

func (*Server) RedisDelItem

func (s *Server) RedisDelItem(ctx context.Context, id string) error

RedisDelItem deletes a single Item by ID.

func (*Server) RedisDelItems

func (s *Server) RedisDelItems(ctx context.Context, items []*Item) error

RedisDelItems deletes one or more Items from Redis.

func (*Server) RedisGetItem

func (s *Server) RedisGetItem(ctx context.Context, k string) (*Item, error)

RedisGetItem retrieves an Item from Redis.

func (*Server) RedisScanKeys

func (s *Server) RedisScanKeys(ctx context.Context) ([]string, error)

RedisScanKeys retrieves all keys from a redis instance. This uses the SCAN command so it's save to use on large database & in production.

func (*Server) RedisSetItem

func (s *Server) RedisSetItem(ctx context.Context, i *Item) error

RedisSetItem sets an Item as a hash in Redis.

func (*Server) Respond

func (s *Server) Respond(ctx context.Context, status int, m string, c int, data []*Item, w http.ResponseWriter)

Respond sends a JSON-encoded response.

func (*Server) Run

func (s *Server) Run() error

Run starts a Server and shuts it down properly on a SIGINT and SIGTERM.

func (*Server) ServeHTTP added in v0.2.0

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP dispatches the request to the matching mux handler. This function is mainly intended for testing purposes.

func (*Server) Stop

func (s *Server) Stop()

Stop will stop the server

type ServerOptions

type ServerOptions func(*Server) error

ServerOptions sets options when creating a new server.

func SetLogLevel

func SetLogLevel(level string) ServerOptions

SetLogLevel sets the log level to either debug, warn, error or info. Info is default.

func SetRedisAddress

func SetRedisAddress(address string) ServerOptions

SetRedisAddress sets a custom address for the redis connection.

func SetServerAddress

func SetServerAddress(address string) ServerOptions

SetServerAddress sets the server address.

func SetServerEndpoint

func SetServerEndpoint(address string) ServerOptions

SetServerEndpoint sets the server endpoint address for other services to call it.

Jump to

Keyboard shortcuts

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