-
Notifications
You must be signed in to change notification settings - Fork 180
/
handler.go
124 lines (106 loc) · 3.13 KB
/
handler.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
package index
import (
"compress/gzip"
"context"
"html/template"
"net/http"
"strings"
"github.com/gorilla/mux"
"encoding/xml"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/micro"
"github.com/pydio/cells/common/service/frontend"
)
type IndexHandler struct {
tpl *template.Template
loadingTpl *template.Template
frontendDetected bool
}
func NewIndexHandler() *IndexHandler {
h := &IndexHandler{}
h.tpl, _ = template.New("index").Parse(page)
h.loadingTpl, _ = template.New("loading").Parse(loading)
return h
}
func (h *IndexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
pool, e := frontend.GetPluginsPool()
if e != nil {
w.WriteHeader(500)
return
}
// Try to precompute registry
ctx := r.Context()
user := &frontend.User{}
cfg := config.Default()
rolesConfigs := user.FlattenedRolesConfigs()
status := frontend.RequestStatus{
Config: cfg,
AclParameters: rolesConfigs.Get("parameters").(*config.Map),
AclActions: rolesConfigs.Get("actions").(*config.Map),
WsScopes: user.GetActiveScopes(),
User: user,
NoClaims: !user.Logged,
Lang: "en",
Request: r,
}
registry := pool.RegistryForStatus(ctx, status)
bootConf := frontend.ComputeBootConf(pool)
url := config.Get("defaults", "url").String("")
startParameters := map[string]interface{}{
"BOOTER_URL": "/frontend/bootconf",
"MAIN_ELEMENT": "ajxp_desktop",
"REBASE": url,
"PRELOADED_BOOT_CONF": bootConf,
}
if regXml, e := xml.Marshal(registry); e == nil {
startParameters["PRELOADED_REGISTRY"] = string(regXml)
}
tplConf := &TplConf{
ApplicationTitle: "Pydio",
Rebase: url,
ResourcesFolder: "plug/gui.ajax/res",
Theme: "material",
Version: common.Version().String(),
Debug: config.Get("frontend", "debug").Bool(false),
LoadingString: GetLoadingString(bootConf.CurrentLanguage),
StartParameters: startParameters,
}
vars := mux.Vars(r)
if reset, ok := vars["resetPasswordKey"]; ok {
tplConf.StartParameters["USER_GUI_ACTION"] = "reset-password"
tplConf.StartParameters["USER_ACTION_KEY"] = reset
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
for hK, hV := range config.Get("frontend", "secureHeaders").StringMap(map[string]string{}) {
w.Header().Set(hK, hV)
}
var tpl *template.Template
if !h.detectFrontendService() {
tpl = h.loadingTpl
} else {
tpl = h.tpl
}
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
out := gzip.NewWriter(w)
defer out.Close()
w.Header().Set("Content-Encoding", "gzip")
w.WriteHeader(200)
tpl.Execute(out, tplConf)
} else {
w.WriteHeader(200)
tpl.Execute(w, tplConf)
}
}
func (h *IndexHandler) detectFrontendService() bool {
if h.frontendDetected {
return true
}
if s, e := defaults.Registry().GetService(common.SERVICE_REST_NAMESPACE_ + common.SERVICE_FRONTEND); e == nil && len(s) > 0 {
h.frontendDetected = true
return true
}
log.Logger(context.Background()).Error("Frontend Service Not Detected")
return false
}