Skip to content

Commit

Permalink
some renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
thehowl committed Dec 9, 2024
1 parent bc52d9b commit d97f08f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 30 deletions.
4 changes: 2 additions & 2 deletions contribs/gnodev/cmd/gnodev/setup_web.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ func setupGnoWebServer(logger *slog.Logger, cfg *devCfg, dnode *gnodev.Node) (ht

appcfg := gnoweb.NewDefaultAppConfig()
appcfg.UnsafeHTML = cfg.webHTML
appcfg.Remote = remote
appcfg.NodeRemote = remote
appcfg.ChainID = cfg.chainId
if cfg.webRemoteHelperAddr != "" {
appcfg.RemoteHelp = cfg.webRemoteHelperAddr
}

router, err := gnoweb.MakeRouterApp(logger, appcfg)
router, err := gnoweb.NewRouter(logger, appcfg)
if err != nil {
return nil, fmt.Errorf("unable to create router app: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion gno.land/pkg/gnoweb/components/index.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
</div>
<div class="flex justify-between col-span-3">
<ul aria-label="Footer navigation" class="flex flex-col xl:flex-row gap-4 xl:gap-6">
<li><a href="https://gno.land/about">About</a></li>
<li><a href="/about">About</a></li>
<li><a href="https://docs.gno.land/">Docs</a></li>
<li><a href="https://faucet.gno.land/">Faucet</a></li>
<li><a href="https://gno.land/r/gnoland/blog">Blog</a></li>
Expand Down
37 changes: 12 additions & 25 deletions gno.land/pkg/gnoweb/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"log/slog"
"net/http"
"path/filepath"
"slices"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -84,7 +85,7 @@ func (h *WebHandler) Get(w http.ResponseWriter, r *http.Request) {

start := time.Now()
defer func() {
h.logger.Debug("request ended",
h.logger.Debug("request completed",
"url", r.URL.String(),
"elapsed", time.Since(start).String())
}()
Expand All @@ -103,7 +104,7 @@ func (h *WebHandler) Get(w http.ResponseWriter, r *http.Request) {
} else {
switch gnourl.Kind() {
case KindRealm, KindPure:
status, err = h.renderRealm(body, gnourl)
status, err = h.renderPackage(body, gnourl)
default:
h.logger.Warn("invalid page kind", "kind", gnourl.Kind)
status, err = http.StatusNotFound, components.RenderStatusComponent(body, "page not found")
Expand Down Expand Up @@ -133,20 +134,23 @@ func (h *WebHandler) Get(w http.ResponseWriter, r *http.Request) {
return
}

func (h *WebHandler) renderRealm(w io.Writer, gnourl *GnoURL) (status int, err error) {
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 gnourl.WebQuery.Has("help") {
if kind == KindRealm && gnourl.WebQuery.Has("help") {
return h.renderRealmHelp(w, gnourl)
}

// Display realm source page?
// XXX: it would probably be better to have this as a middleware somehow
// Display package source page?
switch {
case gnourl.WebQuery.Has("source"):
return h.renderRealmSource(w, gnourl)
case gnourl.Kind() == KindPure, endsWithRune(gnourl.Path, '/') || isFile(gnourl.Path):
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)
Expand Down Expand Up @@ -261,7 +265,7 @@ func (h *WebHandler) renderRealmSource(w io.Writer, gnourl *GnoURL) (status int,
file := gnourl.WebQuery.Get("file")
if file == "" {
fileName = files[0]
} else if contains(files, file) {
} else if slices.Contains(files, file) {
fileName = file
} else {
h.logger.Error("unable to render source", "file", file, "err", "file does not exist")
Expand Down Expand Up @@ -391,23 +395,6 @@ func generateBreadcrumbPaths(path string) []components.BreadcrumbPart {
return parts
}

func contains(files []string, file string) bool {
for _, f := range files {
if f == file {
return true
}
}
return false
}

// EndsWithRune checks if the given path ends with the specified rune
func endsWithRune(path string, r rune) bool {
if len(path) == 0 {
return false
}
return rune(path[len(path)-1]) == r
}

// IsFile checks if the last element of the path is a file (has an extension)
func isFile(path string) bool {
base := filepath.Base(path)
Expand Down
2 changes: 1 addition & 1 deletion gno.land/pkg/gnoweb/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var reRealmPath = regexp.MustCompile(`^` +
`(/(?:[a-zA-Z0-9_-]+)/` + // path kind
`[a-zA-Z][a-zA-Z0-9_-]*` + // First path segment
`(?:/[a-zA-Z][.a-zA-Z0-9_-]*)*/?)` + // Additional path segments
`([:$](?:.*))$`, // Remaining portions args, separate by `$` or `:`
`([:$](?:.*))?$`, // Remaining portions args, separate by `$` or `:`
)

func ParseGnoURL(u *url.URL) (*GnoURL, error) {
Expand Down
12 changes: 11 additions & 1 deletion gno.land/pkg/gnoweb/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,17 @@ func TestParseGnoURL(t *testing.T) {
Expected: nil,
Err: ErrURLMalformedPath,
},

{
Name: "simple",
Input: "https://gno.land/r/simple/test",
Expected: &GnoURL{
Host: "gno.land",
Path: "/r/simple/test",
WebQuery: url.Values{},
Query: url.Values{},
},
Err: nil,
},
{
Name: "webquery + query",
Input: "https://gno.land/r/demo/foo$help&func=Bar&name=Baz",
Expand Down

0 comments on commit d97f08f

Please sign in to comment.