Skip to content

Commit 6e6bcd0

Browse files
committed
feat(serverHandler): add in-middleware
1 parent c0d440c commit 6e6bcd0

File tree

6 files changed

+41
-28
lines changed

6 files changed

+41
-28
lines changed

src/middleware/context.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package middleware
22

33
import (
44
"mjpclab.dev/ghfs/src/serverLog"
5-
"os"
5+
"mjpclab.dev/ghfs/src/user"
66
)
77

88
type Context struct {
@@ -12,19 +12,22 @@ type Context struct {
1212
AliasFsPath string
1313
AliasFsRoot string
1414

15-
NeedAuth bool
16-
AuthUserName string
17-
AuthSuccess bool
15+
WantJson bool
1816

1917
RestrictAccess bool
2018
AllowAccess bool
2119

22-
WantJson bool
20+
NeedAuth bool
21+
AuthUserName string
22+
AuthSuccess bool
2323

24-
Status int
24+
CanUpload *bool
25+
CanMkdir *bool
26+
CanDelete *bool
27+
CanArchive *bool
2528

26-
Item os.FileInfo
27-
SubItems []os.FileInfo
29+
Status *int
2830

31+
Users *user.List
2932
Logger *serverLog.Logger
3033
}

src/param/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ type Param struct {
8888
ErrorLog string
8989

9090
PreMiddlewares []middleware.Middleware
91+
InMiddlewares []middleware.Middleware
9192
PostMiddlewares []middleware.Middleware
9293
}
9394

src/serverHandler/aliasHandler.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type aliasHandler struct {
3030
defaultSort string
3131
aliasPrefix string
3232

33-
users user.List
33+
users *user.List
3434
theme theme.Theme
3535
logger *serverLog.Logger
3636

@@ -79,6 +79,7 @@ type aliasHandler struct {
7979

8080
vary string
8181

82+
inMiddlewares []middleware.Middleware
8283
postMiddlewares []middleware.Middleware
8384
}
8485

@@ -109,8 +110,12 @@ func (h *aliasHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
109110
defer file.Close()
110111
}
111112

113+
if h.applyMiddlewares(h.inMiddlewares, w, r, data, fsPath) {
114+
return
115+
}
116+
112117
if !data.AllowAccess {
113-
if !h.postMiddleware(w, r, data, fsPath) {
118+
if !h.applyMiddlewares(h.postMiddlewares, w, r, data, fsPath) {
114119
h.accessRestricted(w, data.Status)
115120
}
116121
return
@@ -158,7 +163,7 @@ func (h *aliasHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
158163
}
159164
}
160165

161-
if h.postMiddleware(w, r, data, fsPath) {
166+
if h.applyMiddlewares(h.postMiddlewares, w, r, data, fsPath) {
162167
return
163168
}
164169

@@ -248,6 +253,7 @@ func newAliasHandler(
248253

249254
vary: vhostCtx.vary,
250255

256+
inMiddlewares: p.InMiddlewares,
251257
postMiddlewares: p.PostMiddlewares,
252258
}
253259
return h

src/serverHandler/middleware.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"net/http"
66
)
77

8-
func (h *aliasHandler) postMiddleware(w http.ResponseWriter, r *http.Request, data *responseData, fsPath string) (processed bool) {
9-
if len(h.postMiddlewares) == 0 {
10-
return false
8+
func (h *aliasHandler) applyMiddlewares(mids []middleware.Middleware, w http.ResponseWriter, r *http.Request, data *responseData, fsPath string) (processed bool) {
9+
if len(mids) == 0 {
10+
return
1111
}
1212

1313
context := &middleware.Context{
@@ -17,25 +17,28 @@ func (h *aliasHandler) postMiddleware(w http.ResponseWriter, r *http.Request, da
1717
AliasFsPath: fsPath,
1818
AliasFsRoot: h.root,
1919

20-
NeedAuth: data.NeedAuth,
21-
AuthUserName: data.AuthUserName,
22-
AuthSuccess: data.AuthSuccess,
20+
WantJson: data.wantJson,
2321

2422
RestrictAccess: data.RestrictAccess,
2523
AllowAccess: data.AllowAccess,
2624

27-
WantJson: data.wantJson,
25+
NeedAuth: data.NeedAuth,
26+
AuthUserName: data.AuthUserName,
27+
AuthSuccess: data.AuthSuccess,
2828

29-
Status: data.Status,
29+
CanUpload: &data.CanUpload,
30+
CanMkdir: &data.CanMkdir,
31+
CanDelete: &data.CanDelete,
32+
CanArchive: &data.CanArchive,
3033

31-
Item: data.Item,
32-
SubItems: data.SubItems,
34+
Status: &data.Status,
3335

36+
Users: h.users,
3437
Logger: h.logger,
3538
}
3639

37-
for i := range h.postMiddlewares {
38-
result := h.postMiddlewares[i](w, r, context)
40+
for i := range mids {
41+
result := mids[i](w, r, context)
3942
if result == middleware.Outputted {
4043
return true
4144
} else if result == middleware.SkipRests {

src/serverHandler/vhostHandler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
type vhostContext struct {
14-
users user.List
14+
users *user.List
1515
theme theme.Theme
1616
logger *serverLog.Logger
1717

@@ -89,7 +89,7 @@ func NewVhostHandler(
8989

9090
// alias param
9191
vhostCtx := &vhostContext{
92-
users: *users,
92+
users: users,
9393
theme: theme,
9494
logger: logger,
9595

src/user/list.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func (list *List) Len() int {
1414
return len(list.users)
1515
}
1616

17-
func (list *List) findIndex(username string) int {
17+
func (list *List) FindIndex(username string) int {
1818
for i := range list.users {
1919
if list.namesEqualFunc(list.users[i].getName(), username) {
2020
return i
@@ -25,7 +25,7 @@ func (list *List) findIndex(username string) int {
2525

2626
func (list *List) addUser(user user) error {
2727
username := user.getName()
28-
index := list.findIndex(username)
28+
index := list.FindIndex(username)
2929
if index < 0 {
3030
list.users = append(list.users, user)
3131
return nil
@@ -87,7 +87,7 @@ func (list *List) AddSha512(username, password string) error {
8787
}
8888

8989
func (list *List) Auth(username, password string) bool {
90-
index := list.findIndex(username)
90+
index := list.FindIndex(username)
9191
if index < 0 {
9292
return false
9393
}

0 commit comments

Comments
 (0)