maekitalo / tntnet

GNU Lesser General Public License v2.1
74 stars 35 forks source link

Hosting static files #62

Closed jgwinner closed 5 years ago

jgwinner commented 5 years ago

Ok, I've got a (hopefully) dumb question.

I checked the docs, and it says: http://www.tntnet.org/howto/static-howto.html

In tntnet.xml, I put the lines:

<mapping>
  <url>^/(.*)</url>
  <target>static@tntnet</target>
  <pathinfo>/var/www/htdocs/$1</pathinfo>
</mapping>

I'm using the basic sample:

int main(int argc, char* argv[])
{
    std::cerr << "Starting up CLabs webserver" << std::endl;
    try
    {
        tnt::Tntnet app;
        app.listen(8000);
        app.mapUrl("^/(.*)", "$1");
        app.mapUrl("^/([^.]+)(\\..+)?", "$1"); 

        app.run();
    }
    catch (const std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
    std::cerr << "Shutting down webserver" << std::endl;
}

Yet, I get 404 errors for any of the files I have loaded in /var/www/htdocs/

I've tried slightly different versions of the files to make sure I didn't have a typo or something.

Do we have to do something in the code to enable static files? Do I need a different app.mapURL statement? Does that bypass tntnet.xml? Do you need a statement to read tntnet.xml?

Thank you,

== John ==

maekitalo commented 5 years ago

Tntnet has to know, what to do when a request arrives. For that there are mappings from url to the component. There are 2 methods to set those mappings. Either using a configuration file or in the program itself calling mapUrl. Both can also be combined.

By default nothing is loaded. Also there is no default "tntnet.xml", which is loaded. There is a process "tntnet", which reads the tntnet.xml. But nowadays I recommend to write your own main and instantiate a tnt::Tntnet object as you do in the above example.

If you want to read the tntnet.xml you have to do something like:

tnt::TntConfig cfg;
std::ifstream in("tntnet.xml");
in >> cxxtools::xml::Xml(cfg);
app.init(cfg);

or just recently I added a constructor for that, so that you can write:

tnt::Tntnet app(cfg);

Or you call mapUrl to map urls to the static component:

app.mapUrl("^/(.*)", "static@tntnet")
   .setPathInfo("/var/www/htdocs/$1");

Maybe you want to add a default index.html:

app.mapUrl("^/index.html", "static@tntnet")
   .setPathInfo("/var/www/htdocs/index.html");
app.mapUrl("^/(.*)/index.html", "static@tntnet")
   .setPathInfo("/var/www/htdocs/$1/index.html");
jgwinner commented 5 years ago

Ah, got it!

I thought I tried the process method, and it didn't seem to work, but no doubt I didn't have it starting in the right place. In any event, the code above is what I needed to know, thank you!

== John ==

jgwinner commented 5 years ago

By the way - it might be a good idea to mention something like this in the docs under static files; that's what I missed (it's covered in other areas, so a reminder might be good or a link to those areas).