appharbor support for swagger
Hi,
How can i use swagger (enabled on root url) with my deployed application in Appharbor? It works perfectly when I debug locally but as soon as my build is deployed I get the below error.
Can't read from server. It may not have the appropriate access-control-origin settings.
Is there a way to enable CORS on your side?
Discussions are closed to public comments.
If you need help with AppHarbor please
start a new discussion.
Keyboard shortcuts
Generic
| ? | Show this help |
|---|---|
| ESC | Blurs the current field |
Comment Form
| r | Focus the comment reply box |
|---|---|
| ^ + ↩ | Submit the comment |
You can use Command ⌘ instead of Control ^ on Mac
Support Staff 1 Posted by rune on 11 Nov, 2016 01:41 AM
Hi,
Yep it's definitely possible to serve CORS requests on your application's root domain - along with all the CORS access control headers required for this. It's difficult to tell exactly what goes wrong with the swagger requests without more information. Have you identified and can you provide more details about the failing requests and/or ideally a sample app/request that doesn't work as expected so I can take a look for myself?
Best,
Rune
2 Posted by evilroxx on 11 Nov, 2016 03:59 AM
Hi Rune,
Thanks so much for reaching out.
As explained in my email it says cant read from server. it may not have the appropriate access-control-origin settings.
I cant reveal my api uri in a public forum however I will gladly speak with an example.
So lets say my root api / calls reroutes to /swagger/ui/index. It opens swagger page successfully and says:
"fetching resource list: http://myapi.apphb.com:12345/swagger/docs/v1; Please wait."
and then shows the error message:
"Can't read from server. It may not have the appropriate access-control-origin settings."
I checked on SO and people said that web servers needs to have CORS enabled.
Let me know how you can help.
Thanks
R
3 Posted by balexandre on 11 Nov, 2016 08:35 PM
@wanudev
can't you just enable that in your application with:
or maybe in your app directly...
Support Staff 4 Posted by rune on 12 Nov, 2016 02:00 AM
Hi,
You may have to add the headers like balexandre mentioned, but there's also another potential cause of this issue: The error message "fetching resource list: http://myapi.apphb.com:12345/swagger/docs/v1; Please wait." shows that a port number is included n the URL. I'm assuming that this is a fake port number, but can you confirm that it's not either port 80 or port 443?
In that case the problem likely is that a URL generated by your application uses the local port number for the web worker. AppHarbor is a load-balanced platform where your application listens on port 80 and 443, and those requests are then reverse-proxied to backend servers that serve requests from different port numbers.
There are a few ways you can approach this to generate URLs without the port number; take a look at this KB article for a couple of tips and tricks on how to do that.
If that doesn't help, perhaps you can let me know which part of your application that generates the URL with the port number? Is it in your own code or does a Swagger library build the URL?
Best,
Rune
5 Posted by evilroxx on 19 Nov, 2016 07:11 PM
Hi,
The port number is 16340. Its automatically generated by Swagger library.
http://{myapp}.apphb.com/swagger/ui/index => http://{myapp}.
apphb.com:16340/swagger/docs/v1
I am trying to programmatically see if I can change the way swagger
generates the url but having no luck at the moment. Can you please see if
something can be done on your end?
Thanks
Rohan
6 Posted by evilroxx on 19 Nov, 2016 07:28 PM
I have enabled CORS programmatically.
private static void ConfigureCors(HttpConfiguration config)
{
//Support for CORS
config.EnableCors();
EnableCorsAttribute CorsAttribute = new
EnableCorsAttribute("*", "*", "GET,POST,PUT,DELETE");
config.EnableCors(CorsAttribute);
}
Support Staff 7 Posted by rune on 22 Nov, 2016 02:35 AM
Hi,
Ok so I can see you're using the Swagger library, and the source code does seem to use the request URL port number when the
X-Forwarded-Portheader isn't present. This would explain the behavior you're seeing as theX-Forwarded-Portheader is not being set by AppHarbor's load balancers at this time.We may add that header later on, but in the meantime I suspect you can address this issue yourself by setting the Root URL resolver used by the
SwaggerDocsConfiginstance. In particular, you can set the resolver after initializingSwaggerDocsConfigusing theRootUrlmethod here. This would just require you to inject a method that's almost identical to the default resolver, but sets the port based on the scheme (if it's "https" the port will be 443 and if it's "http" the port is 80, and maybe add some logic to handle development/local requests]).That being said -- did you try setting the appSetting described in the top of the KB article I referenced earlier? I suspect setting the
aspnet:UseHostHeaderForRequestUrlsetting totruemight be the easiest and most elegant solution since the issue is caused by third party code. This approach doesn't always work though, but can quickly be tested by adding a configuration variable to your your application so it's injected when redeployed.Best,
Rune
8 Posted by johnkors on 24 Apr, 2017 05:57 PM
Takk, Rune!
The `aspnet:UseHostHeaderForRequestUrl` helped me getting public URLs working for Swagger. I was having the same issue.
Another related issue with Swashbuckle, was that the UI would complain about the missing xml doc file. The project setting of auto-generating the xml doc file needs to be set for both debug AND release builds, since apphb of course uses release mode to build.
Support Staff 9 Posted by rune on 26 Apr, 2017 02:25 AM
Hi John,
You're welcome :) And thanks for the feedback and extra information, this might very well be helpful for other customers!
Best,
Rune
rune closed this discussion on 26 Apr, 2017 02:25 AM.