Code Monkey home page Code Monkey logo

Comments (3)

Doerge avatar Doerge commented on May 22, 2024

I think the gateway already sets the header for redirects. The setup is just a bit complicated, as it must handle other response codes as well.

The basics (as I understand) is that the handler calls some app (Django/flask) and then interprets the result. Based on that, it either returns the response normally, (200), or raises an exception with a special payload in the body.

This is the handler after the app have returned a response:

        # To ensure correct status codes, we need to
        # pack the response as a deterministic B64 string and raise it
        # as an error to match our APIGW regex.
        # The DOCTYPE ensures that the page still renders in the browser.
        if response.status_code in [400, 401, 403, 404, 500]:
            content = "<!DOCTYPE html>" + str(response.status_code) + response.data
            b64_content = base64.b64encode(content)
            raise Exception(b64_content)
        # Internal are changed to become relative redirects
        # so they still work for apps on raw APIGW and on a domain.
        elif response.status_code in [301, 302]:
            # Location is by default relative on Flask. Location is by default
            # absolute on Werkzeug. We can set autocorrect_location_header on
            # the response to False, but it doesn't work. We have to manually
            # remove the host part.
            location = response.location.split(environ[u'HTTP_HOST'])[1]
            raise Exception(location)

The gateway parses exceptions and handles them differently based on the payload in the exception.
E.g. for a redirect we look for an exception like raise Exception('/my/redirect/location').

zappa.py line 622-625'ish configures the gateway:

elif status_code in ['301', '302']:
    selection_pattern = '\/.*'
    response_templates = {content_type: REDIRECT_RESPONSE_TEMPLATE for content_type in self.integration_content_types}
    response_parameters["method.response.header.Location"] = "integration.response.body.errorMessage"

The selection_pattern above is an expression which looks for exception payloads that starts with forward slash.

This is transparent from inside the app. You just return a 301 or 302 response.

Or is there a simpler/better way of doing this?

from zappa.

mathom avatar mathom commented on May 22, 2024

Ah, that makes sense. Looking over the mapping again, looks like it's already working like I was thinking it should.

Thanks for the explanation!

from zappa.

Doerge avatar Doerge commented on May 22, 2024

No problemo! Took me quite a while to grok!

from zappa.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.