Skip to content
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

How to best serve sub folders and files? #9

Closed
kabukky opened this issue May 6, 2015 · 4 comments
Closed

How to best serve sub folders and files? #9

kabukky opened this issue May 6, 2015 · 4 comments

Comments

@kabukky
Copy link

kabukky commented May 6, 2015

Hi,
I'd like to serve everything that is present in folder called pages. Here's a sample structure:

|____pages
| |____webgl
| | |____Compressed
| | | |____webgl-export.jsgz
| | |____index.html
| | |____Release
| | | |____fileloader.js
| | |____TemplateData
| | | |____default-cover.jpg
| | | |____favicon.ico

Here's what I'm doing with httptreemux:

func pagesHandler(w http.ResponseWriter, r *http.Request, params map[string]string) {
    http.ServeFile(w, r, filepath.Join("pages", params["filepath"]))
    return
}

func InitializePages(router *httptreemux.TreeMux) {
    // For serving standalone projects or pages saved in in content/pages
    router.GET("/pages/*filepath", pagesHandler)
}

This works fine when I open e. g. http://server.url/pages/webgl/Release/fileloader.js.

However, http.ServeFile is supposed to serve the index.html when opening http://server.url/pages/webgl/.
It does that, but the path will be rewritten to the one without a trailing slash (http://server.url/pages/webgl).
This breaks links to files and sub folders in the index.html, e.g.:

<script src="Release/fileloader.js"></script>

which will now link to http://server.url/pages/Release/fileloader.js instead of http://server.url/pages/webgl/Release/fileloader.js.

Thanks
Kai

@dimfeld
Copy link
Owner

dimfeld commented May 6, 2015

Ah, that's interesting. As you've surmised, I don't think there's currently a good way to do this.

What I have in mind is an router-level option to never do the trailing slash redirect for paths with a catch-all at the end. I might actually make that the default behavior, since the current behavior in this case doesn't really make much sense, and I suspect there isn't anyone depending on it working how it does now.

I should be able to get this to you by next week, but maybe earlier. Hope that works :)

@kabukky
Copy link
Author

kabukky commented May 6, 2015

That's totally fine. Thanks for taking this on :)

@dimfeld dimfeld closed this as completed in 8901b30 May 6, 2015
@dimfeld
Copy link
Owner

dimfeld commented May 6, 2015

Ended up with a bit of time so I implemented this. Thanks for all your feedback!

@kabukky
Copy link
Author

kabukky commented May 6, 2015

Wow, you're fast! This works as imagined. Thank you!

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

No branches or pull requests

2 participants