forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
134 lines (115 loc) · 3.4 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
125
126
127
128
129
130
131
132
133
134
package index
import (
"compress/gzip"
"context"
"encoding/xml"
"html/template"
"net/http"
"strings"
"github.com/gorilla/mux"
"github.com/pydio/cells/common"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/common/log"
defaults "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{}
rolesConfigs := user.FlattenedRolesConfigs()
status := frontend.RequestStatus{
Config: config.Get(),
AclParameters: rolesConfigs.Val("parameters"),
AclActions: rolesConfigs.Val("actions"),
WsScopes: user.GetActiveScopes(),
User: user,
NoClaims: !user.Logged,
Lang: "en",
Request: r,
}
registry, e := pool.RegistryForStatus(ctx, status)
if e != nil {
w.WriteHeader(500)
return
}
bootConf, e := frontend.ComputeBootConf(pool)
if e != nil {
w.WriteHeader(500)
return
}
startParameters := map[string]interface{}{
"BOOTER_URL": "/frontend/bootconf",
"MAIN_ELEMENT": "ajxp_desktop",
"PRELOADED_BOOT_CONF": bootConf,
}
if regXml, e := xml.Marshal(registry); e == nil {
startParameters["PRELOADED_REGISTRY"] = string(regXml)
}
tplConf := &TplConf{
ApplicationTitle: config.Get("frontend", "plugin", "core.pydio", "APPLICATION_TITLE").Default("Cells").String(),
Rebase: "/",
ResourcesFolder: "plug/gui.ajax/res",
Favicon: "plug/gui.ajax/res/themes/common/images/favicon.png",
Theme: "material",
Version: frontend.VersionHash(),
LoadingString: GetLoadingString(bootConf.CurrentLanguage),
StartParameters: startParameters,
}
if customHeader := config.Get("frontend", "plugin", "gui.ajax", "HTML_CUSTOM_HEADER").String(); customHeader != "" {
tplConf.CustomHTMLHeader = template.HTML(customHeader)
}
tplConf = FilterTplConf(tplConf)
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() {
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.ServiceRestNamespace_ + common.ServiceFrontend); e == nil && len(s) > 0 {
h.frontendDetected = true
return true
}
log.Logger(context.Background()).Error("Frontend Service Not Detected")
return false
}