Relative paths for assets in Play Framework - nginx

I'm running two Play 2.3.x applications behind nginx. In nginx, application A is configured to be accessed at "/". Application B is configured to be accessed at "/appB/".
I'm having some problems resolving assets for application B when using the built in routes/assets functionality (<script src="#routes.Assets.at("someScriptfile.js")") type="text/javascript"></script>. The problem here is that the URL will be absolute, for example /assets/file.png. This will result in that the proxy forwards the request to application A.
I want to use relatives path instead. That would allow me to run the application B both with and without the proxy. I want the asset url to be assets/file.png.
What are my options? I know I can solve it by using content rewrite in the proxy, but we would

Assets.at returns a Call, which is converted to a String implicitly in Scala. The String it produces is just the URL the reverse router resolves to. If all you want to do is remove the leading slash, you can drop the first character.
#routes.Assets.at("someScriptfile.js")").drop(1)
Or to be super sure that what's removed is only a leading slash, you could use stripPrefix.
#routes.Assets.at("someScriptfile.js")").stripPrefix("/")

Try something like this:
For your Application B use absolute URLs but prepend them with the "/appB/"
package misc;
import controllers.routes;
public class BAssets {
public static String at(String path) {
return "/appB" + routes.Assets.at(path).toString();
}
}
Then every time you want to serve some asset for application B, you will write it like this:
<script src="#misc.BAssets.at("javascripts/hello.js")"></script>
If you want, you can even include more logic in the method which calculates the path to the asset - for example based on if your application is in dev mode or in prod. Or even based on the proxy configuration (you can decide to change the path to appC tomorrow).

I just found a solution that works.
Edit application.conf and add the following: application.context="/appB/".
This will translate <script src="#routes.Assets.at("someScriptfile.js")" type="text/javascript"></script> to url /appB/assets/someScriptfile.js

I just created a PR to have relative paths added to Play. Looks like it will be released with 2.6.7
https://github.com/playframework/playframework/pull/7839

Related

Opening absolute urls in RedwoodJS - SSR

I have a RedwoodJS App with several routes.
It's very basic for now and I start the development server with yarn rw dev.
Now I have created some routes:
/ -> HomePage
/sign-in -> SignInPage
/register -> RegisterPage
I can call the routes internally via navigate which works fine. But I cannot call these routes via localhost:8910/register for example. I get a 404 error.
What do I have to do to make (almost) all routes callable directly with their absolute urls?
A late response to your question, but all the routes are managed in the Routes.tsx-file. In my example, I have a route to access the BikesPage-component (located in .../pages/Bike/BikesPage hence the naming BikeBikesPage).
So to ask your question, to make them callable by their absolute name, the name must be the same as the path. Hope that helped you or anyone coming here afterwards!

Openstack swift, which url to use in my use case?

I'll be using swift as like S3, where it will host number of files for my site.
I've set my container as public, and so
Here is the URL for a file.
https://provider/v1/auth_1293kdfj/folder/file.mp4
There are two problems:
Is it correct in using above format uri in public setting? It feels kinda dangerous because it has the auth_bit.
If I visit https://provider/v1/auth_1293kdfj/folder/ it lists all files/bojects in container folder. I wish to turn this off, how would I do so? Should I make my container private and assign temp url to ALL objects?
Thanks for your help!
1 - Yes. It's correct. Probably your authentication server is protected behind a firewall. This is just the project id. However if you are really worried about security you can configure your webserver to hide this info.
2 - Probably your container has the following acl:
.r:*,.rlistings
If you set by yourself (what I believe is the most common) you should change it just to:
.r:*

Use Firebase SSL Hosting for two different web applications

It appears that I can only have one Top Level Domain (TLD) associated with one firebaseapp SSL hosting. Is that true?
For example, I'd like https://x.example.com to go to my https://x.firebaseapp.com and https://y.example.com to go to my https://y.firebaseapp.com
When I try I see: (see image below) (I'm using https://city.tidalforce.org/ for a different firebase app at https://cityregister.firebaseapp.com/ but I'd like to use the same domain for another app with another full name such as https://y.tidalforce.org
Is this possible?
In short no, although you may be able to host different apps in different subfolders like https://example.com/x, https://example.com/y and if they have javascript router for example, if you are using Angular for front-end and using Angular-router or something you will nee to provide appropriate rewrite rules like:
"rewrites": [ {
"source" : "/x/**",
"destination" : "/x/index.html"
},
{
"source" : "/grid/**",
"destination" : "/grid/index.html"
} ]
to rewrite url requests from that folder to route correctly.
Hope it helps :)

how can i check url "\\abc\wof\TY044-12" exist

i 've tried using
string path= "\\abc\wof\TY044-12";
bool exist=System.IO.Directory.Exists(path);
but 'bool' return true on localhost but return false on server side.
Also, i've searched some answers, but it is difficult reconfigure permission of IIS.
can i use FileWebrequest/httpWebRequest command? not understand about this
FileWebRequest request = (FileWebRequest)System.Net.WebRequest.Create("\\abc\wof\TY044-12");
FileWebResponse response = (FileWebResponse)request.GetResponse();
Ihope someone can help me. THANKS!!!
That looks more like a UNC.
To me it seems the appool account on iis which is hosting your application doesn't have permissions to access that UNC folder. Please refer link below to set it:
http://technet.microsoft.com/en-us/library/cc771170(v=ws.10).aspx
The link below says you can access files over the network using pre-registered reserved types like http, https, file, etc.
http://msdn.microsoft.com/en-us/library/bw00b1dc(v=vs.110).aspx
...And if you pass unc paths to the Uri class during construction, you can get the required uri scheme:
var uri = new Uri(#"\\abc\folder\file.jpg");
Console.WriteLine(uri.ToString()); //outputs - file://abc/folder/file.jpg
However the recommended approach is using classes in the System.IO namespace like you originally started out with.

Creating a url in controller in asp.net mvc 4

I am trying to send activation mail to the currently registered user.In mail body,I need to send a link like http://example.com/account/activation?username=d&email=g.Now, for debugging on local machine, I manually write it as localhost:30995/account/activation?username=d&email=g. But, when my port number changes, I need to rewrite it.
I tried another question
on this website,but, compiler gives error like url.action doesnot exist.
Please give me fresh solution as I am confused with that solution.
Use a Url.Action overload that takes a protocol parameter to generate your URLs:
Url.Action("Activation", "Account", new { username = "d", email = "g" }, "http")
This generates an absolute URL rather than a relative one. The protocol can be either "http" or "https". So this will return http://localhost:XXXXX/account/activation?username=d&email=g on your local machine, and http://example.com/account/activation?username=d&email=g on production.
In short, this will stick whatever domain you're hosting your app on in front of your URL; you can then change your hostname/port number/domain name as many times as you want. Your links will always point to the host they originated from. That should solve the problem you're facing.
Try using IIS / IIS-Express instead of Casinni web server that comes with visual studio.
You could add bindings to have the right URL (with host entries of course).
This will avoid the port numbers in your links.

Resources