mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +00:00
feat(api): include booking URL in sync response
Some checks failed
CI / checks (push) Has been cancelled
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:
parent
be5b707fa0
commit
fe6beeded5
1 changed files with 11 additions and 1 deletions
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue