/
website.go
190 lines (155 loc) 路 4.32 KB
/
website.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
package website
import (
"archive/zip"
"context"
"errors"
"fmt"
"io"
goHttp "net/http"
"path"
"strings"
"time"
"github.com/spf13/afero/zipfs"
"github.com/taubyte/go-interfaces/services/substrate/components"
httpComp "github.com/taubyte/go-interfaces/services/substrate/components/http"
matcherSpec "github.com/taubyte/go-specs/matcher"
http "github.com/taubyte/http"
"github.com/taubyte/tau/protocols/substrate/components/http/common"
"github.com/taubyte/tau/protocols/substrate/components/metrics"
"go4.org/readerutil"
)
func (w *Website) Provision() (web httpComp.Serviceable, err error) {
w.instanceCtx, w.instanceCtxC = context.WithCancel(w.srv.Context())
w.readyCtx, w.readyCtxC = context.WithCancel(w.srv.Context())
defer func() {
w.readyDone = true
w.readyError = err
w.readyCtxC()
}()
cachedWeb, err := w.srv.Cache().Add(w, w.branch)
if err != nil {
return nil, fmt.Errorf("adding website to cache failed with: %w", err)
}
if w != cachedWeb {
_w, ok := cachedWeb.(httpComp.Website)
if ok {
return _w, nil
}
// TODO: Debug Logger if this case is met
}
if err = w.getAsset(); err != nil {
return nil, fmt.Errorf("getting website `%s`assets failed with: %w", w.config.Name, err)
}
w.metrics.Cached = 1
w.provisioned = true
return w, nil
}
func (w *Website) Metrics() *metrics.Website {
return &w.metrics
}
func (w *Website) Handle(_w goHttp.ResponseWriter, r *goHttp.Request, matcher components.MatchDefinition) (t time.Time, err error) {
_matcher, ok := matcher.(*common.MatchDefinition)
if !ok {
return t, errors.New("invalid match definition")
}
pathMatch := _matcher.Get(common.PathMatch)
_path := path.Clean("/" + strings.TrimPrefix(r.URL.Path, pathMatch))
if strings.HasSuffix(r.URL.Path, "/") {
_path += "/"
}
r.URL.Path = _path
err = w.srv.Http().LowLevelAssetHandler(&http.HeadlessAssetsDefinition{
FileSystem: w.root,
SinglePageApplication: true,
Directory: "/",
}, _w, r)
return time.Now(), err
}
func (w *Website) Validate(matcher components.MatchDefinition) error {
if w.Match(matcher) == matcherSpec.NoMatch {
return errors.New("no match")
}
return nil
}
func (w *Website) getAsset() error {
dagReader, err := w.srv.Node().GetFile(w.srv.Context(), w.assetId)
if err != nil {
return fmt.Errorf("getting build zip failed with: %w", err)
}
size, _ := readerutil.Size(dagReader)
if _, err = dagReader.Seek(0, io.SeekStart); err != nil {
return fmt.Errorf("seek start of build zip failed with: %w", err)
}
zipReader, err := zip.NewReader(
readerutil.NewBufferingReaderAt(dagReader),
size,
)
if err != nil {
return fmt.Errorf("reading build zip failed with: %w", err)
}
var computedPaths []string
for _, file := range zipReader.File {
if !file.FileInfo().IsDir() {
computedPaths = append(computedPaths, file.Name)
}
}
w.computedPaths[w.matcher.Path] = computedPaths
w.root = zipfs.New(zipReader)
dagReader.Close()
return nil
}
func (w *Website) Match(matcher components.MatchDefinition) (currentMatchIndex matcherSpec.Index) {
currentMatch := matcherSpec.NoMatch
var pathMatch string
_matcher, ok := matcher.(*common.MatchDefinition)
if !ok {
return
}
for _, path := range w.config.Paths {
switch _matcher.Method {
case "", "GET", "HEAD":
matchValue := pathContains(path, _matcher.Path)
if matchValue > currentMatch {
pathMatch = path
currentMatch = matchValue
}
}
}
if currentMatch >= matcherSpec.MinMatch && currentMatch < matcherSpec.HighMatch {
computedPaths, ok := w.computedPaths[pathMatch]
if ok {
for _, _path := range computedPaths {
if path.Join(pathMatch, _path) == _matcher.Path {
currentMatch = matcherSpec.HighMatch
}
}
}
}
_matcher.Set(common.PathMatch, pathMatch)
return currentMatch
}
func pathContains(path, requestPath string) matcherSpec.Index {
pathLen := len(path)
reqLen := len(requestPath)
if pathLen == reqLen && path == requestPath {
return matcherSpec.HighMatch
}
if pathLen == 1 && path == "/" {
return matcherSpec.MinMatch
}
if reqLen < pathLen {
return matcherSpec.NoMatch
}
var score = matcherSpec.DefaultMatch
// we re add the "/" at the end of path
path += "/"
pathLen++
var i int
for i = 0; i < pathLen; i++ {
if requestPath[i] != path[i] {
return matcherSpec.NoMatch
}
}
score += matcherSpec.Index(i)
return score
}