Dealer/pkg/internal/server/api/link_expander.go

33 lines
815 B
Go
Raw Normal View History

2024-08-19 06:35:24 +00:00
package api
import (
"encoding/base64"
"git.solsynth.dev/hydrogen/dealer/pkg/internal/services"
"github.com/gofiber/fiber/v2"
"sync"
2024-08-19 06:35:24 +00:00
)
var inProgress sync.Map
2024-08-19 06:35:24 +00:00
func getLinkMeta(c *fiber.Ctx) error {
2024-08-19 07:36:57 +00:00
targetEncoded := c.Params("*1")
targetRaw, _ := base64.StdEncoding.DecodeString(targetEncoded)
if ch, loaded := inProgress.LoadOrStore(targetEncoded, make(chan struct{})); loaded {
// If the request is already in progress, wait for it to complete
<-ch.(chan struct{})
} else {
// If this is the first request, process it and signal others
defer func() {
close(ch.(chan struct{}))
inProgress.Delete(targetEncoded)
}()
}
2024-08-19 06:35:24 +00:00
if meta, err := services.LinkExpand(string(targetRaw)); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.JSON(meta)
}
}