Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Unable to get proxying to work #18

Closed
js1972 opened this issue Nov 18, 2015 · 8 comments
Closed

Unable to get proxying to work #18

js1972 opened this issue Nov 18, 2015 · 8 comments

Comments

@js1972
Copy link

js1972 commented Nov 18, 2015

Just wondering if you have an example Gruntfile using grunt-connect's proxy feature as I can't seem to get it to work.

For example.... When the app tries to load URL's from "/destinations/Northwind/v3/northwind/northwind.svc/" they should proxy to: "http://services.odata.orgv3/northwind/northwind.svc/"

Using the proxypath option as per the doco doesn't seem to engage the functionality...

@matz3
Copy link
Contributor

matz3 commented Nov 18, 2015

Yes, the current proxy does only work in a generic way. So you can't bind a specific path to a specific destination.

What you could currently do is to add a custom middleware and handle the requests on your own.

We are looking into a way to add this functionality, so I'll leave this one open.

@matz3 matz3 added the enhancement New feature or request label Nov 18, 2015
@js1972
Copy link
Author

js1972 commented Nov 19, 2015

okay will try the middleware. I've also used grunt-connect-proxy which works well but doesn't play with grunt-openui5 for some reason. Will try the middleware and update back here...

@js1972
Copy link
Author

js1972 commented Nov 19, 2015

ps. the point of this is so that you can git clone a WebIDE repository and immediately run it on your pc where the local proxy takes care of what the Destinations do on HCP.

@js1972
Copy link
Author

js1972 commented Nov 19, 2015

I got the local proxy working using grunt-connect-proxy. Its slightly changed since I've used it last. However it does not play well behind corporate firewall. ;-(

Here's a sample grunt file which shows how you can git clone a web ide app to your local machine and serve it with grunt serve while proxying the Northwind services to match what your destinations do in WebIDE:

/*eslint-env node*/
/*eslint-disable camelcase*/
module.exports = function(grunt) {
    "use strict";

    grunt.initConfig({

        dir: {
            webapp: "webapp",
            tests: "webapp/test",
            dist: "dist",
            bower_components: "bower_components",
            // Use http://localhost:8080/test-resources/testService.html to access test run
            localServerTestUrl: "http://localhost:8080/test-resources"
        },

        connect: {
            options: {
                port: 8080,
                // Listen on all network interfaces instead of just the local
                // loopback, so that this works within Docker containers.
                // set to -> hostname: "0.0.0.0",
                hostname: "*",
                livereload: 35729,
                middleware: function (connect, options, defaultMiddleware) {
                    var proxy = require("grunt-connect-proxy/lib/utils").proxyRequest;
                    return [
                        proxy
                    ].concat(defaultMiddleware);
                }
            },
            src: {},
            dist: {},
            proxies: [
                {
                    context: "/destinations/Northwind",
                    host: "services.odata.org",
                    headers: {
                        "host": "services.odata.org"
                    },
                    changeOrigin: true,
                    rewrite: {
                        "^/destinations/Northwind": ""
                    }
                }
            ]
        },

        openui5_connect: {
            options: {
                resources: [
                    //"../openui5/latest/resources"
                    "/Users/jason/code/openui5/latest/resources"
                    //"<%= dir.bower_components %>/openui5/src/sap.ui.core/src",
                    //"<%= dir.bower_components %>/openui5/src/sap.m/src",
                    //"<%= dir.bower_components %>/openui5/src/sap.ui.layout/src",
                    //"<%= dir.bower_components %>/openui5/src/themelib_sap_bluecrystal/src"
                ]
            },
            src: {
                options: {
                    appresources: ["<%= dir.webapp %>"],
                    testresources: ["<%= dir.tests %>"]
                }
            },
            dist: {
                options: {
                    appresources: "<%= dir.dist %>"
                }
            }
        },

        openui5_preload: {
            component: {
                options: {
                    resources: {
                        cwd: "<%= dir.webapp %>",
                        prefix: "sap/ui/demo/mdtemplate" //'todo'
                    },
                    dest: "<%= dir.dist %>"
                },
                components: true
            }
        },

        clean: {
            dist: "<%= dir.dist %>/"
        },

        copy: {
            dist: {
                files: [{
                    expand: true,
                    cwd: "<%= dir.webapp %>",
                    src: [
                        "**",
                        "!test/**"
                    ],
                    dest: "<%= dir.dist %>"
                }]
            }
        },

        eslint: {
            options: {
                quiet: false,
                configFile: "./.eslintrc"
            },

            all: ["<%= dir.tests %>", "<%= dir.webapp %>"],
            webapp: ["<%= dir.webapp %>"]
        },

        qunit: {
            options: {
                /* for debugging*/
                "--remote-debugger-autorun": "yes",
                "--remote-debugger-port": 8081
            },

            unit: {
                options: {
                    urls: [
                        "<%= dir.localServerTestUrl %>/unit/UnitTestsGrunt.qunit.html"
                    ]
                }

            },
            opa: {
                options: {
                    urls: [
                        "<%= dir.localServerTestUrl %>/opa/OpaTestsGrunt.qunit.html"
                    ],
                    // same as qunits timeout 90 seconds since opa test might take a while
                    timeout: 120000
                }

            }
        },

        watch: {
            webapp: {
                files: "<%= dir.webapp %>/**",
                tasks: ["eslint"]
            },
            livereload: {
                options: {
                    livereload: "<%= connect.options.livereload %>"
                },
                files: [
                    "<%= dir.webapp %>/**"
                ]
            }
        }

    });

    // These plugins provide necessary tasks.
    grunt.loadNpmTasks("grunt-contrib-connect");
    grunt.loadNpmTasks("grunt-contrib-clean");
    grunt.loadNpmTasks("grunt-contrib-copy");
    grunt.loadNpmTasks("grunt-openui5");
    grunt.loadNpmTasks("grunt-eslint");
    grunt.loadNpmTasks("grunt-contrib-qunit");
    grunt.loadNpmTasks("grunt-contrib-watch");
    grunt.loadNpmTasks("grunt-connect-proxy");

    // Server task
    grunt.registerTask("serve", function(target) {
        grunt.task.run("configureProxies:" + (target || "src"));
        grunt.task.run("openui5_connect:" + (target || "src") + ":keepalive");
    });

    // Server task - with live reloading
    grunt.registerTask('serveLive', function(target) {
        grunt.task.run("openui5_connect:" + (target || "src:livereload"));
        grunt.task.run("watch");
    });

    // Linting task
    grunt.registerTask("lint", ["eslint:all"]);

    // Build task
    grunt.registerTask("build", ["openui5_preload", "copy"]);

    // Test task
    grunt.registerTask("test", ["openui5_connect:src:keepalive", "qunit:unit",
        "qunit:opa"
    ]);
    grunt.registerTask("unitTest", ["openui5_connect:src:keepalive", "qunit:unit"]);
    grunt.registerTask("opaTest", ["openui5_connect:src:keepalive", "qunit:opa"]);

    // Default task
    grunt.registerTask("default", [
        "lint:all",
        "test"
    ]);
};

Note that I can no longer get the openui5 bower library to work anymore so switched to a local openui5 repository.

@matz3
Copy link
Contributor

matz3 commented Nov 25, 2015

Great to hear that you could get it working.
I don't quite understand why the bower libraries don't work for you, but I guess that's another issue.

@matz3
Copy link
Contributor

matz3 commented Nov 25, 2015

I closed this issue as consulting and opened a new issue for the enhancement request in the correct repo:
SAP-archive/connect-openui5#8

@chimurai
Copy link

chimurai commented Dec 2, 2015

You could try https://github.com/chimurai/http-proxy-middleware

It's build on top of http-proxy and should have similar features provided by grunt-connect-proxy.

Think the following recipes should answer some questions raised in this thread:

@matz3
Copy link
Contributor

matz3 commented Dec 15, 2015

Thanks @chimurai !
Looks very promising for a more advanced setup.

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

No branches or pull requests

3 participants