-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
111 lines (92 loc) · 2.69 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
database "github.com/vp-cap/data-lib/database"
config "github.com/vp-cap/video-service/config"
"github.com/julienschmidt/httprouter"
"github.com/rs/cors"
)
var (
configs config.Configurations
db database.Database = nil
)
type videoAndAdInfo struct {
Video database.Video `json:"video"`
Intervals map[string]database.Interval `json:"intervals"`
Ads []database.Advertisement `json:"ads"`
}
// handle video fetch all
func videoGetAll(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
videos, err := db.GetAllVideos(r.Context())
if err != nil {
fmt.Fprintf(w, "Unable to fetch video information")
return
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(videos)
log.Println("Sent all video information")
}
func getVideoAndAdInfo(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
video, err := db.GetVideo(r.Context(), ps.ByName("name"))
if err != nil {
fmt.Fprintf(w, "Unable to get video information")
return
}
log.Println("Fetched video")
videoInference, err := db.GetVideoInference(r.Context(), video.StorageLink)
if err != nil {
log.Println("No Video inference found", err)
} else {
log.Println("Fetched video inference")
}
keys := make([]string, 0, len(videoInference.TopFiveObjectsToInterval))
for k := range videoInference.TopFiveObjectsToInterval {
keys = append(keys, k)
}
ads, err := db.FindAdsWithObjects(r.Context(), keys)
if err != nil {
log.Println("No Ads found", err)
} else {
log.Println("Fetched relevant ads")
}
videoAndAdInfo, err := json.Marshal(&videoAndAdInfo{Video: video, Intervals: videoInference.TopFiveObjectsToInterval, Ads: ads})
if err != nil {
fmt.Fprintf(w, "Unable to marshal")
w.WriteHeader(http.StatusInternalServerError)
return
}
fmt.Fprintf(w, string(videoAndAdInfo))
log.Println("Sent video and relevant ad information")
}
func init() {
var err error
configs, err = config.GetConfigs()
if err != nil {
log.Fatal("Unable to get config")
}
}
func main() {
// Enable line numbers in logging
log.SetFlags(log.LstdFlags | log.Lshortfile )
ctx := context.Background()
var cancel context.CancelFunc
ctx, cancel = context.WithCancel(ctx)
defer cancel()
var err error
db, err = database.GetDatabaseClient(ctx, configs.Database)
if err != nil {
log.Fatalln(err)
}
log.Println("Connected to DB")
router := httprouter.New()
router.GET("/videos", videoGetAll)
router.GET("/videos/:name", getVideoAndAdInfo)
handler := cors.Default().Handler(router)
log.Println("Serving on", configs.Server.Port)
http.ListenAndServe(":" + configs.Server.Port, handler)
}