-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
76 lines (64 loc) · 2.16 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/google/uuid"
"github.com/julienschmidt/httprouter"
"go.opentelemetry.io/otel/attribute"
)
func HealthzHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
tr := traceProvider.Tracer("playlist-ms-main-component")
id := uuid.New()
ip := strings.Split(r.RemoteAddr, ":")[0]
_, span := tr.Start(context.Background(), "healthz")
span.SetAttributes(attribute.Key("Protocol").String(r.Proto))
span.SetAttributes(attribute.Key("UUID").String(id.String()))
span.SetAttributes(attribute.Key("Client IP").String(ip))
defer span.End()
Sugar.Infof("client_ip: %v", ip)
Sugar.Infof("request_id: %v", id.String())
fmt.Fprintf(w, "ok!")
}
func GetPlaylistsHandler(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
tr := traceProvider.Tracer("playlist-ms-main-component")
ctx, span := tr.Start(context.Background(), "GET Playlist HTTP Handler")
id := uuid.New()
ip := strings.Split(r.RemoteAddr, ":")[0]
Sugar.Infof("client_ip: %v", ip)
Sugar.Infof("request_id: %v", id.String())
defer span.End()
span.SetAttributes(attribute.Key("Function").String("GetPlaylistHandler"))
span.SetAttributes(attribute.Key("Protocol").String(r.Proto))
span.SetAttributes(attribute.Key("UUID").String(id.String()))
span.SetAttributes(attribute.Key("Client IP").String(strings.Split(r.RemoteAddr, ":")[0]))
Cors(w)
playlistsJson := GetPlaylists(ctx, id, ip)
var playlists []Playlist
err := json.Unmarshal([]byte(playlistsJson), &playlists)
if err != nil {
Sugar.Errorf("Error while unmarshalling JSON file: %v\n", err)
return
}
PlaylistsMetrics(playlists)
//get videos for each playlist from videos api
for pi := range playlists {
vs := GetVideosOfPlaylists(playlists[pi], ctx, id, ip)
playlists[pi].Videos = vs
}
playlistsBytes, err := json.Marshal(playlists)
if err != nil {
Sugar.Errorf("Error while marshalling JSON file: %v\n", err)
return
}
reader := bytes.NewReader(playlistsBytes)
if b, err := ioutil.ReadAll(reader); err == nil {
fmt.Fprintf(w, "%s", string(b))
} else {
Sugar.Errorf("Error while reading data: %v\n", err)
}
}