Use base32 encoded UUID to reduce address size

This commit is contained in:
nemunaire 2025-10-20 12:06:53 +07:00
commit a45d136546
4 changed files with 62 additions and 13 deletions

View file

@ -22,8 +22,10 @@
package api
import (
"encoding/base32"
"fmt"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
@ -50,16 +52,36 @@ func NewAPIHandler(store storage.Storage, cfg *config.Config) *APIHandler {
}
}
// uuidToBase32 converts a UUID to a URL-safe Base32 string (without padding)
// with hyphens every 7 characters for better readability
func uuidToBase32(id uuid.UUID) string {
// Use RFC 4648 Base32 encoding (URL-safe)
encoded := base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(id[:])
// Convert to lowercase for better readability
encoded = strings.ToLower(encoded)
// Insert hyphens every 7 characters
var result strings.Builder
for i, char := range encoded {
if i > 0 && i%7 == 0 {
result.WriteRune('-')
}
result.WriteRune(char)
}
return result.String()
}
// CreateTest creates a new deliverability test
// (POST /test)
func (h *APIHandler) CreateTest(c *gin.Context) {
// Generate a unique test ID (no database record created)
testID := uuid.New()
// Generate test email address
// Generate test email address using Base32-encoded UUID
email := fmt.Sprintf("%s%s@%s",
h.config.Email.TestAddressPrefix,
testID.String(),
uuidToBase32(testID),
h.config.Email.Domain,
)
@ -94,10 +116,10 @@ func (h *APIHandler) GetTest(c *gin.Context, id openapi_types.UUID) {
apiStatus = TestStatusPending
}
// Generate test email address
// Generate test email address using Base32-encoded UUID
email := fmt.Sprintf("%s%s@%s",
h.config.Email.TestAddressPrefix,
id.String(),
uuidToBase32(id),
h.config.Email.Domain,
)