In this chapter, I complete the development of the SportsStore application and prepare it for deployment.
You can download the example project for this chapter—and for all the other chapters in this book—from https://github.com/apress/pro-go. See Chapter 2 for how to get help if you have problems running the examples.
Completing the Administration Features
Two of the four administration sections defined in Chapter 37 are not yet implemented. I define both of these features at the same time in this section, reflecting the fact that they are simpler than the product and category features.
Extending the Repository
Adding an Interface in the repository.go File in the models Folder
The SetOrderShipped method will be used to update an existing Order to indicate when it has been shipped. The Init method corresponds to a method name already defined by the SQL implementation of the interface and will be used to allow the administrator to prepare the database for first use after it has been deployed.
The Contents of the update_order.sql File in the sql Folder
Adding a New Command in the sql_repo.go File in the models/repo Folder
Adding a Configuration Setting in the config.json File in the sportsstore Folder
The Contents of the sql_order_update.go File in the models/repo Folder
Implementing the Request Handlers
The New Contents of the orders_handler.go File in the admin Folder
The New Contents of the database_handler.go File in the admin Folder
There are handler methods for each of the operations that can be performed on the database, which will allow the administrator to jump-start the application after the application has been prepared for deployment later in this chapter.
Creating the Templates
The Contents of the admin_orders.html File in the templates Folder
The Contents of the admin_database.html File in the templates Folder
Restricting Access to the Administration Features
Granting open access to the administration features simplifies development but should never be allowed in production. Now that the administration features are complete, it is time to make sure they are available only to authorized users.
Creating the User Store and Request Handler
The Contents of the user_store.go File in the admin/auth Folder
The Contents of the auth_handler.go File in the admin Folder
The GetSignIn method renders a template that will prompt the user for their credentials and displays a message that is stored in the session. The PostSignIn method receives the credentials from the form and either signs the user into the application or adds a message to the session and redirects the browser so the user can try again.
The Contents of the signin.html File in the templates Folder
This template prompts the user for their account name and password, which is posted back to the request handler.
The Contents of the signout_handler.go File in the admin Folder
The Contents of the user_widget.html File in the templates Folder
Adding a Widget in the admin.html File in the templates Folder
Configuring the Application
Adding a Configuration Setting in the config.json File in the sportsstore Folder
Configuring the Application in the main.go File in the sportsstore Folder
Creating a Web Service
The final feature I am going to add is a simple web service, just to show how it can be done. I am not going to use authorization to protect the web service, which can be a complex process that depends on the type of clients expected to require access. This means that any user will be able to modify the database. If you are deploying a real web service, then you can use cookies in much the same way as I have done in this example. If your clients don’t support cookies, then JSON Web Tokens (JWTs) can be used, as described at https://jwt.io.
The Contents of the rest_handler.go File in the store Folder
Registering a Handler in the main.go File in the sportsstore Folder
Adding a New Product
Adding a New Product in Windows
Requesting Data
Requesting Data in Windows
Preparing for Deployment
In this section, I will prepare the SportsStore application and create a container that can be deployed into production. This isn’t the only way that a Go application can be deployed, but I picked Docker containers because they are widely used and because they suit web applications. This is not a complete guide to deployment, but it will give you a sense of the process to prepare an application.
Installing the Certificates
The first step is to add the certificates that will be used for HTTPS. As explained in Chapter 24, you can create a self-signed certificate if you don’t have a real certificate available, or you can use the certificate files from the GitHub repository for this book (which contain a self-signed certificate that I created).
Configuring the Application
Changing Settings in the config.json File in the sportsstore Folder
Make sure that the values you specify for the httpsCert and httpsKey properties match the names of your certificate files and that the certificate files are in the sportsstore folder.
Building the Application
Setting Linux as the Build Target
Building the Application
If you are a Windows user, you can return to a normal Windows build with the following command: $Env:GOOS = "windows"; $Env:GOARCH = "amd64". But don’t run this command until you have completed the deployment process.
Installing Docker Desktop
Go to docker.com and download and install the Docker Desktop package. Follow the installation process, reboot your machine, and run the command shown in Listing 38-27 to check that Docker has been installed and is in your path. (The Docker installation process seems to change often, which is why I am not being more specific about the process.)
You will have to create an account on docker.com to download the installer.
Checking the Docker Desktop Installation
Creating the Docker Configuration Files
The Contents of the Dockerfile in the sportsstore Folder
Creating an Image
Creating and Starting a Container
Stopping Containers
Summary
In this chapter, I completed the SportsStore application by finishing the administration features, configuring authorization, and creating a basic web service, before preparing the application for deployment using a Docker container.
That’s all I have to teach you about Go. I can only hope that you have enjoyed reading this book as much as I enjoyed writing it, and I wish you every success in your Go projects.