-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update go version to 1.22.2 and add API to fetch errors based on vide…
…o id
- Loading branch information
Showing
5 changed files
with
102 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module yt-clone-video-processing | ||
|
||
go 1.21 | ||
go 1.22.2 | ||
|
||
require ( | ||
github.com/aws/aws-sdk-go-v2 v1.24.1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package handlers | ||
|
||
import ( | ||
"database/sql" | ||
"net/http" | ||
) | ||
|
||
type Dependencies struct { | ||
DBConn *sql.DB | ||
} | ||
|
||
func (dependencies *Dependencies) ApiHandler() *http.ServeMux { | ||
mux := http.NewServeMux() | ||
|
||
mux.HandleFunc("GET /videos/errors/{id}", dependencies.errorHandler) | ||
|
||
return mux | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
) | ||
|
||
type FileStatus struct { | ||
Vid int64 `json:"vid"` | ||
Quality string `json:"quality"` | ||
ErrorMessage string `json:"errorMessage"` | ||
} | ||
|
||
const ( | ||
selectErrorsUsingVid = "SELECT vid, quality, error FROM job_status WHERE vid = $1 AND success = false" | ||
id = "id" | ||
contentType = "Content-Type" | ||
applicationJson = "application/json" | ||
) | ||
|
||
func (dependencies *Dependencies) errorHandler(w http.ResponseWriter, r *http.Request) { | ||
exec, err := dependencies.DBConn.Query(selectErrorsUsingVid, r.PathValue(id)) | ||
if err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
_, err := fmt.Fprintf(w, err.Error()) | ||
|
||
if err != nil { | ||
writeInternalServerError(w, err) | ||
} | ||
return | ||
} | ||
|
||
defer func() { | ||
err = exec.Close() | ||
if err != nil { | ||
writeInternalServerError(w, err) | ||
} | ||
}() | ||
|
||
statuses := make([]FileStatus, 0) | ||
for exec.Next() { | ||
var vid int64 | ||
var quality string | ||
var errorMessage string | ||
err := exec.Scan(&vid, &quality, &errorMessage) | ||
if err != nil { | ||
log.Println("Error scanning job status:", err) | ||
} | ||
statuses = append(statuses, FileStatus{ | ||
Vid: vid, | ||
Quality: quality, | ||
ErrorMessage: errorMessage, | ||
}) | ||
} | ||
|
||
encoder, err := json.Marshal(statuses) | ||
if err != nil { | ||
writeInternalServerError(w, err) | ||
return | ||
} | ||
w.Header().Set(contentType, applicationJson) | ||
_, err = w.Write(encoder) | ||
|
||
if err != nil { | ||
writeInternalServerError(w, err) | ||
} | ||
} | ||
|
||
func writeInternalServerError(w http.ResponseWriter, err error) { | ||
w.WriteHeader(http.StatusInternalServerError) | ||
log.Println(err) | ||
} |