URL Shortener API
A simple URL shortener API built with Go and Firebase Firestore as the database backend. This API allows users to shorten long URLs and retrieve the original URL by accessing the shortened URL.
Table of Contents
- Introduction
- Getting Started
- Folder Structure
- Environment Variables
- API Endpoints
- Run the Application
- Dependencies
- License
Introduction
This project is a URL shortener API built in Go. It uses Firebase Firestore to store shortened URLs and their original counterparts. Users can shorten long URLs and later retrieve them by visiting the shortened URL.
Getting Started
To run this project locally, you will need Go installed on your system and a Firebase account to generate a service account key for accessing Firestore.
Prerequisites
- Go: Download and install Go from the official site Go Downloads.
- Firebase: Set up a Firebase project and create a service account key for Firestore. You can download the key JSON file from Firebase Console.
Install Dependencies
-
Clone the repository:
git clone https://github.com/amt27/url-shortener.git
cd url-shortener
-
Initialize Go modules and install dependencies:
go mod init url_shortener
go get firebase.google.com/go
-
Set the environment variable GOOGLE_APPLICATION_CREDENTIALS
to point to your Firebase service account JSON file:
export GOOGLE_APPLICATION_CREDENTIALS="path/to/your/firebase-adminsdk.json"
Folder Structure
The project structure is organized as follows:
cmd/shorturl/main.go
: The entry point for the application, where the server is started and routes are defined.
db/db.go
: Contains the logic for initializing the Firebase database connection using the Firestore client.
handlers/urlhandlers.go
: Contains the HTTP handler functions for creating shortened URLs and redirecting to the original URL.
utils/urlshortener.go
: Contains utility functions, such as generating random shortened URLs.
Environment Variables
Before running the project, ensure you have set the following environment variables:
API Endpoints
POST /shorten
GET /original/{shortened}
- Description: Redirects to the original URL associated with the shortened URL.
- Response: A 302 redirect to the original URL.
Run the Application
-
Start the Go server:
go run cmd/shorturl/main.go
-
The application will be accessible at http://localhost:8080
.
Dependencies
- Firebase Admin SDK: Used to interact with Firebase Firestore.
- Go: Programming language used for building the server.
- Go modules: To manage dependencies.
Install Go dependencies:
go get firebase.google.com/go
License
This project is licensed under the MIT License - see the LICENSE file for details.