SG with Single Page WebSite

Dec 5, 2014 at 12:57 PM
Hello,

I actually use the Service Gateway with multiple API endpoint, it's work well.
But I also would like to use it with a Single Page WebSite.
The problem is the following: call to web api are done in the javascript code and so url are not rewrited, for example :

The website is the target : portal.
The configuration allow me to go to the portal using the url http://mycloudservice.cloudapp.net/portal

When I try to login, the following javascript code is executed :
  var url = '/Token';
    $.ajax(url,
        {
            method: 'POST',
            dataType: 'json',
            cache: false,
            data: data
        })
        .then(...)
so when I click to authenticate, the code goes to http://mycloudservice.cloudapp.net/token and not http://mycloudservice.cloudapp.net/portal/token.

The aim is to use the website with or without the Gateway (the WebSite doesn't have to know that there is a Gateway in the architecture and must work w/wo the Gateway).

What is the best way to realize this? I think there is no configuration that allow this in the SG and I have to modify a little bit my WebSite...

any idea?
Dec 9, 2014 at 3:54 AM
Hi Jérémie,

You're right that the Gateway doesn't update the JavaScript pages like that and that it doesn't make sense to do so. But what you can do is add a request header on the gateway server that passes through a flag that you can use on the web server to set a root path in JavaScript.

On the gateway server you can create a rule at the server or site level. Here's an example:

<rule name="Add Gateway Header">
<match url=".*" />
<serverVariables>
    <set name="HTTP_ServiceGateway" value="1" />
</serverVariables>
<action type="None" />
</rule>

Note that if you create this at the site level that you'll need to give permission for that server variable to be set at the site level. That's set in URL Rewrite at the server level under "View Server Variables", if you're using IIS Manager.

On the web servers you'll have a server variable called HTTP_ServiceGateway. You can't get this from JavaScript directly since it doesn't have access to the request variables, but you can write the variable server-side as a hidden tag in your HTML. A simple way to add this to an ASPX page is:
<%= Request.ServerVariables("HTTP_ServiceGateway") %>. Obviously you can do this from an MVC view or other framework.

Then in your JavaScript you can check if that is set to 1, and if so you can set your own root folder to differentiate between the gateway-called or the directly called pages.

Or you could call the variable HTTP_ServiceGatewayRootPath and include the root path. Then you can control the root path from the gateway rather than your code.