0

I'm building an app hosted on a kubernetes cluster. My app serves a javascript file to the clients. The file contains something like this:

const URL = "https://fancy.com";

async function doSomething() {
    // ...Do something here...
    let res = await fetch(`${URL}/v1/pizza?${params}`);
    return await res.json();
}

The javascript file is embedded in a Docker image, and the backend (written in Go with Gin) simply sends it to the client whenever the client accesses the index page. The backend is behind a ningx load balancer.

Now, I'm building a dev environment and obviously the javascript above won't work without modification. It needs to contain:

const URL = "https://dev.fancy.com";

Is there a good way to serve the right javascript (ie: with the right url) without building a different image or duplicating the javascript code? I'm mostly a backend dev so sorry if this question is naive.

JPFrancoia
  • 4,866
  • 10
  • 43
  • 73
  • can you server serve the JS files if i pass the Domain as a variable ? or while building it need a domain to create the artifacts sorry not sure actual case. By any chance you could pass the domain as Env var based on the environment as it will work ? build docker image as general scenario and docker expect the Variable which will set the domain. – Harsh Manvar Mar 12 '23 at 19:40
  • https://stackoverflow.com/a/68874510/5525824 – Harsh Manvar Mar 12 '23 at 19:52
  • Which back-end framework are you using? Express? – underflow Mar 12 '23 at 20:33
  • It's in Go, nothing related to javascript :) And the javascript I'm serving is vanilla javascript. – JPFrancoia Mar 12 '23 at 21:02
  • You can get the url of the client who requested the page – underflow Mar 12 '23 at 21:48
  • Search online for origin GET request for example. It appears sometimes as a header in the request – underflow Mar 12 '23 at 21:49
  • Something like: ```const { host, hostname, href, origin, pathname, port, protocol, search } = window.location const URL = origin; ``` – JPFrancoia Mar 12 '23 at 21:54
  • You know the correct answer to your question is to use environment variables instead of using code embedded in the image. Everything else is a hack solution. The simplest hack solution would be to use an environment variable that allows your go code to serve an arbitrary file, defaults to the one in the image. Then just copy and paste the content of the JS file in the image to a local file in dev, and have the environment variable point to your local file – Adam Jenkins Mar 12 '23 at 23:28
  • We agree on the ideal solution (I'm just not there yet). The JS code of my previous comment does work (just tested it). I have no idea how robust/safe it is though. The problem I see with your suggestion of serving an arbitrary JS file from the backend: I'll have to duplicate the JS code and change one line. One day I'll forget to port the changes from dev to prod and they'll get out of sync. – JPFrancoia Mar 12 '23 at 23:47
  • Having Dev and Prod environments at the same place is not recommended from a security standpoint because in dev we will be doing all kinds of experiments and if there is any vulnerability it might compromise your production environment as well. – Kranthiveer Dontineni Mar 13 '23 at 09:42
  • So I suggest you to implement DevOps pipelines using something like git and some other software for CICD and instead of directly making changes in the servers make changes to your code and push it to local git and then by using CICD deploy the code. So for each commit you can have a comment using which you can record the changes made and you will be having a better understanding and record of the changes done, you can even rollback the changes with a single click if something went wrong. – Kranthiveer Dontineni Mar 13 '23 at 09:42
  • You can configure nginx for your applications and it will help in routing the requests to respective endpoints. Nginx rewrite helps you in achieving this. – Kranthiveer Dontineni Mar 13 '23 at 09:42

0 Answers0