0

I Wanted to deploy an web application on Heroku which has to be served using staticfilesfolder in WebSocketServer.ja

    port(getHerokuAssignedPort());
    webSocketIdleTimeoutMillis(1000000000);
    staticFileLocation("/public");
    webSocket("/em/", WebSocketHandler.class);
    init();

and websocketClient.js

var webSocket = new WebSocket("ws://" + location.hostname + ":" + location.port + "/em/");

But I was unable to server the *.html, *.css, *.js file from /public folder.It is working fine with my local system not working on heroku site.It shows application error.

Alper t. Turker
  • 34,230
  • 9
  • 83
  • 115
Amutheezan
  • 345
  • 1
  • 8
  • 24
  • 1
    It really depends on your WAR file structure. But I'd just make few experiments on the non-local deployment and then add `if (local) {...} else {...}` in my initialization code with the correct path for each. – SHG May 13 '18 at 23:21
  • 1
    yeah, I updated staticFileLocation with "/public" relative to target it building still it doesn't work.... – Amutheezan May 14 '18 at 04:55
  • 1
    Unrelated: check out https://stackoverflow.com/questions/10699492/bi-directional-map-in-java ... for that Bi map question. You were too quick deleting your question. Besides, the answer is **no**. For a bi map in java, you need two maps. – GhostCat May 14 '18 at 06:16
  • 1
    i checked those already, thats why I have asked that question, sorry for deleting as it reduces reputation to minus levels – Amutheezan May 14 '18 at 06:18
  • 1
    @SHG how to get the location of /public folder in Heroku, which is linked by github account – Amutheezan May 14 '18 at 09:58

1 Answers1

1
public class Bootstrap {

private static final int DEFAULT_PORT = 4567;

public static void main(String[] args) throws URISyntaxException {
     port(getHerokuAssignedPort());
     externalStaticFileLocation(getPublicFolderPath());
}

private static int getHerokuAssignedPort() {
    ProcessBuilder processBuilder = new ProcessBuilder();
    if (processBuilder.environment().get("PORT") != null) {
        return Integer.parseInt(processBuilder.environment().get("PORT"));
    }
    return DEFAULT_PORT;
}

private static String getPublicFolderPath() throws URISyntaxException{
    CodeSource codeSource = Bootstrap.class.getProtectionDomain().getCodeSource();
    File jarFile = new File(codeSource.getLocation().toURI().getPath());
    String jarDir = jarFile.getParentFile().getPath();
    return jarDir + File.separator + "public";
}}

/public directory should be in the same folder as build jar file.

Andrew
  • 479
  • 5
  • 15
  • are there anyways to access database from another server, while using heroku (means without using Herokudb) – Amutheezan May 21 '18 at 08:53