Deeplink provides a robust solution for short link generation, click tracking, and Open Graph preview pages in Go. With support for pluggable processors and both Redis and in-memory storage, it allows easy customization and integration into existing applications. Create and manage short links effortlessly.
deeplink is a comprehensive solution for short link generation, click tracking, and Open Graph (OG) preview pages built in Go. Designed with flexibility in mind, this project supports pluggable processors and offers the option to use Redis or in-memory storage, making it suitable for a variety of use cases.
Implementing your own short link generation service is straightforward using the following example:
svc, err := deeplink.New(deeplink.Config{
BaseURL: "https://link.example.com",
Store: deeplink.NewMemoryStore(),
TemplateDir: "templates/default",
})
if err != nil {
log.Fatal(err)
}
svc.Register(deeplink.RedirectProcessor{})
mux := http.NewServeMux()
mux.Handle("/", svc.Handler())
mux.HandleFunc("GET /hello", yourHandler)
log.Fatal(http.ListenAndServe(":8090", mux))
To create a short link, utilize the following CURL command:
curl -X POST http://localhost:8090/shorten \
-H 'Content-Type: application/json' \
-d '{"type":"redirect","url":"https://example.com/docs","title":"Docs"}'
Open the returned short_url in a web browser to access the shortened link.
Developers can create custom processors by implementing the Processor interface:
type Processor interface {
Type() string
Process(ctx context.Context, link *Link) error
}
For custom preview data, the Previewer interface can be implemented as follows:
type Previewer interface {
Preview(link *Link) any
}
See the example/custom directory for a working example and tests.
A fully functional Redis-backed server can be quickly initiated using Docker:
docker compose up -d
go run ./cmd/deeplink
The service provides several HTTP routes to interact with:
| Method | Path | Description |
|---|---|---|
| POST | /shorten | Create a short link |
| GET | /{shortID} | Preview page or 302 redirect |
| GET | /links/{type} | List links by type |
| GET | /links/{type}/{shortID} | Retrieve link detail with click count |
| GET | /health | Health check status |
For customizable routing based on platforms, additional paths are available related to app store redirects and file serving from the template directory. Support for iOS Universal Links and Android App Links can be achieved by placing the respective files in the specified template directory.
Several environment variables aid in configuring the deeplink server, including:
| Variable | Default | Description |
|---|---|---|
DEEPLINK_LISTEN_ADDR | :8090 | Server listening address |
DEEPLINK_BASE_URL | http://localhost:8090/ | Base URL for generating short links |
DEEPLINK_REDIS_ADDR | localhost:6379 | Redis connection address |
DEEPLINK_TEMPLATE_DIR | templates/default | Directory for templates |
The default templates located in templates/default/ can be altered for personalized branding and messaging, utilizing key fields from the Link data structure, including URL, Title, Description, and ImageURL.
For developers looking to contribute or modify the project:
go test ./... # Execute tests
go run ./cmd/deeplink # Start standalone server with Redis
Explore the potential of the deeplink package to streamline link management, enhance user engagement, and elevate marketing efforts.
No comments yet.
Sign in to be the first to comment.