REST is an architectural style that tackles the challenges of building scalable web services. APIs provide the fabric through which systems interact, and REST has become synonymous with APIs. The depth, breadth, and ease of use of Go, make it a breeze for developers to work with it to build robust Web APIs. This course will teach you to build a RESTful web server. You will learn to add routing and handle requests, thus making your APIs more RESTful. Later, by testing and profiling your code you will ensure it runs correctly and behaves well at scale. Moving on with security, you will learn to log requests and outcomes of API calls and speed up performance using the Echo framework. By the end of this course, you will have the knowledge you need to start building your own enterprise-grade RESTful web services that are production-ready, secure, scalable, and reliable.
REST is an architectural style that tackles the challenges of building scalable web services. APIs provide the fabric through which systems interact, and REST has become synonymous with APIs. The depth, breadth, and ease of use of Go, make it a breeze for developers to work with it to build robust Web APIs. This course will teach you to build a RESTful web server. You will learn to add routing and handle requests, thus making your APIs more RESTful. Later, by testing and profiling your code you will ensure it runs correctly and behaves well at scale. Moving on with security, you will learn to log requests and outcomes of API calls and speed up performance using the Echo framework. By the end of this course, you will have the knowledge you need to start building your own enterprise-grade RESTful web services that are production-ready, secure, scalable, and reliable.
About the Author
Bartlomiej Walczak has almost two decades of web development and security experience. He wrote front-ends and back-ends at various scales in PHP, Python, Node.js, and Go. He has worked for large clients and small startups, learning the value of structure from the former, and agility from the latter. These days, Go remains his language of choice; he prefers it due to its ease of use, extensive testing framework, and powerful multithreading capabilities.
This video explains the course prerequisites and provides an entire overview of the course.
Go is supported by many tools out there. Let’s select the ones that will be useful for us in this course.
Install Go and check the installation
Choose an IDE
Install additional helper tools
Go is a relatively new language. We will understand why it is particularly well suited to write RESTful APIs.
Understand the convenience of using the Go language
To have a working API we need a working web server.
Create a web server
Start and test the server
Understand error handling in Go
Any API needs to handle various routes and requests.
Learn where to get detailed help
Understand the HandleFunc type
Create the default route
Each route needs a handler to work correctly.
Create the handler function
Write header and output
Test the default route
Invalid request should produce an error with the appropriate status code.
Check if the request URL is correct
Output the error message and code
Test valid and invalid requests
Using curl to test API is cumbersome. Postman is an industry standard when it comes to organized API testing.
Create a collection
Create a request
Test the request
Every API needs a specification that describes what the functionality should be.
Establish basic premises
Define each request
Define typical responses
API data must persist in the event of server failure.
Select a permanent data store solution
Install required tools and libraries
Create a data structure
Stored data needs to be read and modified.
Establish data store location
Add data manipulation functions
Add data validation
Default router in Go doesn’t parse request methods and doesn’t understand URI parameters. For an API it’s vital that each method is handled differently and that parameters are properly parsed.
Create a handler function for a given resource
Add routing for collection
Add routing for an item
GET request to a resource collection should produce a list of items.
Create a generic response function
Write the request handler
Test the added route
POST request to resource collection should create a new item in the collection from the request body.
Understand POST, PUT and PATCH requests
Create a function to parse body content
Write and test POST request handler
GET request to a collection item should produce this item’s data. It’s also possible to streamline our testing using Postman’s additional capabilities.
Write and test GET request handler
Learn about Postman environments and variables
Save last inserted id into a Postman variable
PUT request to a collection item should replace the item’s current data with request body.
Write the request
Hook the request in the router
Test the request in Postman
PATCH request to a collection item should update the item’s current data with request body.
Write the request
Hook the request in the router
Test the request in Postman
DELETE request to a collection item should remove the item record from the collection.
Write the request
Test the request in Postman
Remove the last inserted id Postman variable
HEAD request to a resource should mirror GET request path but only output the header.
Learn about the use cases for HEAD request
Modify the GET request handlers
Test the requests in Postman
OPTIONS request should return a list of valid methods that a given resource accepts. It can also be used to document the API.
Create a generic OPTIONS response function
Hook the requests in the router
Test the requests in Postman
Tests should be an integral part of development. Bug-ridden code is not worth anything.
Understand the difference between various test types
Understand what makes a good test
Learn how unit tests are implemented in Go
Not all tests are created equal. Good ones cover all functionality and most of the test cases.
Understand the concept of a test suite
Write unit tests covering invalid cases
Write unit tests covering all valid cases
A decent test is better than no test at all. Learn how to write tests that are still meaningful, despite not being comprehensive.
Understand when a test is “good enough”
Write a minimum viable unit test
Learn about test setup and cleanup
Good code is good, but good and fast code is better. How to determine which code is slow?
Learn about benchmark implementation in Go
Write and run CRUD benchmarks
Write and run a separate benchmark for each operation
Caching is the best way to reduce execution time of your most common requests and should be the first API optimization.
Selecting solutions and specifying cache implementation
Creating cache structure and functions
Adding cache to request handlers
How do we make sure that we cache exactly what is being sent out?
Learn about interfaces in Go
Create a custom ResponseWriter
Inject the custom writer in the response chain
Some functionality depends on operations such as disk or network access that introduce considerable lag and uncertainty. How to test it reliably?
Learn about mock-ups
Create a ResponseWriter mock-up
Write unit tests for our cache Writer
When performing code optimization, it is important to measure the impact of the updated code. Sometimes the results might not be what you expect.
Prepare the database for benchmarks
Write benchmark tests for cached requests
Run benchmarks and compare the results
Frameworks are third-party code that can give you a significant head start when developing common features.
Learn the pros and cons of using frameworks
Learn about various Go frameworks
Explore gzip compression option
Echo is an extremely fast, extensible framework. We’re going to learn how to use it.
Start a web server in Echo
Write a root route handler
Test default functionality
How would our API look like when written in Echo? Let’s find out.
Add routes
Add route handlers
Test functionality in Postman using new environment
Important functionality is added to APIs via various types of middleware. You need to know your options.
Learn about middleware
Add simple middleware with default configuration
Write custom middleware and add configured middleware
This video summarizes the course and gives suggestions on further learning.
OpenCourser helps millions of learners each year. People visit us to learn workspace skills, ace their exams, and nurture their curiosity.
Our extensive catalog contains over 50,000 courses and twice as many books. Browse by search, by topic, or even by career interests. We'll match you to the right resources quickly.
Find this site helpful? Tell a friend about us.
We're supported by our community of learners. When you purchase or subscribe to courses and programs or purchase books, we may earn a commission from our partners.
Your purchases help us maintain our catalog and keep our servers humming without ads.
Thank you for supporting OpenCourser.