Skip to content

Commit

Permalink
fix(gnoweb): simplify url parsing system (#3366)
Browse files Browse the repository at this point in the history
Co-authored-by: Morgan <[email protected]>
  • Loading branch information
gfanton and thehowl authored Jan 8, 2025
1 parent c6c0e6e commit 6472eea
Show file tree
Hide file tree
Showing 4 changed files with 468 additions and 126 deletions.
4 changes: 3 additions & 1 deletion gno.land/pkg/gnoweb/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func TestRoutes(t *testing.T) {

for _, r := range routes {
t.Run(fmt.Sprintf("test route %s", r.route), func(t *testing.T) {
t.Logf("input: %q", r.route)
request := httptest.NewRequest(http.MethodGet, r.route, nil)
response := httptest.NewRecorder()
router.ServeHTTP(response, request)
Expand Down Expand Up @@ -125,7 +126,7 @@ func TestAnalytics(t *testing.T) {
request := httptest.NewRequest(http.MethodGet, route, nil)
response := httptest.NewRecorder()
router.ServeHTTP(response, request)
fmt.Println("HELLO:", response.Body.String())

assert.Contains(t, response.Body.String(), "sa.gno.services")
})
}
Expand All @@ -143,6 +144,7 @@ func TestAnalytics(t *testing.T) {
request := httptest.NewRequest(http.MethodGet, route, nil)
response := httptest.NewRecorder()
router.ServeHTTP(response, request)

assert.NotContains(t, response.Body.String(), "sa.gno.services")
})
}
Expand Down
48 changes: 14 additions & 34 deletions gno.land/pkg/gnoweb/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ func (h *WebHandler) Get(w http.ResponseWriter, r *http.Request) {
indexData.HeaderData.WebQuery = gnourl.WebQuery

// Render
switch gnourl.Kind() {
case KindRealm, KindPure:
switch {
case gnourl.IsRealm(), gnourl.IsPure():
status, err = h.renderPackage(&body, gnourl)
default:
h.logger.Debug("invalid page kind", "kind", gnourl.Kind)
h.logger.Debug("invalid path: path is neither a pure package or a realm")
status, err = http.StatusNotFound, components.RenderStatusComponent(&body, "page not found")
}
}
Expand All @@ -129,37 +129,20 @@ func (h *WebHandler) Get(w http.ResponseWriter, r *http.Request) {
func (h *WebHandler) renderPackage(w io.Writer, gnourl *GnoURL) (status int, err error) {
h.logger.Info("component render", "path", gnourl.Path, "args", gnourl.Args)

kind := gnourl.Kind()

// Display realm help page?
if kind == KindRealm && gnourl.WebQuery.Has("help") {
if gnourl.WebQuery.Has("help") {
return h.renderRealmHelp(w, gnourl)
}

// Display package source page?
switch {
case gnourl.WebQuery.Has("source"):
return h.renderRealmSource(w, gnourl)
case kind == KindPure,
strings.HasSuffix(gnourl.Path, "/"),
isFile(gnourl.Path):
i := strings.LastIndexByte(gnourl.Path, '/')
if i < 0 {
return http.StatusInternalServerError, fmt.Errorf("unable to get ending slash for %q", gnourl.Path)
}

case gnourl.IsFile():
// Fill webquery with file infos
gnourl.WebQuery.Set("source", "") // set source

file := gnourl.Path[i+1:]
if file == "" {
return h.renderRealmDirectory(w, gnourl)
}

gnourl.WebQuery.Set("file", file)
gnourl.Path = gnourl.Path[:i]

return h.renderRealmSource(w, gnourl)
case gnourl.IsDir(), gnourl.IsPure():
return h.renderRealmDirectory(w, gnourl)
}

// Render content into the content buffer
Expand Down Expand Up @@ -250,12 +233,16 @@ func (h *WebHandler) renderRealmSource(w io.Writer, gnourl *GnoURL) (status int,
return http.StatusOK, components.RenderStatusComponent(w, "no files available")
}

file := gnourl.WebQuery.Get("file") // webquery override file
if file == "" {
file = gnourl.File
}

var fileName string
file := gnourl.WebQuery.Get("file")
if file == "" {
fileName = files[0]
fileName = files[0] // Default to the first file if none specified
} else if slices.Contains(files, file) {
fileName = file
fileName = file // Use specified file if it exists
} else {
h.logger.Error("unable to render source", "file", file, "err", "file does not exist")
return http.StatusInternalServerError, components.RenderStatusComponent(w, "internal error")
Expand Down Expand Up @@ -370,10 +357,3 @@ func generateBreadcrumbPaths(path string) []components.BreadcrumbPart {

return parts
}

// IsFile checks if the last element of the path is a file (has an extension)
func isFile(path string) bool {
base := filepath.Base(path)
ext := filepath.Ext(base)
return ext != ""
}
Loading

0 comments on commit 6472eea

Please sign in to comment.