forked from vfarcic/cncf-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo.go
144 lines (132 loc) · 3.49 KB
/
video.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"crypto/tls"
"errors"
"log"
"net/http"
"os"
"github.com/go-pg/pg/extra/pgotel/v10"
"github.com/go-pg/pg/v10"
"github.com/gin-gonic/gin"
)
var dbSession *pg.DB = nil
type Video struct {
ID string `json:"id"`
Title string `json:"title"`
}
func videosGetHandler(ctx *gin.Context) {
traceContext, span := tp.Tracer(serviceName).Start(ctx, "video-get")
defer func() { span.End() }()
span.AddEvent("Authorizing the user")
allowed, err := fgaCheck(ctx.Query("user"), "reader")
if err != nil {
httpErrorInternalServerError(err, span, ctx)
return
}
if !allowed {
httpStatusUnauthorized(errors.New("not allowed"), span, ctx)
return
}
span.AddEvent("Establishing connection to the database")
db := getDB(ctx)
if db == nil {
return
}
var videos []Video
err = db.ModelContext(traceContext, &videos).Select()
if err != nil {
httpErrorInternalServerError(err, span, ctx)
return
}
ctx.JSON(http.StatusOK, videos)
}
func videoPostHandler(ctx *gin.Context) {
traceContext, span := tp.Tracer(serviceName).Start(ctx, "video-post")
defer func() { span.End() }()
span.AddEvent("Authorizing the user")
allowed, err := fgaCheck(ctx.Query("user"), "writer")
if err != nil {
httpErrorInternalServerError(err, span, ctx)
return
}
if !allowed {
httpStatusUnauthorized(errors.New("not allowed"), span, ctx)
return
}
span.AddEvent("Establishing connection to the database...")
db := getDB(ctx)
if db == nil {
return
}
span.AddEvent("Retrieving values...")
id := ctx.Query("id")
if len(id) == 0 {
httpErrorBadRequest(errors.New("id is empty"), span, ctx)
return
}
title := ctx.Query("title")
if len(title) == 0 {
httpErrorBadRequest(errors.New("title is empty"), span, ctx)
return
}
span.AddEvent("Retrieving data from the database...")
video := &Video{
ID: id,
Title: title,
}
_, err = db.ModelContext(traceContext, video).Insert()
if err != nil {
httpErrorInternalServerError(err, span, ctx)
return
}
}
func getDB(c *gin.Context) *pg.DB {
if dbSession != nil {
return dbSession
}
endpoint := os.Getenv("DB_ENDPOINT")
if len(endpoint) == 0 {
log.Println("Environment variable `DB_ENDPOINT` is empty")
c.String(http.StatusBadRequest, "Environment variable `DB_ENDPOINT` is empty")
return nil
}
port := os.Getenv("DB_PORT")
if len(port) == 0 {
log.Println("Environment variable `DB_PORT` is empty")
c.String(http.StatusBadRequest, "Environment variable `DB_PORT` is empty")
return nil
}
user := os.Getenv("DB_USER")
if len(user) == 0 {
user = os.Getenv("DB_USERNAME")
if len(user) == 0 {
log.Println("Environment variables `DB_USER` and `DB_USERNAME` are empty")
c.String(http.StatusBadRequest, "Environment variables `DB_USER` and `DB_USERNAME` are empty")
return nil
}
}
pass := os.Getenv("DB_PASS")
if len(pass) == 0 {
pass = os.Getenv("DB_PASSWORD")
if len(pass) == 0 {
log.Println("Environment variables `DB_PASS` and `DB_PASSWORD are empty")
c.String(http.StatusBadRequest, "Environment variables `DB_PASS` and `DB_PASSWORD are empty")
return nil
}
}
name := os.Getenv("DB_NAME")
if len(name) == 0 {
log.Println("Environment variable `DB_NAME` is empty")
c.String(http.StatusBadRequest, "Environment variable `DB_NAME` is empty")
return nil
}
dbSession := pg.Connect(&pg.Options{
Addr: endpoint + ":" + port,
User: user,
Password: pass,
Database: name,
TLSConfig: &tls.Config{InsecureSkipVerify: true},
})
dbSession.AddQueryHook(pgotel.NewTracingHook())
return dbSession
}