service

package
v0.0.0-...-c02d020 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2016 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ServiceUnavailableErr is a global error that will get returned when we are experiencing
	// technical issues.
	ServiceUnavailableErr = &jsonErr{"sorry, this service is currently unavailable"}
	// UnauthErr is a global error returned when the user does not supply the proper
	// authorization headers.
	UnauthErr = &jsonErr{"please include a valid USER_ID header in the request"}
)

Functions

This section is empty.

Types

type MySQLSavedItemsRepo

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

MySQLSavedItemsRepo is an implementation of the repo interface built on top of MySQL.

func (*MySQLSavedItemsRepo) Delete

func (r *MySQLSavedItemsRepo) Delete(userID uint64, url string) error

Delete will attempt to remove an item from a user's saved items.

func (*MySQLSavedItemsRepo) Get

func (r *MySQLSavedItemsRepo) Get(userID uint64) ([]*SavedItem, error)

Get will attempt to query the underlying MySQL database for saved items for a single user.

func (*MySQLSavedItemsRepo) Put

func (r *MySQLSavedItemsRepo) Put(userID uint64, url string) error

Put will attempt to insert a new saved item for the user.

type SavedItem

type SavedItem struct {
	UserID    uint64    `json:"user_id"`
	URL       string    `json:"url"`
	Timestamp time.Time `json:"timestamp"`
}

SavedItem represents an article, blog, interactive, etc. that a user wants to save for reading later.

type SavedItemsRepo

type SavedItemsRepo interface {
	Get(uint64) ([]*SavedItem, error)
	Put(uint64, string) error
	Delete(uint64, string) error
}

SavedItemsRepo is an interface layer between our service and our database. Abstracting these methods out of a pure implementation helps with testing.

func NewSavedItemsRepo

func NewSavedItemsRepo(cfg *mysql.Config) (SavedItemsRepo, error)

NewSavedItemsRepo will attempt to connect to to MySQL and return a SavedItemsRepo implementation.

type SavedItemsService

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

SavedItemsService will keep a handle on the saved items repository and implement the gizmo/server.JSONService interface.

func NewSavedItemsService

func NewSavedItemsService(cfg *mysql.Config) (*SavedItemsService, error)

NewSavedItemsService will attempt to instantiate a new repository and service.

func (*SavedItemsService) Delete

func (s *SavedItemsService) Delete(r *http.Request) (int, interface{}, error)

Delete is JSONEndpoint for deleting a saved item from a user's list.

func (*SavedItemsService) Get

func (s *SavedItemsService) Get(r *http.Request) (int, interface{}, error)

Get is a JSONEndpoint to return a list of saved items for the given user ID.

func (*SavedItemsService) JSONEndpoints

func (s *SavedItemsService) JSONEndpoints() map[string]map[string]server.JSONEndpoint

JSONEndpoints is the most important method of the Service implementation. It provides a listing of all endpoints available in the service with their routes and HTTP methods. This method helps satisfy the server.JSONService interface.

func (*SavedItemsService) JSONMiddleware

JSONMiddleware provides a hook to add service-wide middleware for how JSONEndpoints should behave. In this example, we’re using the hook to check for a header to identify and authorize the user. This method helps satisfy the server.JSONService interface.

func (*SavedItemsService) Middleware

func (s *SavedItemsService) Middleware(h http.Handler) http.Handler

Middleware provides a hook to add service-wide http.Handler middleware to the service. In this example we are using it to add GZIP compression to our responses. This method helps satisfy the server.Service interface.

func (*SavedItemsService) Prefix

func (s *SavedItemsService) Prefix() string

Prefix is to implement gizmo/server.Service interface. The string will be prefixed to all endpoint routes.

func (*SavedItemsService) Put

func (s *SavedItemsService) Put(r *http.Request) (int, interface{}, error)

Put is a JSONEndpoint for adding a new saved item to a user's list.

Jump to

Keyboard shortcuts

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