-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
239 lines (203 loc) · 5.38 KB
/
service.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package partial
import (
"context"
"html/template"
"io/fs"
"log/slog"
"net/http"
"sync"
"github.com/donseba/go-partial/connector"
)
type (
Logger interface {
Warn(msg string, args ...any)
Error(msg string, args ...any)
}
Config struct {
Connector connector.Connector
UseCache bool
FuncMap template.FuncMap
Logger Logger
fs fs.FS
}
Service struct {
config *Config
data map[string]any
combinedFunctions template.FuncMap
connector connector.Connector
funcMapLock sync.RWMutex // Add a read-write mutex
}
Layout struct {
service *Service
filesystem fs.FS
content *Partial
wrapper *Partial
data map[string]any
request *http.Request
combinedFunctions template.FuncMap
connector connector.Connector
funcMapLock sync.RWMutex // Add a read-write mutex
}
)
// NewService returns a new partial service.
func NewService(cfg *Config) *Service {
if cfg.FuncMap == nil {
cfg.FuncMap = DefaultTemplateFuncMap
}
if cfg.Logger == nil {
cfg.Logger = slog.Default().WithGroup("partial")
}
return &Service{
config: cfg,
data: make(map[string]any),
funcMapLock: sync.RWMutex{},
combinedFunctions: cfg.FuncMap,
connector: cfg.Connector,
}
}
// NewLayout returns a new layout.
func (svc *Service) NewLayout() *Layout {
return &Layout{
service: svc,
data: make(map[string]any),
filesystem: svc.config.fs,
connector: svc.connector,
combinedFunctions: svc.getFuncMap(),
}
}
// SetData sets the data for the Service.
func (svc *Service) SetData(data map[string]any) *Service {
svc.data = data
return svc
}
// AddData adds data to the Service.
func (svc *Service) AddData(key string, value any) *Service {
svc.data[key] = value
return svc
}
func (svc *Service) SetConnector(conn connector.Connector) *Service {
svc.connector = conn
return svc
}
// MergeFuncMap merges the given FuncMap with the existing FuncMap.
func (svc *Service) MergeFuncMap(funcMap template.FuncMap) {
svc.funcMapLock.Lock()
defer svc.funcMapLock.Unlock()
for k, v := range funcMap {
if _, ok := protectedFunctionNames[k]; ok {
svc.config.Logger.Warn("function name is protected and cannot be overwritten", "function", k)
continue
}
// Modify the existing map directly
svc.combinedFunctions[k] = v
}
}
func (svc *Service) getFuncMap() template.FuncMap {
svc.funcMapLock.RLock()
defer svc.funcMapLock.RUnlock()
return svc.combinedFunctions
}
// FS sets the filesystem for the Layout.
func (l *Layout) FS(fs fs.FS) *Layout {
l.filesystem = fs
return l
}
// Set sets the content for the layout.
func (l *Layout) Set(p *Partial) *Layout {
l.content = p
l.applyConfigToPartial(l.content)
return l
}
// Wrap sets the wrapper for the layout.
func (l *Layout) Wrap(p *Partial) *Layout {
l.wrapper = p
l.applyConfigToPartial(l.wrapper)
return l
}
// SetData sets the data for the layout.
func (l *Layout) SetData(data map[string]any) *Layout {
l.data = data
return l
}
// AddData adds data to the layout.
func (l *Layout) AddData(key string, value any) *Layout {
l.data[key] = value
return l
}
// MergeFuncMap merges the given FuncMap with the existing FuncMap in the Layout.
func (l *Layout) MergeFuncMap(funcMap template.FuncMap) {
l.funcMapLock.Lock()
defer l.funcMapLock.Unlock()
for k, v := range funcMap {
if _, ok := protectedFunctionNames[k]; ok {
l.service.config.Logger.Warn("function name is protected and cannot be overwritten", "function", k)
continue
}
// Modify the existing map directly
l.combinedFunctions[k] = v
}
}
func (l *Layout) getFuncMap() template.FuncMap {
l.funcMapLock.RLock()
defer l.funcMapLock.RUnlock()
return l.combinedFunctions
}
// RenderWithRequest renders the partial with the given http.Request.
func (l *Layout) RenderWithRequest(ctx context.Context, r *http.Request) (template.HTML, error) {
l.request = r
if l.wrapper != nil {
l.wrapper.With(l.content)
// Render the wrapper
return l.wrapper.RenderWithRequest(ctx, r)
} else {
// Render the content directly
return l.content.RenderWithRequest(ctx, r)
}
}
// WriteWithRequest writes the layout to the response writer.
func (l *Layout) WriteWithRequest(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
l.request = r
if l.connector.RenderPartial(r) {
if l.wrapper != nil {
l.content.parent = l.wrapper
}
err := l.content.WriteWithRequest(ctx, w, r)
if err != nil {
if l.service.config.Logger != nil {
l.service.config.Logger.Error("error rendering layout", "error", err)
}
return err
}
return nil
}
if l.wrapper != nil {
l.wrapper.With(l.content)
err := l.wrapper.WriteWithRequest(ctx, w, r)
if err != nil {
if l.service.config.Logger != nil {
l.service.config.Logger.Error("error rendering layout", "error", err)
}
return err
}
}
return nil
}
func (l *Layout) applyConfigToPartial(p *Partial) {
if p == nil {
return
}
// Combine functions only once
combinedFunctions := l.getFuncMap()
p.mergeFuncMapInternal(combinedFunctions)
p.connector = l.service.connector
if l.filesystem != nil {
p.fs = l.filesystem
}
if l.service.config.Logger != nil {
p.logger = l.service.config.Logger
}
p.useCache = l.service.config.UseCache
p.globalData = l.service.data
p.layoutData = l.data
p.request = l.request
}