-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.go
181 lines (153 loc) · 3.93 KB
/
view.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
package cms
import (
"context"
"net/http"
"go.sancus.dev/web"
"go.sancus.dev/web/errors"
)
type View interface {
http.Handler
web.Handler
web.RouterPageInfo
Middleware(prefix string) func(next http.Handler) http.Handler
Config() *ViewConfig
}
type ViewConfig struct {
GetRoutePath func(r *http.Request) string
GetUser func(ctx context.Context) User
SetResource func(ctx context.Context, res Resource) context.Context
GetResource func(ctx context.Context) Resource
SetDirectory func(ctx context.Context, res Directory) context.Context
GetDirectory func(ctx context.Context) Directory
Edit string // per resource
EditHandler web.HandlerFunc
Files string // per directory
FilesHandler web.HandlerFunc
Ping string // optional per view
PingHandler web.HandlerFunc
Sitemap string // optional per view
SitemapHandler web.HandlerFunc
ErrorHandler web.ErrorHandlerFunc
Index string // default page
ReadOnly bool // storage can't be modified through this View
}
func (c *ViewConfig) SetGetRoutePath(f func(*http.Request) string) error {
if f == nil {
f = DefaultGetRoutePath
}
c.GetRoutePath = f
return nil
}
func (c *ViewConfig) SetGetUser(f func(ctx context.Context) User) error {
if f == nil {
f = DefaultGetUser
}
c.GetUser = f
return nil
}
// Put Resource into context.Context
func (c *ViewConfig) SetSetResource(f func(ctx context.Context, res Resource) context.Context) error {
if f == nil {
f = DefaultSetResource
}
c.SetResource = f
return nil
}
// Get Resource from context.Context
func (c *ViewConfig) SetGetResource(f func(ctx context.Context) Resource) error {
if f == nil {
f = DefaultGetResource
}
c.GetResource = f
return nil
}
// Put Directory into context.Context
func (c *ViewConfig) SetSetDirectory(f func(ctx context.Context, res Directory) context.Context) error {
if f == nil {
f = DefaultSetDirectory
}
c.SetDirectory = f
return nil
}
// Get Directory from context.Context
func (c *ViewConfig) SetGetDirectory(f func(ctx context.Context) Directory) error {
if f == nil {
f = DefaultGetDirectory
}
c.GetDirectory = f
return nil
}
// Set View's File Editor
func (c *ViewConfig) SetEditHandler(path string, handler web.HandlerFunc) error {
if path == "" || handler == nil {
path = ""
handler = nil
} else if path[0] != '/' {
path = "/" + path
}
c.Edit = path
c.EditHandler = handler
return nil
}
// Set View's File Manager
func (c *ViewConfig) SetFilesHandler(path string, handler web.HandlerFunc) error {
if path == "" || handler == nil {
path = ""
handler = nil
} else if path[0] != '/' {
path = "/" + path
}
c.Files = path
c.FilesHandler = handler
return nil
}
// Set View's Ping Handler
func (c *ViewConfig) SetPingHandler(path string, handler web.HandlerFunc) error {
if path == "" || handler == nil {
path = ""
handler = nil
} else if path[0] != '/' {
path = "/" + path
}
c.Ping = path
c.PingHandler = handler
return nil
}
// Set View's Sitemap Handler
func (c *ViewConfig) SetSitemapHandler(path string, handler web.HandlerFunc) error {
if path == "" || handler == nil {
path = ""
handler = nil
} else if path[0] != '/' {
path = "/" + path
}
c.Sitemap = path
c.SitemapHandler = handler
return nil
}
func (c *ViewConfig) SetErrorHandler(f web.ErrorHandlerFunc) error {
if f == nil {
f = errors.HandleError
}
c.ErrorHandler = f
return nil
}
func (c *ViewConfig) SetIndexPage(index string) error {
c.Index = index
return nil
}
// Defaults
func (c *ViewConfig) SetDefaults() error {
c.SetGetRoutePath(c.GetRoutePath)
c.SetGetUser(c.GetUser)
c.SetSetResource(c.SetResource)
c.SetGetResource(c.GetResource)
c.SetSetDirectory(c.SetDirectory)
c.SetGetDirectory(c.GetDirectory)
c.SetEditHandler(c.Edit, c.EditHandler)
c.SetFilesHandler(c.Files, c.FilesHandler)
c.SetPingHandler(c.Ping, c.PingHandler)
c.SetSitemapHandler(c.Sitemap, c.SitemapHandler)
c.SetErrorHandler(c.ErrorHandler)
return nil
}