-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add a tutorial example for URL Shortner built using DiceDB and Go #1259
base: master
Are you sure you want to change the base?
feat: Add a tutorial example for URL Shortner built using DiceDB and Go #1259
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for contributing a tutorial for dicedb. It's a great way to help new users get started.
I have left some comments on this PR, please address them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's put this under the SDK section instead of the getting started section.
|
||
1. Go installed (at least version 1.18) | ||
2. DiceDB server running locally | ||
3. Basic familiarity with Go and DiceDB |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove this.
3. Basic familiarity with Go and DiceDB |
2. DiceDB server running locally | ||
3. Basic familiarity with Go and DiceDB | ||
|
||
## Environment setup |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
## Environment setup | |
## Setup |
|
||
## Environment setup | ||
|
||
Refer to [DiceDB Installation Guide](https://dicedb.io/get-started/installation/) to get your DiceDB server up and running with a simple Docker command. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prefer relative URLs instead of absolute URLs. This would mean that if the docs move to a new domain in the future, we wouldn't have to manually update all the links.
Refer to [DiceDB Installation Guide](https://dicedb.io/get-started/installation/) to get your DiceDB server up and running with a simple Docker command. | |
Refer to [DiceDB Installation Guide](/get-started/installation) to get your DiceDB server up and running with a simple Docker command. |
|
||
1. Clone the repository | ||
```bash | ||
git clone https://github.com/Prachi-Jamdade/url-shortner-go-dicedb |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two things,
- The examples should allow users to have a super minimal setup to try stuff out. Ideally, they should not require much setup so they don't need to clone repositories to get started.
- We shouldn't be linking personal repositories in the official docs. If you would like to make the code available to the users you can add it under the examples directory of this repo.
func FindURLByID(id string) (models.URL, error) { | ||
ctx := context.Background() | ||
|
||
// Retrieve the serialized URL data from DiceDB | ||
stringCmd := db.Get(ctx, id) | ||
urlData, err := stringCmd.Result() | ||
if err != nil { | ||
if err == dicedb.Nil { | ||
return models.URL{}, errors.New("URL not found") | ||
} | ||
return models.URL{}, fmt.Errorf("failed to retrieve URL: %w", err) | ||
} | ||
|
||
// Deserialize the JSON data back into the URL struct | ||
var url models.URL | ||
err = json.Unmarshal([]byte(urlData), &url) | ||
if err != nil { | ||
return models.URL{}, fmt.Errorf("failed to decode URL data: %w", err) | ||
} | ||
return url, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on above comments this can be simplified down to just a couple of lines of code. All it needs to do is lookup keys by short URL.
func CreateShortURLService(longURL string) (string, error) { | ||
id := uuid.New().String()[:8] // Shorten ID for URL | ||
|
||
url := models.URL{ | ||
ID: id, | ||
LongURL: longURL, | ||
ShortURL: "http://localhost:8080/" + id, | ||
} | ||
|
||
if err := repository.SaveURL(url); err != nil { | ||
fmt.Println(err) | ||
return "", err | ||
} | ||
|
||
return url.ShortURL, nil | ||
} | ||
|
||
func GetOriginalURLService(id string) (string, error) { | ||
url, err := repository.FindURLByID(id) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return url.LongURL, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to have "services" here. Just direct SDK calls from within the go application are enough.
package utils | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func RespondWithError(c *gin.Context, code int, message string) { | ||
c.JSON(code, gin.H{"error": message}) | ||
} | ||
|
||
func RespondWithJSON(c *gin.Context, code int, payload interface{}) { | ||
c.JSON(code, payload) | ||
} | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to have this in a separate package/file. the actual code these functions encapsulate is simpler to understand at first glance as compared to hiding them behind an abstraction
} | ||
``` | ||
|
||
6. `main.go`: Sets up the Gin (a HTTP Web Framework) router and registers routes. This is the entry point of the application. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please have the entire code for the application contained within main.go
.
## Conclusion | ||
|
||
This project demonstrates how to set up a simple yet functional API for URL shortening, which can be expanded further with additional features or integrated into a larger application. With Go’s performance and DiceDB’s efficiency, this offers a lightweight solution for URL management. | ||
|
||
Find the complete code for this example on [Github](https://github.com/Prachi-Jamdade/url-shortner-go-dicedb). | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove this.
## Conclusion | |
This project demonstrates how to set up a simple yet functional API for URL shortening, which can be expanded further with additional features or integrated into a larger application. With Go’s performance and DiceDB’s efficiency, this offers a lightweight solution for URL management. | |
Find the complete code for this example on [Github](https://github.com/Prachi-Jamdade/url-shortner-go-dicedb). |
Hi @JyotinderSingh, I have addressed above mentioned review changes and pushed the code. Please take a look at it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor comments in the shortener code.
examples/url-shotener/main.go
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: typo in directory name url-shortner
examples/url-shotener/main.go
Outdated
// Initialize DiceDB connection | ||
func init() { | ||
db = dicedb.NewClient(&dicedb.Options{ | ||
Addr: "localhost:7379", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Get hostname and port from ENV variables?
Similar to:
dice/examples/leaderboard-go/main.go
Lines 36 to 44 in 44779c2
dhost := "localhost" | |
if val := os.Getenv("DICEDB_HOST"); val != "" { | |
dhost = val | |
} | |
dport := "7379" | |
if val := os.Getenv("DICEDB_PORT"); val != "" { | |
dport = val | |
} |
examples/url-shotener/main.go
Outdated
} | ||
|
||
// Creates a short URL from a given long URL | ||
func CreateShortURL(c *gin.Context) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
func CreateShortURL(c *gin.Context) { | |
func createShortURL(c *gin.Context) { |
examples/url-shotener/main.go
Outdated
} | ||
|
||
// Redirects to the original URL based on the short URL ID | ||
func RedirectURL(c *gin.Context) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
func RedirectURL(c *gin.Context) { | |
func redirectURL(c *gin.Context) { |
examples/url-shotener/main.go
Outdated
requestBody.ID = shortID | ||
requestBody.ShortURL = "http://localhost:8080/" + shortID |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to store ID
and ShortURL
in the struct?
Having just LongURL
is sufficient?
The mapping b/w ID
and LongURL
is anyway stored in the DiceDB.
Thanks for addressing the comments @Prachi-Jamdade. Could you please resolve comments by @everlearner as well? |
Hey @JyotinderSingh, pushed the code addressing changes raised by @everlearner |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making the changes.
Hey @JyotinderSingh, any update ? |
This commit adds an example of how to integrate the DiceDB Go SDK for building a URL shortener application.
The tutorial walks through setting up a basic URL shortening service, providing -
The application utilizes the
Set
andGet
commands from DiceDB to store and retrieve shortened URLs, and the example is designed to help developers quickly understand and implement a URL shortener using DiceDB and Go.