Skip to content

Commit

Permalink
Update go version to 1.22.2 and add API to fetch errors based on vide…
Browse files Browse the repository at this point in the history
…o id
  • Loading branch information
cant-code committed Apr 11, 2024
1 parent 85b66f3 commit af386d8
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '1.21.x'
go-version: '1.22.x'
- name: Install dependencies
run: go mod download -x
- name: Build
Expand Down
9 changes: 8 additions & 1 deletion cmd/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package main

import (
"log"
"net/http"
"yt-clone-video-processing/internal/consumer"
"yt-clone-video-processing/internal/dependency"
"yt-clone-video-processing/internal/handlers"
"yt-clone-video-processing/internal/initializations"
)

Expand All @@ -15,5 +17,10 @@ func main() {

initializations.RunMigrations(dependencies)

consumer.Consume(dependencies)
go consumer.Consume(dependencies)

h := handlers.Dependencies{DBConn: dependencies.DBConn}
if err := http.ListenAndServe(":8080", h.ApiHandler()); err != nil {
log.Println(err)
}
}
2 changes: 1 addition & 1 deletion go.mod
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
Expand Down
18 changes: 18 additions & 0 deletions internal/handlers/apiHandler.go
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
}
74 changes: 74 additions & 0 deletions internal/handlers/errorHandlers.go
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)
}

0 comments on commit af386d8

Please sign in to comment.