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 set default page in static request #1346

Open
boboxiaodd opened this issue Dec 26, 2018 · 11 comments
Open

How to set default page in static request #1346

boboxiaodd opened this issue Dec 26, 2018 · 11 comments

Comments

@boboxiaodd
Copy link

just like:
http://127.0.0.1/

redirect to:
http://127.0.0.1/index.html

@essen
Copy link
Member

essen commented Dec 26, 2018

Make a handler for / that redirects to /index.html.

@boboxiaodd
Copy link
Author

@essen Yes, but i think it's very cumbersome .

[
    {"/user/[...]", user_router, []},
    {"/",cowboy_static,{file,"/www/index.html"}},
    {"/web/",cowboy_static,{file,"/www/web/index.html"}},
    {"/mobile/",cowboy_static,{file,"/www/mobile/index.html"}},
    {"/[...]" , cowboy_static, {dir,"/www"}
]

@jeromedes
Copy link

@boboxiaodd
Copy link
Author

@jeromedes
i mean like Apache Server, can set the default page
maybe like this options:

{"/[...]" , cowboy_static, {dir,"/www"},[{default,"index.html"}]}

when visit /game/ can auto redirect /www/game/index.html

@essen
Copy link
Member

essen commented Jan 10, 2019

No I'm not going to add an option like this. If any rewrite has to be added to Cowboy it should occur long before, at the stream handler level, and be more general than finding an index.html file.

@essen
Copy link
Member

essen commented Jan 10, 2019

Why close? The issue remains and there's no other issue tracking this.

@boboxiaodd
Copy link
Author

Sorry , i think this is not an urgent issue, so ...
maybe we can add a option in

{"/[...]" , cowboy_static, {dir,"/www",[{index,"index.html"}]}}

@boboxiaodd boboxiaodd reopened this Jan 10, 2019
@adrianroe
Copy link
Contributor

I'd be interested in (and happy to provide PRs to) enriching cowboy_static. A use case that I currently have (and use custom handlers for) is adding headers to the response when serving static content. I suspect there are lots of similar modules across lots of cowboy users that could be made obsolete :)

Let me know if you want help / have decided a strategy.

@essen
Copy link
Member

essen commented Jan 10, 2019

Please open a separate ticket for each additional feature you think is needed. In this particular ticket the solution can be generalized to a rewrite stream handler that would work for more than cowboy_static for example, and it's possible your changes can as well.

In general I'd like to keep cowboy_static itself focused on the core feature of serving the file itself, but I'm not against adding features to it if they're really specific.

@jeromedes
Copy link

jeromedes commented Jan 11, 2019

@boboxiaodd Yes like with most web servers which have a way to configure a default file like index.html to be served for a directory. Again, look at the file server example where you can easily change the behavior and serve an index.html file instead of listing the content of the directory.

execute(Req, Env=#{handler := cowboy_static, handler_opts := {dir, Dir, Extra}}) ->
	Path = cowboy_req:path(Req),
	FullPath = filename:join([<<Dir/binary, Path/binary>>]),
	case filelib:is_dir(FullPath) of
		true ->
			{dir_handler, DirHandler} = lists:keyfind(dir_handler, 1, Extra),
			{ok, Req, Env#{handler => DirHandler, handler_opts => FullPath}};
		false ->
			{ok, Req, Env}
	end;
execute(Req, Env) ->
	{ok, Req, Env}.

-define(INDEX, <<"index.html">>).

resource_exists(Req, DirPath) ->
	IndexPath = filename:join(DirPath, ?INDEX),
	case filelib:is_regular(IndexPath) of
		true ->
			{true, Req, IndexPath};
		false ->
			{false, Req, DirPath}
	end.

to_html(Req, IndexPath) ->
	{ok, HTML} = file:read_file(IndexPath),
	{HTML, Req, IndexPath}.

@essen
Copy link
Member

essen commented Oct 3, 2019

Hm I was looking into making an index-specific stream handler but we are lacking information necessary to do this. It has to be done after routing, no choice. Which comforts me in the idea that it should just be done via some kind of rewrite functionality.

Maybe the configuration of a cowboy_rewrite_h could look like this:

[{PathPrefix, [RewriteFun], How}]

PathPrefix :: binary()
RewriteFun :: fun((PathSuffix) -> {true, PathSuffix} | false)
How :: internal | 300..399

If the path prefix matches, then the rest of the path is given to all the funs defined and they can return whether a rewrite should happen. When a rewrite happens it can either occur internally or create a redirect response. Maybe a {continue, PathSuffix} return value can also be added to continue with the rules rather than redirect immediately, in this case multiple rewrite rules can be chained as needed.

This could be used for more than index functionality, like ensuring there's a forward slash or all the usual rewrite use cases.

Thoughts?

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

4 participants