atuttle / Taffy

:candy: The REST Web Service framework for ColdFusion and Lucee
http://taffy.io
Other
225 stars 117 forks source link

Access-Control-Allow-Origin error when api application scope expires #344

Closed tvanderlois closed 7 years ago

tvanderlois commented 7 years ago

allowCrossDomain is set to true - api works fine until the api application scope expires then get following error. The only known fix is to open the api using the reloadKey... probably could fix by reloadOnEveryRequest but that's not preferred. image

Can replicate by setting the application scope to timeout quickly

atuttle commented 7 years ago

Can you share your Application.cfc code?

tvanderlois commented 7 years ago

Application_save.pdf

atuttle commented 7 years ago

in the future https://gist.github.com/ would be a better way to share it. :)

Looking...

atuttle commented 7 years ago

Lines 54-59: Is there a reason you're re-implementing taffy's reload functionality? I see you're calling onApplicationEnd, onSessionEnd, etc explicitly. Those might make good additions to the core framework for its built-in reload functionality... (i.e. a nice pull request 😄)

I don't see anything standing out as obvious reasons you're having that problem. My only guess is that since you're setting variables.framework in your onRequestStart method, they're not available when Taffy starts up and it doesn't configure itself for CORS appropriately for that initial request. Maybe try moving that out to the pseudo-constructor area (lines 3-17).

tvanderlois commented 7 years ago

line 54-59 is my standard practice to reset session & application scope when needed. Happens to coincide with taffy's reload functionality.

With your suggestion moving the variables.framework section after line 17 results in an "application not defined" error (and of course taking out the application scoped vars first), so that doesn't seem like an option.

atuttle commented 7 years ago

I think it's worth setting a temporary static value, rather than an application variable, into those settings where necessary to see if this fixes your issue. If it does, I can help you work around that.

tvanderlois commented 7 years ago

that's what i did... but failed to keep "return super.onRequestStart();" in the OnRequestStart method. duh must be the reason for "application" not defined error.

Seems to work now, but how to use variables again?

atuttle commented 7 years ago

That's great news! Now for your application variables. The values just need to be set before they would be used. As long as there are no issues with the initialization/startup code, the values won't be used until pretty deep within the request lifecycle. You can simply set them to a static value and then overwrite them once you have the applicable value. Something like the following:

component {

    variables.framework = {
        exceptionLogAdapterConfig = {
            emailTo = "me@example.com" //fallback until application vars can be loaded
        }
    };

    function onApplicationStart(){
        //your code to load application variables here...

        super.onApplicationStart();
    }

    function onRequestStart(){
        variables.framework.exceptionLogAdapterConfig.emailTo = application.errorEmail;

        super.onRequestStart();
    }

}

Follow that same pattern as needed and you should be good to go. The only time it would use the fallback value is if there was an error prior to application vars loading and onRequestStart copying them over to variables.framework... But your current approach doesn't have anything in variables.framework up to that point either, so you would be no worse off than you are now. :)

tvanderlois commented 7 years ago

Thanks for the awesome support. I ended up just setting the framework.allowCrossDomain in the pseudo-constructor area - and set the remaining framework vars in onRequestStart... all is good.

atuttle commented 7 years ago

Excellent! Glad you got it sorted, and thanks for being willing to have the discussion here in public so others might benefit from it if they find this issue in a search. 👍