Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AddContactRequest ¶
type Client ¶
type Client interface { AddContact(contact AddContactRequest) (*Contact, error) GetContactByEmail(email string) (*Contact, error) }
Client defines the interface exposed by our API.
type ContactResponse ¶
type ContactResponse struct {
Contact *Contact `json:"contact"`
}
type Database ¶
Database wraps our SQL database. Defining our own type allows us to define helper functions on the Database.
func (*Database) AddContact ¶
AddContact inserts a new contact into the database.
func (*Database) GetContactByEmail ¶
GetContactByEmail reads a Contact from the Database.
func (*Database) Read ¶
func (db *Database) Read(reader TransactionFunc) (err error)
Read begins a read-only transaction and passes it to the given function. The transaction will be rolled back after the function returns. Any panics will be handled, and returned as an error.
func (*Database) Write ¶
func (db *Database) Write(writer TransactionFunc) (err error)
Write begins a transaction and passes it to the given function. The transaction will be committed when the function returns. If the function panics, the transaction is rolled back, and the error provided to panic is returned.
type DefaultClient ¶
type DefaultClient struct { BaseURL string // contains filtered or unexported fields }
DefaultClient provides an implementation of the Client interface.
func (*DefaultClient) AddContact ¶
func (c *DefaultClient) AddContact(contact AddContactRequest) (*Contact, error)
func (*DefaultClient) GetContactByEmail ¶
func (c *DefaultClient) GetContactByEmail(email string) (*Contact, error)
type ErrorResponse ¶
ErrorResponse is returned by our service when an error occurs.
func (ErrorResponse) Error ¶
func (e ErrorResponse) Error() string
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server contains all that is needed to respond to incoming requests, like a database. Other services like a mail, redis, or S3 server could also be added.
func NewServer ¶
NewServer initializes the service with the given Database, and sets up appropriate routes.
func (*Server) AddContact ¶
func (s *Server) AddContact(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
AddContact handles HTTP requests to add a Contact.
func (*Server) GetContactByEmail ¶
func (s *Server) GetContactByEmail(w http.ResponseWriter, r *http.Request, ps httprouter.Params)
AddContact handles HTTP requests to GET a Contact by an email address.
type ServerError ¶
The ServerError type allows errors to provide an appropriate HTTP status code and message. The Server checks for this interface when recovering from a panic inside a handler.
type Transaction ¶
Transaction wraps a SQL transaction. Defining our own type allows functions to be defined on the Transaction.
func (*Transaction) AddContact ¶
func (tx *Transaction) AddContact(c Contact) int
AddContact inserts a new contact within the transaction.
func (*Transaction) GetContactByEmail ¶
func (tx *Transaction) GetContactByEmail(email string) *Contact
GetContactByEmail finds a contact given an email address. `nil` is returned if the Contact doesn't exist in the DB.
type TransactionFunc ¶
type TransactionFunc func(*Transaction)