In our Beta 9 release, we updated our Go driver. In this tutorial, we will learn to build a simple SurrealDB URL shortener using the Go driver. A URL shortener is a very simple yet powerful tool that can help you be more productive. URL shorteners are used to condense long URLs into shorter, more manageable links and also to track and analyse a particular website. But that's not it. URL shorteners use databases to store and manage the mappings between the original URLs and their shortened versions.
Prerequisites
Before you begin, make sure you have the following software installed on your system:
Go (version 1.17 or higher): https://golang.org/doc/install
SurrealDB: /install
Project Structure
Our project will have the following file structure:
Step 1: Setting Up the Project
Create a new folder for your project, and inside the folder, initialise it as a Go module:
Replace yourusername with your GitHub username or any other namespace you prefer.
Step 2: Import the SurrealDB Go Driver and other required packages.
Installation
Install the SurrealDB go driver by running the following command
In your go.mod file, add the following dependencies:
How does the URL shortener work?
SurrealDB is at the heart of the URL shortener. It will efficiently map the original URLs to their shortened counterparts. When a user submits a long URL, the shortening service will generate a unique key which is then used to create the shortened URL. This unique key, along with the original URL, will be stored as a new record in the database. When someone accesses the shortened URL, the service will look up the corresponding key in the database, retrieve the original URL, and redirect the user to that destination.
Step 3: Setting Up the Repository
Create a new file called repository.go inside the repository folder. This file will contain the ShortenerRepository struct and methods to interact with the SurrealDB database. The repository design pattern is responsible for handling the database interactions, such as creating short URLs, retrieving the original URLs from the shortened ones, and managing the database connections. This separation of concerns allows the web service layer to focus on handling HTTP requests and responses without worrying about the details of data access and storage.
The ShortenerRepository struct is defined with a single field db of type *surreal.DB, which is a pointer to the SurrealDB database connection.
1. Connect to SurrealDB: In order to store the original and shortened links on the SurrealDB database, you first have to establish a connection to the SurrealDB instance and connect to a local database endpoint.
db, err := surrealdb.New(address)
2. db.Signin: This method signs in to the SurrealDB instance using the provided credentials (username and password).
_, err = db.Signin(map[string]string{ "user": "root", "pass": "root", })
3. db.Use: You can select the namespace and database to be used for the application.
_, err = db.Use(namespace, database)
4. db.Close: The close method can be called at the end of the program or during shutdown.
5. db.Create : db.Create method is used to create a new record in the urls table of the database. The first parameter of the function is the table name, in this case, "urls".
The second parameter is a map[string]interface{}, which represents the data to be inserted into the new record. The keys of the map correspond to the column names in the urls table, and their respective values are the function's input parameters original and shortened
6. db.Query: The purpose of this method is to query the database and find the corresponding original URL for a given shortened URL ID. db.Query function is called to execute a SurrealQL query on the database. This query selects all columns from the urls table where the shortened column value matches the provided $shortened parameter, and it limits the result to one record.
Step 4: Implementing the Web Service
The web.go file will contain the code to handle HTTP requests and responses. There are two main handler functions in this code:
1. ShortenURL: This function shortens a given URL. When an HTTP request is made to the handler, it expects a form value named "url" to be provided. The function checks if the URL has an "http://" or "https://" prefix, and if not, it adds "https://". It then generates a shortened URL using the shortenURL helper function and saves the mapping in the repository using ws.repository.CreateShortUrl(). Finally, it sends the shortened URL as a JSON response to the client.
2. shortenURL: This function takes a single parameter, redirectUrl, which is the base URL. You generate a 6-character random string by appending a random character to the empty string "s" and then concatenate it to the base URL.
3. RedirectURL: When a shortened URL is accessed, this function redirects the user to the original URL. It does this by extracting the URL's ID from the request path and fetching the original URL from the repository using ws.repository.FindShortenedURL(). The user is then redirected to the original URL.
Step 5: The main function.
The main.go function brings together the components implemented in web.go and repository.go to create a fully functional URL shortener application. The main.go file is responsible for initialising the database repository, setting up the web service, and starting the HTTP server to handle incoming requests.
We start by importing the required packages, including those implemented in the web and repository files. Additionally, we import the logrus package for logging and the http package for handling HTTP requests.
Next, we set up constants for the application, such as the listening port, the redirect address, and the database connection details like URL, namespace, and database name.
We then define HTTP endpoints and their corresponding handlers, which were implemented in the web.go file. In this case, we have two endpoints:
/shorten: Handles POST requests to shorten a given URL.
/: Handles GET requests for redirection of shortened URLs to their original counterparts.
Running the URL shortener
To test the URL shortener, we will first start the SurrealDB server on our local machine using the following command.
surreal sql -c <http://localhost:8000> --ns surrealdb-conference-content --db urlshortner
Run the URL shortener with the following command
go run main.go
We will then run the following bash script using the following command in another terminal
./shortenURL.sh
Following is the screenshot of original URL and the shortened URL
Conclusion
In this tutorial, you have successfully built a URL shortener using Go and SurrealDB. You've learned how to set up your project structure, create a connection to the SurrealDB database, create a repository to interact with the database, and implement a web service to handle HTTP requests for shortening and redirecting URLs. You can find the code here.
By following this tutorial, you have gained a deeper understanding of building web applications using Go and interacting with databases like SurrealDB. This knowledge can be applied to create other web applications or even extend the functionality of this URL shortener. Happy coding!
