Added handlers for Download and HLS requests.

This commit is contained in:
Сергей Петров 2025-03-11 12:36:49 +05:00
parent 333a51f633
commit ff2b71b935
2 changed files with 12 additions and 16 deletions

View File

@ -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)
// })
//}

View File

@ -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)
}