service

package
v0.0.0-...-f080cac Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2017 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AddContactRequest

type AddContactRequest struct {
	Email string `json:"email"`
	Name  string `json:"name"`
}

type Client

type Client interface {
	AddContact(contact AddContactRequest) (*Contact, error)
	GetContactByEmail(email string) (*Contact, error)
}

Client defines the interface exposed by our API.

func NewClient

func NewClient(baseURL string) Client

NewClient creates a Client that accesses a service at the given base URL.

type Contact

type Contact struct {
	Id    int    `json:"id"`
	Email string `json:"email"`
	Name  string `json:"name"`
}

Contact describes a contact in our database.

type ContactResponse

type ContactResponse struct {
	Contact *Contact `json:"contact"`
}

type Database

type Database struct {
	DB *sql.DB
}

Database wraps our SQL database. Defining our own type allows us to define helper functions on the Database.

func (*Database) AddContact

func (db *Database) AddContact(c Contact) (int, error)

AddContact inserts a new contact into the database.

func (*Database) Close

func (db *Database) Close()

func (*Database) GetContactByEmail

func (db *Database) GetContactByEmail(email string) (*Contact, error)

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

type ErrorResponse struct {
	StatusCode int    `json:"status_code"`
	Message    string `json:"message"`
}

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

func NewServer(db *Database) *Server

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.

func (*Server) ServeHTTP

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

type ServerError

type ServerError interface {
	HttpStatusCode() int
	HttpStatusMessage() string
}

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

type Transaction struct {
	*sql.Tx
	// contains filtered or unexported fields
}

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)

Jump to

Keyboard shortcuts

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