Skip to content

Fix Static files route not working #1723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed

Fix Static files route not working #1723

wants to merge 4 commits into from

Conversation

pwli0755
Copy link
Contributor

fix 404 error when using Static files route Echo.Static()
resubmit of #1671
related issue

Fix Static files route not working
@codecov
Copy link

codecov bot commented Dec 17, 2020

Codecov Report

Merging #1723 (3d8e07c) into master (829e821) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1723   +/-   ##
=======================================
  Coverage   85.77%   85.77%           
=======================================
  Files          29       29           
  Lines        2017     2018    +1     
=======================================
+ Hits         1730     1731    +1     
  Misses        181      181           
  Partials      106      106           
Impacted Files Coverage Δ
echo.go 86.44% <100.00%> (+0.04%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 829e821...3d8e07c. Read the comment docs.

@@ -161,6 +170,40 @@ func TestEchoStatic(t *testing.T) {
}
}

func TestEchoStaticRedirectIndex(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not run tests like that because:

  • it adds constant 200ms to execution time - using sleeps makes tests slow in long run and brittle some cases
  • it assumes that port 1323 is free - use 127.0.0.1:0 :0 etc instead. :0 would bind to random port and you can get the address where server was binded with addr := e.Listener.Addr(). See documentation https://golang.org/pkg/net/#Listen

    If the port in the address parameter is empty or "0", as in "127.0.0.1:" or "[::1]:0", a port number is automatically chosen. The Addr method of Listener can be used to discover the chosen port.

TestEchoStatic that you modified above is pretty much same as this is. When you spin up new http server. Each request will invoke ServeHTTP(rec, req) method same as TestEchoStatic does ... but without starting a server.

@@ -500,6 +500,7 @@ func (common) static(prefix, root string, get func(string, HandlerFunc, ...Middl
}
return c.File(name)
}
get(prefix, h)
Copy link
Contributor

@aldas aldas Dec 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then dealing with this method we need to consider where is is called. Is it e.Static() or group.Static()

  • e.Static() is on server root level i.e. http://server/assets/cat.gif URLs
  • group.Static is on group level which means in addition to this method prefix we have group path as prefix in url. i.e. http://server/group/assets/cat.gif

and regarding to prefix we should consider it ends with slash or not
i.e. prefixes:

  • ``
  • /
  • /assets
  • /assets/

now lets add two possibilities here: if group path is /user or /user/ what routes we will end up with?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something like that

	// prefix does not end with slash
	if prefix != "" && prefix[len(prefix) - 1] != '/' {
		get(prefix, h) // i.e. prefix=/assets and URL `/assets` must give directory index
		return get(prefix+"/*", h) // and asterisk to match anything in that directory
	}
	if prefix != "" {
		get(prefix[:len(prefix) - 1], h) // i.e. prefix=/assets/ then add route `/assets` for directory index
	}
	// i.e. prefix="" (empty) and URL `http://server` or `http://server/` must give directory index
	// i.e. prefix=/assets/ and URL `/assets/` must give directory index, `/assets/*` serve files
	return get(prefix+"*", h)

@aldas
Copy link
Contributor

aldas commented Dec 29, 2020

@lammel consider this patch for merge. It will add exact match route for directory when prefix does not end with slash. Also documents behavior and possible problems
patch_1723_with_tests.patch.txt (renamed to *.txt or github will not allow upload)

@lammel
Copy link
Contributor

lammel commented Jan 2, 2021

Thanks @aldas and @pwli0755 .
As I was unable to push to this PR another PR #1747 has been opened and merged. So closing this one.

@lammel lammel closed this Jan 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants