Creating a URL shortener can be a fun and educational project, especially when using modern tools like Go (Golang) and Cursor AI. In this article, we will walk through the steps to build a simple yet effective URL shortener in approximately 15 minutes. We'll incorporate basic functionalities, including a web interface to accept long URLs, the creation and storage of shortened URLs, and the ability to redirect users to the original URLs.
Create a Directory
Start by creating a new directory for your URL shortener project.
[mkdir URLShortener](https://www.topview.ai/blog/detail/How-to-Shorten-a-URL)
cd URLShortener
Create the main.go and index.html Files
Inside your project directory, you'll want to create two essential files: main.go
(for your Go application) and index.html
(for the front-end interface).
We will need an SQLite database to store the long URLs along with their corresponding shortened codes. Our database schema will consist of a single table, URLs
, with the following columns:
ID
: Auto-incrementing identifierlong_url
: Column for the original long URLshort_code
: Column for the generated shortened URL (must be unique)CREATE TABLE IF NOT EXISTS URLs (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
long_url TEXT NOT NULL,
short_code TEXT UNIQUE NOT NULL
);
Create a basic Go HTTP server that serves the index.html
and handles POST requests for shortening URLs. The application will generate a unique shortcode for each URL, store it in the database, and serve it back to the user.
package main
import (
"database/sql"
"log"
"net/http"
"github.com/mattn/go-sqlite3"
)
func main() (
http.HandleFunc("/", serveIndex)
http.HandleFunc("/app/shorten", shortenURL)
log.Println("Server running on http://localhost:8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal(err)
)
}
// serveIndex serves the index.html page
func serveIndex(w http.ResponseWriter, r *http.Request) (
// Your code to serve the index.html
)
// shortenURL handles the URL shortening logic
func shortenURL(w http.ResponseWriter, r *http.Request) (
// Your code to generate shortcodes and insert into the database
)
When a user submits a long URL through the form, the server will generate a unique shortcode. This can be done randomly or by encoding the next available ID using a base like Base58 encoding to ensure uniqueness.
The application must handle requests to redirect users from the short URL to the original URL. If the entered shortcode exists in the database, users will be redirected to the long URL, otherwise, an error message will be shown.
We can improve the user experience by applying CSS styles to the index.html
. Using a CSS library like Tailwind CSS can help you create a sleek and responsive interface quickly. Add it via a CDN link in your HTML.
<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.0.3/dist/tailwind.min.css" rel="stylesheet">
After implementing the necessary components and styles, run your application using:
go run main.go
Visit http://localhost:8080
in your web browser, and you can now test the URL shortening functionality.
With these steps, you've successfully built your URL shortener using Golang and Cursor AI in just 15 minutes. The project can be further expanded by adding features such as custom short codes, expiration dates, or improved error handling. Feel free to explore and modify as you see fit.
1. What prerequisites do I need to run this project?
You will need to have Go installed on your machine, as well as the SQLite driver for Go.
2. How does the shortcode generation work?
The generation of shortcodes can either be done randomly or through an encoding algorithm such as Base58. This ensures that the shortcodes are unique and user-friendly.
3. Can I customize the short URL?
Yes, the application can be modified to allow users to create custom shortcodes when shortening URLs.
4. How do I run the application?
Simply use the command go run main.go
in your terminal within the project directory.
5. What improvements can be made to this project?
You could implement additional features like user accounts, analytics on URL clicks, or a dashboard to manage shortened URLs.
In addition to the incredible tools mentioned above, for those looking to elevate their video creation process even further, Topview.ai stands out as a revolutionary online AI video editor.
TopView.ai provides two powerful tools to help you make ads video in one click.
Materials to Video: you can upload your raw footage or pictures, TopView.ai will edit video based on media you uploaded for you.
Link to Video: you can paste an E-Commerce product link, TopView.ai will generate a video for you.