order

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: 26 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(id string, items map[string]int, order *Order) error

UnmarshalRedis parses a string and map into an Order. Order.Items will be sorted according to the ID.

Types

type Item

type Item struct {
	ID  string `json:"id"`
	Qty int    `json:"qty"`
}

Item holds stripped down information of a regular item, to be used in an Order.

func NewItem

func NewItem(item *item.Item) (*Item, error)

NewItem creates a new Item from an existing Item.

func (*Item) String

func (i *Item) String() string

type Order

type Order struct {
	ID    int64   `json:"id"`
	Items []*Item `json:"items"`
}

Order defines a placed order with identifier and items. An Order ID of -1 means that the item can be

func NewOrder

func NewOrder(id int64, items ...*Item) (*Order, error)

NewOrder creates a new order according to arguments. This will sort the passed items.

func (*Order) MarshalRedis

func (o *Order) MarshalRedis() (string, map[string]int)

MarshalRedis marshals an Order to hand over to go-redis.

func (*Order) Sort

func (o *Order) Sort() error

Sort will sort the order items according to ID

func (*Order) String

func (o *Order) String() string

type Response

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

Response defines an API response.

func NewResponse

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

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

func (Response) SendJSON

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) RedisGetNextOrderID

func (s *Server) RedisGetNextOrderID(ctx context.Context) (int64, error)

RedisGetNextOrderID retrieves increments the order ID counter in redis and returns it.

func (*Server) RedisGetOrder

func (s *Server) RedisGetOrder(ctx context.Context, id int64) (*Order, error)

RedisGetOrder retrieves a single order from Redis.

func (*Server) RedisScanOrders

func (s *Server) RedisScanOrders(ctx context.Context) ([]int64, error)

RedisScanOrders retrieves the IDs (keys) of all orders.

func (*Server) RedisSetOrder

func (s *Server) RedisSetOrder(ctx context.Context, o *Order) error

RedisSetOrder creates or updates a new order in Redis.

func (*Server) Respond

func (s *Server) Respond(ctx context.Context, status int, m string, c int, data []*Order, 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

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 SetItemServiceAddress

func SetItemServiceAddress(address string) ServerOptions

SetItemServiceAddress sets the address to reach the Item service.

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