-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
executable file
·95 lines (88 loc) · 2.51 KB
/
utils.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
package mongersvast
import (
"crypto/md5"
"fmt"
"io"
"math/rand"
"net/http"
"strings"
"time"
)
//NewVAST get instance on VAST object
func NewVAST(version string) *VAST {
//minimal config
v := &VAST{}
//good ;-)
return v.SetVersion(version)
}
//InLineAd inline ad template
func InLineAd(attrs AdAttributes, adSystem *AdSystem, title *AdTitle, desc *Description, verr *VASTError, imps []*Impression, creatives *Creatives) (req *VAST) {
//minimal config
req = &VAST{
Version: VastXMLVer2,
Ad: []*Ad{
{InLine: &InLine{
ID: fmtAdUUID("1"),
InLineWrapperData: InLineWrapperData{
AdSystem: adSystem,
AdTitle: title,
Description: desc,
Error: verr,
Impression: imps,
Creatives: creatives,
},
},
},
},
}
//options
req.FormatAdAttrs(attrs)
return
}
//WrapperAd wrapper ad template
func WrapperAd(attrs AdAttributes, adSystem *AdSystem, title *AdTitle, desc *Description, verr *VASTError, imps []*Impression, creatives *Creatives, adURI *VASTAdTagURI) (req *VAST) {
//minimal config
req = &VAST{
Version: VastXMLVer2,
Ad: []*Ad{
{Wrapper: &Wrapper{
ID: fmtAdUUID("2"),
InLineWrapperData: InLineWrapperData{
AdSystem: adSystem,
AdTitle: title,
Description: desc,
Error: verr,
Impression: imps,
Creatives: creatives,
VASTAdTagURI: adURI,
},
},
},
},
}
//options
req.FormatAdAttrs(attrs)
return
}
//fmtAdUUID make temp str
func fmtAdUUID(pfx string) string {
return fmt.Sprintf("%x", md5.Sum([]byte("070704"+strings.TrimSpace(fmt.Sprintf("%s%05x%10x", pfx, rand.Intn(99999), time.Now().UTC().UnixNano())))))
}
//SetXMLHeaders set the xml headers simply
func SetXMLHeaders(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/xml")
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") // HTTP 1.1.
w.Header().Set("Pragma", "no-cache") // HTTP 1.0.
w.Header().Set("Expires", "0")
w.Header().Set("Access-Control-Allow-Origin", "*") //Google HTML5 SDK CORS Header
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Headers", "*")
w.Header().Set("Access-Control-Allow-Methods", "GET")
w.Header().Set("Access-Control-Max-Age", "10080")
}
//PushXML push content with proper xml hdrs
func PushXML(w http.ResponseWriter, xml string) {
//just in case ;-)
SetXMLHeaders(w)
io.WriteString(w, xml)
}