This repository has been archived by the owner on Feb 26, 2023. It is now read-only.
/
extended_http.go
111 lines (96 loc) · 2.28 KB
/
extended_http.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
package servers
import (
"net/http"
"github.com/pojntfx/bofied/pkg/authorization"
"github.com/pojntfx/bofied/pkg/constants"
"github.com/pojntfx/bofied/pkg/eventing"
"github.com/pojntfx/liwasc/pkg/validators"
"github.com/rs/cors"
"golang.org/x/net/webdav"
)
const (
WebDAVRealmDescription = `bofied protected area. You can find your credentials (username and password/token) with the "Mount Folder" option in the frontend.`
HTTPPrefix = "/public"
WebDAVPrefix = "/private"
GRPCPrefix = "/grpc"
)
type ExtendedHTTPServer struct {
FileServer
eventsServerHandler http.Handler
eventHandler *eventing.EventHandler
oidcValidator *validators.OIDCValidator
}
func NewExtendedHTTPServer(
workingDir string,
listenAddress string,
oidcValidator *validators.OIDCValidator,
eventsServerHandler http.Handler,
eventHandler *eventing.EventHandler,
) *ExtendedHTTPServer {
return &ExtendedHTTPServer{
FileServer: FileServer{
workingDir: workingDir,
listenAddress: listenAddress,
},
eventsServerHandler: eventsServerHandler,
eventHandler: eventHandler,
oidcValidator: oidcValidator,
}
}
func (s *ExtendedHTTPServer) GetWebDAVHandler(prefix string) webdav.Handler {
return webdav.Handler{
Prefix: prefix,
FileSystem: webdav.Dir(s.workingDir),
LockSystem: webdav.NewMemLS(),
}
}
func (s *ExtendedHTTPServer) GetHTTPHandler() http.Handler {
return eventing.LogRequestHandler(
http.FileServer(
http.Dir(s.workingDir),
),
s.eventHandler,
)
}
func (s *ExtendedHTTPServer) ListenAndServe() error {
webDAVHandler := s.GetWebDAVHandler(WebDAVPrefix)
httpHandler := s.GetHTTPHandler()
mux := http.NewServeMux()
mux.Handle(
HTTPPrefix+"/",
http.StripPrefix(HTTPPrefix, httpHandler),
)
mux.Handle(
WebDAVPrefix+"/",
cors.New(cors.Options{
AllowedMethods: []string{
"GET",
"PUT",
"PROPFIND",
"MKCOL",
"MOVE",
"COPY",
"DELETE",
},
AllowCredentials: true,
AllowedHeaders: []string{
"*",
},
}).Handler(
authorization.OIDCOverBasicAuth(
&webDAVHandler,
constants.OIDCOverBasicAuthUsername,
s.oidcValidator,
WebDAVRealmDescription,
),
),
)
mux.Handle(
GRPCPrefix,
s.eventsServerHandler,
)
return http.ListenAndServe(
s.listenAddress,
mux,
)
}