Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configure base URL for API schema #146

Closed
machristie opened this issue Apr 11, 2018 · 3 comments
Closed

Configure base URL for API schema #146

machristie opened this issue Apr 11, 2018 · 3 comments

Comments

@machristie
Copy link
Collaborator

machristie commented Apr 11, 2018

Provide a setting in settings.py that can be specified with an environment variable that will set the base URL for the API schema. This is needed when the API is behind a proxying web server that changes its path prefix, for example, for launch.usegalaxy.org.

See http://www.django-rest-framework.org/api-guide/schemas/#url

See also CloudVE/cloudlaunch-cli#6 which depends on this issue.

See also DRF issue: https://github.com/encode/django-rest-framework/issues/5788. DRF doesn't take SCRIPT_NAME into account when generating schema urls, it's expected behavior however and one needs to pass the base URL to the schema generator.

@machristie
Copy link
Collaborator Author

@nuwang Is it okay if I work on this one, or have you already started on this?

@nuwang
Copy link
Member

nuwang commented Apr 13, 2018

@machristie Sure, that'd be great!

machristie added a commit to machristie/cloudlaunch that referenced this issue Apr 16, 2018
@machristie
Copy link
Collaborator Author

Tested this with following nginx.conf config running locally on my laptop:


# user  nginx;
worker_processes  1;

# error_log  /var/log/nginx/error.log warn;
# pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}



http {
    # include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    # access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    # include /etc/nginx/conf.d/*.conf;

    upstream cloudlaunch_server {
      # fail_timeout=0 means we always retry an upstream even if it failed
      # to return a good HTTP response (in case the Unicorn master nukes a
      # single worker for timing out).
      server 127.0.0.1:8000 fail_timeout=0;
    }
    
    server {
        listen *:8888;
        server_name localhost;
        
        location /cloudlaunch {
            # an HTTP header important enough to have its own Wikipedia entry:
            #   http://en.wikipedia.org/wiki/X-Forwarded-For
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header SCRIPT_NAME /cloudlaunch;

            # enable this if and only if you use HTTPS, this helps Rack
            # set the proper protocol for doing redirects:
            # proxy_set_header X-Forwarded-Proto https;

            # pass the Host: header from the client right along so redirects
            # can be set properly within the Rack application
            proxy_set_header Host $http_host;

            # we don't want nginx trying to do something clever with
            # redirects, we set the Host: header above already.
            proxy_redirect off;

            # set "proxy_buffering off" *only* for Rainbows! when doing
            # Comet/long-poll stuff.  It's also safe to set if you're
            # using only serving fast clients with Unicorn + nginx.
            # Otherwise you _want_ nginx to buffer responses to slow
            # clients, really.
            # proxy_buffering off;

            # Try to serve static files from nginx, no point in making an
            # *application* server like Unicorn/Rainbows! serve static files.

            if (!-f $request_filename) {
                proxy_pass http://cloudlaunch_server;
                break;
            }
        }
    }
}

Then I ran it with:

nginx -c $PWD/nginx/nginx-local.conf -g "daemon off;"

I then started cloudlaunch locally with

CLOUDLAUNCH_PATH_PREFIX='/cloudlaunch' python manage.py runserver

And now I get the desired effect when I browse the schema.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants