PitchHut logo
Efficient short link generation and tracking for Go applications.
Pitch

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.

Description

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.

Key Features

  • Short Link Creation: Simplify URLs for easier sharing and tracking.
  • Click Tracking: Monitor link engagement with detailed tracking capabilities.
  • OG Preview Pages: Enhance link previews with customizable metadata.
  • Flexible Architecture: Leverage pluggable processors for extended functionality.
  • Storage Options: Choose between Redis and in-memory storage solutions.

Getting Started

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))

Usage Example

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.

Custom Processors

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.

Standalone Server

A fully functional Redis-backed server can be quickly initiated using Docker:

docker compose up -d
go run ./cmd/deeplink

API Endpoints

The service provides several HTTP routes to interact with:

MethodPathDescription
POST/shortenCreate 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/healthHealth 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.

Configuration

Several environment variables aid in configuring the deeplink server, including:

VariableDefaultDescription
DEEPLINK_LISTEN_ADDR:8090Server listening address
DEEPLINK_BASE_URLhttp://localhost:8090/Base URL for generating short links
DEEPLINK_REDIS_ADDRlocalhost:6379Redis connection address
DEEPLINK_TEMPLATE_DIRtemplates/defaultDirectory for templates

Customization of 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.

Development

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.

0 comments

No comments yet.

Sign in to be the first to comment.