Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Serving static files in all subdirectories

25 views
Skip to first unread message

Colin Kahn

unread,
Jun 13, 2018, 12:49:44 PM6/13/18
to
Hi,

I'm following this guide on serving static files, but cannot figure out a way to serve all files recursively in the directory I'm specifying: http://www.swi-prolog.org/howto/http/HTTPFile.html

Right now if I specify a path it will only serve the files in that directory but not the ones in its subdirectories. My use-case involves a js folder with many dynamically created files in several subdirectories that will get loaded dynamically by one another. Because of this I can't hard code all of the paths.

Thanks!

Markus Triska

unread,
Jun 13, 2018, 2:56:42 PM6/13/18
to
Hi Colin,

Colin Kahn <coli...@gmail.com> writes:

> Hi,
>
> I'm following this guide on serving static files, but cannot figure
> out a way to serve all files recursively in the directory I'm
> specifying: http://www.swi-prolog.org/howto/http/HTTPFile.html

You need the [prefix] option in the HTTP handler directive.

In the tutorial you mentioned, you can see how it is done with
http_reply_from_files/3, by adding the HTTP handler directive:

:- http_handler(root(.), http_reply_from_files('.', []), [prefix]).

Alternatively, by adding a suitable handler directive, you can also
inspect the HTTP requests yourself, and then do anything you want:

:- http_handler(/, handle_request, [prefix]).

handle_request(Request) :-
... etc.

Note again the [prefix] option.

I hope this helps!

All the best,
Markus

--
comp.lang.prolog FAQ: http://www.logic.at/prolog/faq/
The Power of Prolog: https://www.metalevel.at/prolog

Colin Kahn

unread,
Jun 14, 2018, 11:54:18 AM6/14/18
to
On Wednesday, June 13, 2018 at 11:56:42 AM UTC-7, Markus Triska wrote:
> Hi Colin,
>
> Colin Kahn
Hi Markus,

Thanks for your reply. Your example worked, using:

:- http_handler(root(.), http_reply_from_files('.', []), [prefix]).

Where I was getting myself into trouble was I was trying to define it like this:

:- http_handler(root(.), http_reply_from_files('resources/public', []), [prefix]).

And expecting the following to map:

http://localhost:2000/ => ./resources/public/index.html (this works)
http://localhost:2000/js/main.js => ./resources/public/js/main.js (this does not work)

It's easy for me to redo my directory structure to avoid that, but if you have any suggestions on making that work I would be interested in knowing how.

Also, what is the significance of [prefix]?

Thanks again!

Markus Triska

unread,
Jun 14, 2018, 3:51:13 PM6/14/18
to
Hi Colin,

Colin Kahn <coli...@gmail.com> writes:

> Where I was getting myself into trouble was I was trying to define it
> like this:
>
> :- http_handler(root(.), http_reply_from_files('resources/public',
> []), [prefix]).
>
> And expecting the following to map:
>
> http://localhost:2000/ => ./resources/public/index.html (this works)
> http://localhost:2000/js/main.js => ./resources/public/js/main.js
> (this does not work)

The basic idea is sound. However, for the particular case of /js/ and
some other locations, they are defined in http_server_files via:

user:file_search_path(js, library('http/web/js')).
...
http:location(js, root(js), [ priority(-100) ]).
...

Therefore, you must override these settings if they conflict with what
you want to serve. For example, in the case of /js/, you can use:

http:location(js, root('resources/public/js'), []).

With this added to your file, it should work as intended.

> Also, what is the significance of [prefix]?

This is best explained in:

http://eu.swi-prolog.org/pldoc/man?predicate=http_handler/3

I think the whole entry is well worth a read for your use case.

Colin Kahn

unread,
Jun 14, 2018, 5:18:28 PM6/14/18
to
Awesome, this clears up all the issues I was having.

These are defined in http/http_server_files:

user:file_search_path(icons, library('http/web/icons')).
user:file_search_path(css, library('http/web/css')).
user:file_search_path(js, library('http/web/js')).

I was able to override the js path with the snippet you posted above, but oddly it did not work for overriding the one for css. I was able to do both with the following:

user:file_search_path(css, 'resources/public/css').
user:file_search_path(js, 'resources/public/js').

Thanks again Markus!

burs...@gmail.com

unread,
Jun 14, 2018, 5:58:26 PM6/14/18
to
Why is there no server.xml and web.xml, where
you could configure such things? Did nobody invent
that yet for Prolog.

You could make a DTD or XSD for them, and then
it would be clear what belongs into each. What
I wonder is whether it would

not be possible to take existing formats, and
run mixed servers. Like putting Prolog stuff
into server.xml or web.xml.

ok, serving just files is not Prolog stuff,
every web server can do that natively. You
dont need any Prolog for that.

But in case you really want to utilize prolog,
no server.xml and web.xml yet?

(*)
I am maybe trying someting with a tweaked
class loader. I was recently studying the Tomcat
internals. Problem is that here and they,

especially in these .xml formats they want
class names. But if we could redirect this to
some Prolog modules, this could be fund.

Or if you want to some Logtalk objects, if
you don't like the idea of OO based on ISO core
modules, that I have already presented.

burs...@gmail.com

unread,
Jun 14, 2018, 6:04:40 PM6/14/18
to
Disclaimer: I understand that your use/business
case might be somewhere something really Prolog and
a lot of static content, and you want

to serve both by the same server. Still I am currious
why such formats (server.xml & web.xml) do not yet
work also for a Prolog server.

But I guess there are also dozen other formats,
around, which I am probably a little agnostic about.
But there are also a dozen devops and companies,

who want to do the deployment and configuration
of such server, and do not want to learn Prolog,
and probably already know xml well...

burs...@gmail.com

unread,
Jun 14, 2018, 6:15:59 PM6/14/18
to
Another common format is httpd.conf, here is an example:

https://collab.its.virginia.edu/wiki/toolbox/Example%20Httpd.conf%20File.html
0 new messages