-
Notifications
You must be signed in to change notification settings - Fork 0
/
mux.go
63 lines (54 loc) · 1.51 KB
/
mux.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package portal
import (
"net/http"
"regexp"
)
type fileSystemPathable struct {
fs http.FileSystem
re *regexp.Regexp
repl []byte
}
func newFileSystemPathable(fs http.FileSystem, pattern string, repl string) (*fileSystemPathable, error) {
re, err := regexp.Compile(pattern)
if err != nil {
return nil, err
}
fsP := fileSystemPathable{fs: fs, re: re, repl: []byte(repl)}
return &fsP, nil
}
func (fsP *fileSystemPathable) Open(name string) (http.File, error) {
newName := string(fsP.re.ReplaceAll([]byte(name), fsP.repl))
return fsP.fs.Open(newName)
}
// Mux wrapper that supprts grpc Mux and http Mux for static files as
// well on same port
type serveMux struct {
staticMux http.Handler
apiMux http.Handler
fsP *fileSystemPathable
}
func newServeMux(
staticFs http.FileSystem,
staticPattern string,
staticReplace string,
apiMux http.Handler,
) (*serveMux, error) {
fsP, err := newFileSystemPathable(staticFs, staticPattern, staticReplace)
if err != nil {
return nil, err
}
staticMux := http.NewServeMux()
staticMux.Handle("/", http.FileServer(fsP))
mux := serveMux{staticMux, apiMux, fsP}
return &mux, nil
}
func (mux *serveMux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if mux.fsP.re.MatchString(r.URL.Path) {
mux.staticMux.ServeHTTP(w, r)
} else {
mux.apiMux.ServeHTTP(w, r)
}
}