-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers.go
60 lines (49 loc) · 1.43 KB
/
handlers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"gopkg.in/vansante/go-ffprobe.v2"
)
type InputPayload struct {
Link string `json:"link"`
}
type OutputPayload struct {
Filename string `json:"filename"`
Streams []*ffprobe.Stream `json:"streams"`
}
func (app *application) metadataHandler(w http.ResponseWriter, r *http.Request) {
var payload InputPayload
err := json.NewDecoder(r.Body).Decode(&payload)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
data, err := ffprobe.ProbeURL(context.TODO(), payload.Link)
if err != nil {
app.logger.Error(fmt.Sprintf("Error getting data: %v", err))
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
parsedData := OutputPayload{
Filename: data.Format.Filename,
Streams: data.Streams,
}
output, _ := json.Marshal(parsedData)
w.Header().Add("Content-Type", "application/json")
_, err = w.Write(output)
if err != nil {
app.logger.Error(err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}
func (app *application) healthCheckHandler(w http.ResponseWriter, r *http.Request) {
_, err := w.Write([]byte("okay!"))
if err != nil {
app.logger.Error(err.Error())
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
}