diff --git a/reader/cmd/main.go b/reader/cmd/main.go index ec34160..6bc02f3 100644 --- a/reader/cmd/main.go +++ b/reader/cmd/main.go @@ -8,23 +8,13 @@ import ( ) func main() { - path := "/home/psa/GoRepository/" port := 8080 http.HandleFunc("GET /download", handlers.Download) // example request: {"date": "07-03-2025", "start_time": "16-43", "end_time": "16-44"} - http.HandleFunc("GET /hls", handlers.HLS) - //http.Handle("/", addHeaders(http.FileServer(http.Dir(path)))) + http.HandleFunc("GET /hls/", handlers.HLS) log.Println("Starting server on:") - log.Printf("Serving %s on HTTP port: %d\n", path, port) + log.Printf("Serving on HTTP port: %d\n", port) log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil)) - } - -//func addHeaders(h http.Handler) http.Handler { -// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { -// w.Header().Set("Access-Control-Allow-Origin", "*") -// h.ServeHTTP(w, r) -// }) -//} diff --git a/reader/pkg/handlers/handlers.go b/reader/pkg/handlers/handlers.go index ed206ee..1d9f139 100644 --- a/reader/pkg/handlers/handlers.go +++ b/reader/pkg/handlers/handlers.go @@ -7,25 +7,26 @@ import ( "reader/internal/processor" ) -type DownloadRequest struct { +type VideoRequest struct { Date string `json:"date"` StartTime string `json:"start_time"` EndTime string `json:"end_time"` } +// Download processes Download request. func Download(w http.ResponseWriter, r *http.Request) { log.Printf("new download request: %+v\n", r) - dr := DownloadRequest{} + downloadRequest := VideoRequest{} - err := json.NewDecoder(r.Body).Decode(&dr) + err := json.NewDecoder(r.Body).Decode(&downloadRequest) if err != nil { log.Printf("json decode error: %v\n", err) w.WriteHeader(http.StatusBadRequest) return } - pathFileNameRes, err := processor.Process(dr.Date, dr.StartTime, dr.EndTime) + pathFileNameRes, err := processor.Process(downloadRequest.Date, downloadRequest.StartTime, downloadRequest.EndTime) if err != nil { log.Printf("process error: %v\n", err) w.WriteHeader(http.StatusBadRequest) @@ -39,7 +40,12 @@ func Download(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, pathFileNameRes) } +// HLS processes Download request. func HLS(w http.ResponseWriter, r *http.Request) { log.Printf("new hls request: %+v\n", r) + path := "/home/psa/GoRepository/data/1280x720/" + + w.Header().Set("Access-Control-Allow-Origin", "*") + http.StripPrefix("/hls", http.FileServer(http.Dir(path))).ServeHTTP(w, r) }