Project

General

Profile

docroot, approot, internal path and directory structure...

Added by Plug Gulp almost 12 years ago

Hi,

I am trying to get my head around the relationship between docroot, approot, internal path and project directory structure. I am trying to write a simple app to verify my understanding but have not succeeded in understanding the relationship between the four. Here is what I am trying to do:

The test app has two "views".

  1. If the app is accessed using the url http://localhost then it displays image_1.
  2. If the app is accessed using the url http://localhost/abcd then it displays image_2.

In the first case the internalPath is automatically set to "/". In the second case the internalPath is automatically set to "/abcd". In the test app I decide which image to display depending on the value of the internal path. The images are accessed in the application without any directory reference as follows:

WImage("image_1.jpg", "image_1");

and

WImage("image_2.jpg", "image_2");

The directory structure of the project is as follows:

/TestAppDir
  |
  +-- TestApp.wt
  |
  +-- image_1.jpg
  |
  +-- image_2.jpg
  |
  +-- resources

I start the wthttp server as follows:

./TestApp.wt --http-address 0.0.0.0 --docroot=".;/image_1.jpg,/image_2.jpg,/resources"

And the test app works as expected. But now, if I change the directory structure of the test app as follows:

/TestAppDir
  |
  +-- doc_root
  |    |
  |    +-- TestApp.wt
  |
  +-- app_root
  |    |
  |    +-- resources
  |         |
  |         +-- image_1.jpg
  |         |
  |         +-- image_2.jpg
  |
  +-- resources

And change the application to access the images as follows:

WImage(appRoot() + "resources/image_1.jpg", "image_1");

and

WImage(appRoot() + "resources/image_2.jpg", "image_2");

then what values do I pass to ---approot and ---docroot when starting the wthttp server? I tried the following:

./doc_root/TestApp.wt --http-address 0.0.0.0  --approot ./app_root --docroot="./doc_root;../app_root/resources/image_1.jpg,../app_root/resources/image_2.jpg,/resources"

But this did not work. What is the correct way of setting the approot and docroot when the directory structure is as described above and the application uses internal paths?

I observed in the server log that the app is able to successfully fetch the images. But it is not able to display them. Instead the alternative text for the image is displayed.

Kind regards,

~Plug


Replies (7)

RE: docroot, approot, internal path and directory structure... - Added by Koen Deforche almost 12 years ago

Hey Plug,

The docroot is for resources that need to be available to the browser; the approot is for 'resources' which are not published.

In this case, you need to move your resources folder to doc_root. Also, you shouldn't put your application in doc_root (because then a user can 'download' it). We usually keep this directory structure as you describe and put the application in the 'root' of it.

Internal paths have no equivalent on the filesystem --- they are entirely 'virtual' and should be handled in code instead.

Regards,

koen

RE: docroot, approot, internal path and directory structure... - Added by Plug Gulp almost 12 years ago

@Koen Deforche

"The docroot is for resources that need to be available to the browser; the approot is for 'resources' which are not published."

So, if I don't want the user to "directly download" the resources (in the above example image_1 and image_2) but instead "served" by the application then is it okay to put image_1 and image_2 under approot and access them using WImage(appRoot() + "resources/image_1") and WImage(appRoot() + "resources/image_2")?

@Koen Deforche

"In this case, you need to move your resources folder to doc_root. Also, you shouldn't put your application in doc_root (because then a user can 'download' it). We usually keep this directory structure as you describe and put the application in the 'root' of it."

Ah, I learnt something new today! I thought that because the browser needs to directly access the application, it has to be under docroot. Anyway, I will try the app with the suggested change.

@Koen Deforche

"Internal paths have no equivalent on the filesystem --- they are entirely 'virtual' and should be handled in code instead."

Yes, but the problem I am facing is, if I use internal paths in the test application then I have to set additional values to the ---docroot parameter of wthttp server. So the question is what values do I pass to ---docroot and ---approot if the directory structure now is as follows. Note that I am keeping some resources in the app_root directory to avoid the "direct download" similar to the reason for moving the application out of the doc_root (is that understanding correct?):

/TestAppDir
  |
  +-- TestApp.wt
  |
  +-- doc_root
  |    |
  |    +-- resources
  |
  +-- app_root
       |
       +-- resources
            |
            +-- image_1.jpg
            |
            +-- image_2.jpg

The wthttp server is invoked as follows:

./TestApp.wt --http-address 0.0.0.0 --approot ???? --docroot=????

So to summerise my question, what should be the value of ---approot and ---docroot, and how should image_1 and image_2 be accessed in the test app if they are stored under app_root directory?

Thanks and regards,

~Plug

RE: docroot, approot, internal path and directory structure... - Added by Koen Deforche almost 12 years ago

Hey Plug,

The misconception is that WImage(appRoot() + "resources/image_1") will work. WImage expects an URL. This URL is used by the browser to do a request for the image.

The URL can reference something

  • that is either deployed in the docroot (e.g. /resources/image_1)
  • that is deployed elsewhere (e.g. http://www.google.be/image_1)
  • that is served dynamically by your application through a WResource, e.g. WImage(new WFileResource(appRoot() + "resources/image_1"))

If you use internal paths, then you can further specify your docroot if your deploy-path ends with a '/' to get 'clean internalpaths' (which do not have this ?_=... structure).

In your case that would be docroot=".;/resources" which specifies that every URL which starts with /resources is in your docroot.

Note that you really need to move the images to doc_root/resources to be able to use WImage(/resources/image_1") instead of the less efficient alternative WImage(new WFileResource(appRoot() + "resources/image_1\"))

koen

RE: docroot, approot, internal path and directory structure... - Added by Plug Gulp almost 12 years ago

@Koen Deforche

"The misconception is that WImage(appRoot() + "resources/image_1") will work."

Ah, okay. I will make the appropriate changes and test again.

But the server log shows that the server was successful in fetching the image from the correct path hence the confusion:

127.0.0.1 - - [2013-Jun-12 13:22:38.316281] "GET /app_root/resources/image_1.jpg HTTP/1.1" 200 2691

@Koen Deforche

"In your case that would be docroot=".;/resources\" which specifies that every URL which starts with /resources is in your docroot.\"

Or should that be docroot="./doc_root;/resources"?

BTW, does the rule that apply for images also apply for templates and strings? Do I need to put the template and string files under docroot directory? I updated my test app to include a template and a string using the following:

<code class="cpp">
messageResourceBundle().use(appRoot() + "template");
messageResourceBundle().use(appRoot() + "string");
</code>

The directory structure is:

/TestAppDir
  |
  +-- TestApp.wt
  |
  +-- doc_root
  |    |
  |    +-- resources
  |    |
  |    +-- image_1.jpg
  |    |
  |    +-- image_2.jpg
  |
  +-- app_root
       |
       +-- template.xml
       |
       +-- string.xml

The wthttp server is invoked as follows:

./TestApp.wt --http-address 0.0.0.0 --approot ./approot --docroot="./doc_root;/resources"

Now the images are displayed, but the template is not displayed. Do I need to move the template and string files under doc_root as well?

Kind regards,

~Plug

RE: docroot, approot, internal path and directory structure... - Added by Plug Gulp almost 12 years ago

Okay, my fault. The test is working now. For the template, I forgot to use WString::tr to load it.

Thanks and kind regards,

~Plug

RE: docroot, approot, internal path and directory structure... - Added by Alan Finley almost 12 years ago

Koen Deforche wrote:

Also, you shouldn't put your application in doc_root (because then a user can 'download' it). We usually keep this directory structure as you describe and put the application in the 'root' of it.

Is this correct for a case when Wt application is being deployed with Apache?

RE: docroot, approot, internal path and directory structure... - Added by Koen Deforche almost 12 years ago

Hey,

For apache indeed you need to put the executable inside your document root --- but configure Apache not to serve it but rather execute it.

Regards,

koen

    (1-7/7)