Skip to content

Commit

Permalink
fix staticcheck errors and run gofmt (#69)
Browse files Browse the repository at this point in the history
* fix staticcheck errors and gofmt
* remove commented-out logging & debugging
* NotFound -> ErrNotFound
  • Loading branch information
llimllib authored Jan 6, 2023
1 parent bf46f43 commit e6a0242
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 90 deletions.
2 changes: 1 addition & 1 deletion _runtime/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func pushupHandler(w http.ResponseWriter, r *http.Request) {
}
if err := build.Respond(w, r); err != nil {
logger.Printf("responding with route: %v", err)
if errors.Is(err, build.NotFound) {
if errors.Is(err, build.ErrNotFound) {
http.NotFound(w, r)
} else {
http.Error(w, http.StatusText(500), 500)
Expand Down
25 changes: 6 additions & 19 deletions _runtime/pushup_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ func regexPatFromRoute(route string) routePat {
return routePat{strings.Join(out, "/"), slugs}
}

var NotFound = errors.New("page not found")
var ErrNotFound = errors.New("page not found")

type ctxKey struct{}

func Respond(w http.ResponseWriter, r *http.Request) error {
routeMatch := getRouteFromPath(r.URL.Path)
switch routeMatch.response {
case routeNotFound:
return NotFound
return ErrNotFound
case redirectTrailingSlash:
http.Redirect(w, r, routeMatch.route.path, 301)
http.Redirect(w, r, routeMatch.route.path, http.StatusMovedPermanently)
return nil
case routeFound:
route := routeMatch.route
Expand All @@ -114,7 +114,6 @@ func Respond(w http.ResponseWriter, r *http.Request) error {
default:
panic("unhandled route match response")
}
return nil
}

func mostSpecificMatch(routes []*route, path string) *route {
Expand Down Expand Up @@ -293,10 +292,7 @@ func isPartialRoute(mainRoute string, requestPath string) bool {
match := getRouteFromPath(requestPath)
if match.response == routeFound {
route := match.route
if route.path == mainRoute {
return false
}
return true
return route.path != mainRoute
}
panic("internal error: unexpected path")
}
Expand All @@ -308,15 +304,10 @@ func displayPartialHere(mainRoute string, partialPath string, requestPath string
} else {
path = mainRoute + partialPath
}
//log.Printf("PATH: %v\tREQUEST_PATH: %v", path, requestPath)
match := getRouteFromPath(path)
if match.response == routeFound {
if matchURLPathSegmentPrefix(match.route.regex, requestPath) {
return true
}
return false
return matchURLPathSegmentPrefix(match.route.regex, requestPath)
}
//log.Printf("MAIN ROUTE: %v\tPARTIAL PATH: %v\tREQUEST PATH: %v", mainRoute, partialPath, requestPath)
panic("internal error: unexpected path")
}

Expand Down Expand Up @@ -375,18 +366,14 @@ func matchURLPathSegmentPrefix(re *regexp.Regexp, s string) bool {
if s != "" {
segments = strings.Split(s, "/")
}
//log.Printf("RESEGMENTS: %#v\tSEGMENTS: %#v", reSegments, segments)
for i := 0; i < min(len(reSegments), len(segments)); i++ {
reseg := reSegments[i]
seg := segments[i]
if !regexp.MustCompile(reseg).MatchString(seg) {
return false
}
}
if len(segments) > len(reSegments) {
return false
}
return true
return len(segments) <= len(reSegments)
}

func min(a int, b int) int {
Expand Down
Loading

0 comments on commit e6a0242

Please sign in to comment.