Implementing the RESTful Server
Understand how to implement a RESTful server in Go by organizing server logic across multiple files. Learn to use gorilla/mux subrouters for handling different HTTP methods and write handler functions to process client requests and interact with the database securely, including admin-only access control. Discover how to run the HTTP server as a goroutine for graceful shutdown on OS signals.
We'll cover the following...
Now that we are sure that the restdb package works as expected, we are ready to explain the implementation of the RESTful server. The server code is split into two files, both belonging to the main package: main.go and handlers.go. The main reason for doing so is to avoid having huge code files to work with and separating the functionality of the server logically.
The main() function
The most important part of main.go, which belongs to the main() function, is the following:
First, we need to define the default handler function—although this is not necessary, it is a good practice to have such a handler.
The MethodNotAllowedHandler handler is executed when we try to visit an endpoint using an unsupported HTTP method. ...