diff --git a/pkg/handlers/docs.go b/pkg/handlers/docs.go index 7c06671..bb7a75f 100644 --- a/pkg/handlers/docs.go +++ b/pkg/handlers/docs.go @@ -112,7 +112,7 @@ func getOpenAPISpecWithURL(baseURL string) *OpenAPISpec { Version: "2.0.0", Contact: Contact{ Name: "API Support", - URL: "https://gitlab.com/foxixus/neomovies-api", + URL: "https://github.com/your-username/neomovies-api-go", }, }, Servers: []Server{ diff --git a/pkg/handlers/players.go b/pkg/handlers/players.go index 5fea0f0..682b2f9 100644 --- a/pkg/handlers/players.go +++ b/pkg/handlers/players.go @@ -3,125 +3,235 @@ package handlers import ( "encoding/json" "fmt" + "io" + "log" "net/http" + "net/url" + "strings" "time" - "github.com/gorilla/mux" - "neomovies-api/pkg/config" + "github.com/gorilla/mux" ) type PlayersHandler struct { - cfg *config.Config + config *config.Config } func NewPlayersHandler(cfg *config.Config) *PlayersHandler { - return &PlayersHandler{cfg: cfg} + return &PlayersHandler{ + config: cfg, + } } func (h *PlayersHandler) GetAllohaPlayer(w http.ResponseWriter, r *http.Request) { + log.Printf("GetAllohaPlayer called: %s %s", r.Method, r.URL.Path) + vars := mux.Vars(r) + log.Printf("Route vars: %+v", vars) + imdbID := vars["imdb_id"] if imdbID == "" { - http.Error(w, "imdb_id is required", http.StatusBadRequest) + log.Printf("Error: imdb_id is empty") + http.Error(w, "imdb_id path param is required", http.StatusBadRequest) return } - if h.cfg.AllohaToken == "" { - http.Error(w, "ALLOHA_TOKEN is not configured", http.StatusServiceUnavailable) + + log.Printf("Processing imdb_id: %s", imdbID) + + if h.config.AllohaToken == "" { + log.Printf("Error: ALLOHA_TOKEN is missing") + http.Error(w, "Server misconfiguration: ALLOHA_TOKEN missing", http.StatusInternalServerError) return } - - // Примерная ссылка встраивания. При необходимости скорректируйте под фактический эндпоинт Alloha - iframeSrc := fmt.Sprintf("https://alloha.tv/embed?imdb_id=%s&token=%s", imdbID, h.cfg.AllohaToken) - renderIframePage(w, "Alloha Player", iframeSrc) + + idParam := fmt.Sprintf("imdb=%s", url.QueryEscape(imdbID)) + apiURL := fmt.Sprintf("https://api.alloha.tv/?token=%s&%s", h.config.AllohaToken, idParam) + log.Printf("Calling Alloha API: %s", apiURL) + + resp, err := http.Get(apiURL) + if err != nil { + log.Printf("Error calling Alloha API: %v", err) + http.Error(w, "Failed to fetch from Alloha API", http.StatusInternalServerError) + return + } + defer resp.Body.Close() + + log.Printf("Alloha API response status: %d", resp.StatusCode) + + if resp.StatusCode != http.StatusOK { + http.Error(w, fmt.Sprintf("Alloha API error: %d", resp.StatusCode), http.StatusBadGateway) + return + } + + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Printf("Error reading Alloha response: %v", err) + http.Error(w, "Failed to read Alloha response", http.StatusInternalServerError) + return + } + + log.Printf("Alloha API response body: %s", string(body)) + + var allohaResponse struct { + Status string `json:"status"` + Data struct { + Iframe string `json:"iframe"` + } `json:"data"` + } + + if err := json.Unmarshal(body, &allohaResponse); err != nil { + log.Printf("Error unmarshaling JSON: %v", err) + http.Error(w, "Invalid JSON from Alloha", http.StatusBadGateway) + return + } + + if allohaResponse.Status != "success" || allohaResponse.Data.Iframe == "" { + log.Printf("Video not found or empty iframe") + http.Error(w, "Video not found", http.StatusNotFound) + return + } + + iframeCode := allohaResponse.Data.Iframe + if !strings.Contains(iframeCode, "<") { + iframeCode = fmt.Sprintf(``, iframeCode) + } + + htmlDoc := fmt.Sprintf(`Alloha Player%s`, iframeCode) + + // Авто-исправление экранированных кавычек + htmlDoc = strings.ReplaceAll(htmlDoc, `\"`, `"`) + htmlDoc = strings.ReplaceAll(htmlDoc, `\'`, `'`) + + w.Header().Set("Content-Type", "text/html") + w.Write([]byte(htmlDoc)) + + log.Printf("Successfully served Alloha player for imdb_id: %s", imdbID) } func (h *PlayersHandler) GetLumexPlayer(w http.ResponseWriter, r *http.Request) { + log.Printf("GetLumexPlayer called: %s %s", r.Method, r.URL.Path) + vars := mux.Vars(r) + log.Printf("Route vars: %+v", vars) + imdbID := vars["imdb_id"] if imdbID == "" { - http.Error(w, "imdb_id is required", http.StatusBadRequest) + log.Printf("Error: imdb_id is empty") + http.Error(w, "imdb_id path param is required", http.StatusBadRequest) return } - if h.cfg.LumexURL == "" { - http.Error(w, "LUMEX_URL is not configured", http.StatusServiceUnavailable) + + log.Printf("Processing imdb_id: %s", imdbID) + + if h.config.LumexURL == "" { + log.Printf("Error: LUMEX_URL is missing") + http.Error(w, "Server misconfiguration: LUMEX_URL missing", http.StatusInternalServerError) return } - - // Примерная ссылка встраивания. При необходимости скорректируйте под фактический эндпоинт Lumex - iframeSrc := fmt.Sprintf("%s/embed/%s", h.cfg.LumexURL, imdbID) - renderIframePage(w, "Lumex Player", iframeSrc) + + url := fmt.Sprintf("%s?imdb_id=%s", h.config.LumexURL, url.QueryEscape(imdbID)) + log.Printf("Generated Lumex URL: %s", url) + + iframe := fmt.Sprintf(``, url) + htmlDoc := fmt.Sprintf(`Lumex Player%s`, iframe) + + w.Header().Set("Content-Type", "text/html") + w.Write([]byte(htmlDoc)) + + log.Printf("Successfully served Lumex player for imdb_id: %s", imdbID) } func (h *PlayersHandler) GetVibixPlayer(w http.ResponseWriter, r *http.Request) { + log.Printf("GetVibixPlayer called: %s %s", r.Method, r.URL.Path) + vars := mux.Vars(r) + log.Printf("Route vars: %+v", vars) + imdbID := vars["imdb_id"] if imdbID == "" { - http.Error(w, "imdb_id is required", http.StatusBadRequest) + log.Printf("Error: imdb_id is empty") + http.Error(w, "imdb_id path param is required", http.StatusBadRequest) return } + + log.Printf("Processing imdb_id: %s", imdbID) - if h.cfg.VibixToken == "" { - http.Error(w, "VIBIX_TOKEN is not configured", http.StatusServiceUnavailable) + if h.config.VibixToken == "" { + log.Printf("Error: VIBIX_TOKEN is missing") + http.Error(w, "Server misconfiguration: VIBIX_TOKEN missing", http.StatusInternalServerError) return } - vibixHost := h.cfg.VibixHost + + vibixHost := h.config.VibixHost if vibixHost == "" { vibixHost = "https://vibix.org" } - req, err := http.NewRequest("GET", fmt.Sprintf("%s/api/v1/publisher/videos/imdb/%s", vibixHost, imdbID), nil) + apiURL := fmt.Sprintf("%s/api/v1/publisher/videos/imdb/%s", vibixHost, imdbID) + log.Printf("Calling Vibix API: %s", apiURL) + + req, err := http.NewRequest("GET", apiURL, nil) if err != nil { - http.Error(w, "failed to create request", http.StatusInternalServerError) + log.Printf("Error creating Vibix request: %v", err) + http.Error(w, "Failed to create request", http.StatusInternalServerError) return } + req.Header.Set("Accept", "application/json") - req.Header.Set("Authorization", "Bearer "+h.cfg.VibixToken) + req.Header.Set("Authorization", "Bearer "+h.config.VibixToken) req.Header.Set("X-CSRF-TOKEN", "") client := &http.Client{Timeout: 8 * time.Second} resp, err := client.Do(req) if err != nil { - http.Error(w, "failed to fetch player", http.StatusBadGateway) + log.Printf("Error calling Vibix API: %v", err) + http.Error(w, "Failed to fetch from Vibix API", http.StatusInternalServerError) return } defer resp.Body.Close() + log.Printf("Vibix API response status: %d", resp.StatusCode) + if resp.StatusCode != http.StatusOK { - http.Error(w, "movie not found on Vibix", http.StatusNotFound) + log.Printf("Vibix API error: %d", resp.StatusCode) + http.Error(w, fmt.Sprintf("Vibix API error: %d", resp.StatusCode), http.StatusBadGateway) return } - var data struct { + body, err := io.ReadAll(resp.Body) + if err != nil { + log.Printf("Error reading Vibix response: %v", err) + http.Error(w, "Failed to read Vibix response", http.StatusInternalServerError) + return + } + + log.Printf("Vibix API response body: %s", string(body)) + + var vibixResponse struct { ID interface{} `json:"id"` IframeURL string `json:"iframe_url"` } - if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { - http.Error(w, "failed to parse Vibix response", http.StatusBadGateway) + + if err := json.Unmarshal(body, &vibixResponse); err != nil { + log.Printf("Error unmarshaling Vibix JSON: %v", err) + http.Error(w, "Invalid JSON from Vibix", http.StatusBadGateway) return } - if data.ID == nil || data.IframeURL == "" { - http.Error(w, "movie not found on Vibix", http.StatusNotFound) + + if vibixResponse.ID == nil || vibixResponse.IframeURL == "" { + log.Printf("Video not found or empty iframe_url") + http.Error(w, "Video not found", http.StatusNotFound) return } - renderIframePage(w, "Vibix Player", data.IframeURL) -} - -func renderIframePage(w http.ResponseWriter, title, src string) { - w.Header().Set("Content-Type", "text/html; charset=utf-8") - fmt.Fprintf(w, ` - - - -%s - - - - - -`, title, src) + log.Printf("Generated Vibix iframe URL: %s", vibixResponse.IframeURL) + + iframe := fmt.Sprintf(``, vibixResponse.IframeURL) + htmlDoc := fmt.Sprintf(`Vibix Player%s`, iframe) + + w.Header().Set("Content-Type", "text/html") + w.Write([]byte(htmlDoc)) + + log.Printf("Successfully served Vibix player for imdb_id: %s", imdbID) } \ No newline at end of file