Documentation ¶
Index ¶
- func NewRedisClient(addr string) (*redis.Client, error)
- func UnmarshalRedis(key string, data map[string]string, i *Item) error
- type Item
- type Response
- type Server
- func (s *Server) InitPromReg()
- func (s *Server) RedisDelItem(ctx context.Context, id string) error
- func (s *Server) RedisDelItems(ctx context.Context, items []*Item) error
- func (s *Server) RedisGetItem(ctx context.Context, k string) (*Item, error)
- func (s *Server) RedisScanKeys(ctx context.Context) ([]string, error)
- func (s *Server) RedisSetItem(ctx context.Context, i *Item) error
- func (s *Server) Respond(ctx context.Context, status int, m string, c int, data []*Item, ...)
- func (s *Server) Run() error
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) Stop()
- type ServerOptions
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewRedisClient ¶
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
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
DataToItems takes a JSON-encoded byte array and marshals it into a list of item.Items
func (*Item) MarshalRedis ¶
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.
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
NewResponse returns a Response with a passed message string and slice of Data. TODO: make d variadic
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 ¶
RedisDelItem deletes a single Item by ID.
func (*Server) RedisDelItems ¶
RedisDelItems deletes one or more Items from Redis.
func (*Server) RedisGetItem ¶
RedisGetItem retrieves an Item from Redis.
func (*Server) RedisScanKeys ¶
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 ¶
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.
type ServerOptions ¶
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.