forked from reportportal/service-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (134 loc) · 3.96 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
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
145
146
147
148
package main
import (
"compress/flate"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/reportportal/commons-go/v5/commons"
"github.com/reportportal/commons-go/v5/conf"
"github.com/reportportal/commons-go/v5/server"
"github.com/unrolled/secure"
)
func main() {
currDir, e := os.Getwd()
if nil != e {
log.Fatalf("Cannot get workdir: %s", e.Error())
}
cfg := conf.EmptyConfig()
rpConf := struct {
Cfg *conf.ServerConfig
StaticsPath string `env:"RP_STATICS_PATH"`
}{
Cfg: cfg,
StaticsPath: currDir,
}
err := conf.LoadConfig(&rpConf)
if nil != err {
log.Fatalf("Cannot log app config")
}
info := commons.GetBuildInfo()
info.Name = "Service UI"
srv := server.New(rpConf.Cfg, info)
configureRouter(srv, rpConf)
srv.StartServer()
}
func configureRouter(srv *server.RpServer, rpConf struct {
Cfg *conf.ServerConfig
StaticsPath string `env:"RP_STATICS_PATH"`
}) {
srv.WithRouter(func(router *chi.Mux) {
// apply compression
compressor := middleware.NewCompressor(flate.DefaultCompression)
router.Use(compressor.Handler)
router.Use(middleware.Logger)
// content security policy
csp := map[string][]string{
"default-src": {"'self'", "data:", "'unsafe-inline'", "*.uservoice.com"},
"script-src": {
"'self'",
"'unsafe-inline'",
"'unsafe-eval'",
"status.reportportal.io",
"www.google-analytics.com",
"stats.g.doubleclick.net",
"*.saucelabs.com",
"*.epam.com",
"*.uservoice.com",
"*.rawgit.com",
},
"worker-src": {"'self'", "blob:"},
"font-src": {"'self'", "data:", "fonts.googleapis.com", "fonts.gstatic.com", "*.rawgit.com"},
"style-src-elem": {"'self'", "data:", "'unsafe-inline'", "*.googleapis.com", "*.rawgit.com"},
"style-src": {"'self'", "'unsafe-inline'", "https://tagmanager.google.com"},
"media-src": {"'self'", "*.saucelabs.com", "blob:"},
"img-src": {"*", "'self'", "'unsafe-inline'", "data:", "blob:", "http:", "https:", "www.google-analytics.com"},
"object-src": {"'self'"},
"connect-src": {"'self'", "https://www.google-analytics.com", "https://stats.g.doubleclick.net"},
}
// apply content security policies
var STSSeconds int64 = 315360000
router.Use(func(next http.Handler) http.Handler {
return secure.New(secure.Options{
ContentTypeNosniff: true,
BrowserXssFilter: true,
ContentSecurityPolicy: buildCSP(csp),
STSSeconds: STSSeconds,
STSIncludeSubdomains: true,
STSPreload: true,
}).Handler(next)
})
err := os.Chdir(rpConf.StaticsPath)
if nil != err {
log.Fatalf("Dir %s not found", rpConf.StaticsPath)
}
router.Handle("/*", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// trim query params
ext := filepath.Ext(trimQuery(r.URL.String(), "?"))
// never cache html
if "/" == r.URL.String() || ".html" == ext {
w.Header().Add("Cache-Control", "no-cache")
}
http.FileServer(http.Dir(rpConf.StaticsPath)).ServeHTTP(&redirectingRW{ResponseWriter: w, Request: r}, r)
}))
})
}
func trimQuery(s string, sep string) string {
sepIndex := strings.Index(s, sep)
if -1 != sepIndex {
return s[:sepIndex]
}
return s
}
func buildCSP(csp map[string][]string) string {
var instr []string
for k, v := range csp {
instr = append(instr, k+" "+strings.Join(v, " "))
}
return strings.Join(instr, "; ")
}
type redirectingRW struct {
*http.Request
http.ResponseWriter
ignore bool
}
func (hrw *redirectingRW) Header() http.Header {
return hrw.ResponseWriter.Header()
}
func (hrw *redirectingRW) WriteHeader(status int) {
if status == http.StatusNotFound {
hrw.ignore = true
http.Redirect(hrw.ResponseWriter, hrw.Request, "/ui/#notfound", http.StatusTemporaryRedirect)
} else {
hrw.ResponseWriter.WriteHeader(status)
}
}
func (hrw *redirectingRW) Write(p []byte) (int, error) {
if hrw.ignore {
return len(p), nil
}
return hrw.ResponseWriter.Write(p)
}