feat(api): include booking URL in sync response
Some checks failed
CI / checks (push) Has been cancelled

Enhance the booking sync API to return the full booking URL in the
response.
The URL is constructed based on the request's scheme and host,
supporting
both HTTP and HTTPS, and uses the X-Forwarded-Proto header if present.
This provides clients with a direct link to the created booking
resource.
This commit is contained in:
Ruidy 2026-01-01 17:40:57 -04:00
parent be5b707fa0
commit fe6beeded5
No known key found for this signature in database
GPG key ID: 705C24D202990805

View file

@ -6,7 +6,9 @@ import (
"io"
"log/slog"
"net/http"
"strings"
"github.com/rjNemo/rentease/internal/constant"
"github.com/rjNemo/rentease/internal/service/booking"
)
@ -38,9 +40,17 @@ func handleSync(bs *booking.Service) http.HandlerFunc {
slog.Info("created booking from API", slog.String("name", b.Name), slog.String("platform", string(b.Platform)))
scheme := "http"
if proto := r.Header.Get("X-Forwarded-Proto"); proto != "" {
scheme = strings.TrimSpace(strings.Split(proto, ",")[0])
} else if r.TLS != nil {
scheme = "https"
}
bookingURL := fmt.Sprintf("%s://%s%s/%d", scheme, r.Host, constant.RouteBooking, b.ID)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode("👍"); err != nil {
if err := json.NewEncoder(w).Encode(fmt.Sprintf("👍 %s", bookingURL)); err != nil {
slog.Error("failed to write response", slog.Any("error", err))
}
}