O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mastering Go Web Services

Book Description

Program and deploy fast, scalable web services and create high-performance RESTful APIs using Go

In Detail

This book will take you through the most important aspects of designing, building, and deploying a web service utilizing idiomatic REST practices with a focus on speed, security, and flexibility. You will begin by building your first API in Go using the HTTP package. You will look at designing and building your application including popular design structures like Model-View-Controller. You will also understand methods for deploying code to staging and development. Finally, you will see how the security features in Go can be used for protection against SQL injection, and sensitive data compromise.

By the end of this book, you will have achieved a high level of proficiency in building and deploying web services and web APIs with Go.

What You Will Learn

  • Familiarize yourself with RESTful practices and apply them in Go
  • Acquaint yourself with the best practices for API design such as URL routing and HTTP response methods
  • Utilize useful middleware and frameworks to augment applications otherwise relying on pure Go
  • Understand the basic and complex authentication models for APIs
  • Deploy your code to staging and development environments
  • Look at ways to make your Go web services as speedy and thrifty as possible
  • Secure your web services
  • Ingest your own API and make it appealing to other developers

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Mastering Go Web Services
    1. Table of Contents
    2. Mastering Go Web Services
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the color images of this book
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Our First API in Go
      1. Understanding requirements and dependencies
        1. Installing Go
        2. Installing and using MySQL
        3. Redis
        4. Couchbase
        5. Nginx
        6. Apache JMeter
        7. Using predefined datasets
        8. Choosing an IDE
          1. Eclipse
          2. Sublime Text
          3. LiteIDE
          4. IntelliJ IDEA
          5. Some client-side tools
        9. Looking at our application
        10. Setting up our database
      2. Introducing the HTTP package
        1. Quick hitter – saying Hello, World via API
      3. Building our first route
        1. Gorilla
        2. Routes
      4. Setting data via HTTP
        1. Connecting to MySQL
      5. Serving data from the datastore to the client
        1. Setting headers to add detail for clients
      6. Summary
    9. 2. RESTful Services in Go
      1. Designing our application
      2. Looking at REST
        1. Making a representation in an API
        2. Self-description
        3. The importance of a URI
          1. HATEOAS
      3. Other API architectures
        1. RPC
      4. Choosing formats
        1. JSON
        2. XML
        3. YAML
        4. CSV
      5. Comparing the HTTP actions and methods
        1. The PATCH method versus the PUT method
      6. Bringing in CRUD
      7. Adding more endpoints
      8. Handling API versions
      9. Allowing pagination with the link header
      10. Summary
    10. 3. Routing and Bootstrapping
      1. Writing custom routers in Go
      2. Using more advanced routers in Gorilla
      3. Using Gorilla for JSON-RPC
      4. Using services for API access
      5. Using a simple interface for API access
      6. Returning valuable error information
      7. Handling binary data
      8. Summary
    11. 4. Designing APIs in Go
      1. Designing our social network API
      2. Handling our API versions
      3. Concurrent WebSockets
      4. Separating our API logic
      5. Expanding our error messages
      6. Updating our users via the web service
      7. Summary
    12. 5. Templates and Options in Go
      1. Sharing our OPTIONS
      2. Implementing alternative formats
      3. Rolling our own data representation format
      4. Introducing security and authentication
        1. Forcing HTTPS
        2. Adding TLS support
      5. Letting users register and authenticate
        1. A quick hit – generating a salt
      6. Examining OAuth in Go
      7. Making requests on behalf of users
      8. Summary
    13. 6. Accessing and Using Web Services in Go
      1. Connecting our users to other services
        1. Saving the state with a web service
      2. Using data from other OAuth services
      3. Connecting securely as a client in Go
      4. Summary
    14. 7. Working with Other Web Technologies
      1. Serving Go through a reverse proxy
      2. Using Go with Apache
      3. Go and NGINX as reverse proxies
      4. Enabling sessions for the API
        1. Sessions in a RESTful design
      5. Using NoSQL in Go
        1. Memcached
        2. MongoDB
      6. Enabling connections using a username and password
      7. Allowing our users to connect to each other
      8. Summary
    15. 8. Responsive Go for the Web
      1. Creating a frontend interface
      2. Logging in
      3. Using client-side frameworks with Go
      4. jQuery
        1. Consuming APIs with jQuery
        2. AngularJS
        3. Consuming APIs with Angular
      5. Setting up an API-consuming frontend
        1. Creating a client-side Angular application for a web service
      6. Viewing other users
      7. Rendering frameworks on the server side in Go
      8. Creating a status update
      9. Summary
    16. 9. Deployment
      1. Project structures
      2. Using process control to keep your API running
        1. Using supervisor
        2. Using Manners for more graceful servers
      3. Deploying with Docker
      4. Deploying in cloud environments
        1. Amazon Web Services
          1. Using Go to interface directly with AWS
      5. Handling binary data and CDNs
        1. Checking for the existence of a file upload
        2. Sending e-mails with net/smtp
      6. RabbitMQ with Go
      7. Summary
    17. 10. Maximizing Performance
      1. Using middleware to reduce cruft
        1. Caching requests
        2. Simple disk-based caching
          1. Enabling filtering
          2. Transforming a disk cache into middleware
        3. Caching in distributed memory
        4. Using NoSQL as a cache store
        5. Implementing a cache as middleware
        6. Using a frontend caching proxy in front of Go
      2. Rate limiting in Go
        1. Implementing rate limiting as middleware
      3. Implementing SPDY
        1. Detecting SPDY support
      4. Summary
    18. 11. Security
      1. Handling error logging for security
      2. Preventing brute-force attempts
        1. Knowing what to log
      3. Handling basic authentication in Go
      4. Handling input validation and injection mitigation
        1. Using best practices for SQL
      5. Validating output
        1. Protection against XSS
      6. Using server-side frameworks in Go
        1. Tiger Tonic
        2. Martini
        3. Goji
        4. Beego
      7. Summary
    19. Index