Endpoint url in dev vs prod

Is there any good way to dynamically get the correct url for the endpoint, e.g. localhost vs url in production, so I don’t have to manually change it before deploying? My current thought is to send it as a flag when I init Main.elm, but I don’t feel that is the best option. Is there a better way?

If you’re serving your app from the same endpoint you should be able to use relative urls.

I’ve used either a Config.elm file, or a config.js file loaded and sent to init. Typically this is not checked into source control, with an example file provided.

that’s what I do :see_no_evil:

I tend to define a Config record and pass it round the application where it is needed:

type alias Config =
    { mainApiRoot : String
    , searchApiRoot : String
    , otherApiRoot : String
    , whateverParams : ...
    }

For quick start during initial development, you can just hard code it:

config =
    { mainApiRoot = http://localhost:8000/api, 
    ...
    }

At least that way you have the config built into the application from scratch. As you evolve more towards production, pass in appropriate prod/test/dev versions as flags.

Better than using flags: Use the URL provided by Browser.application.init to pick the right URL for the endpoint.
You can check if that URL is on localhost and decide the endpoint URL based on that.
This approach supports more diverse scenarios than relative URLs. For example, it allows to serve the frontend from a different host in production.

2 Likes

We have config files for each environment, sharing the same function signatures. Our build script chooses the right one and creates a symlink to it. This is inspired by https://github.com/iosphere/elm-i18n. Then, we can import the config module (which is a symlink to either of the actual implementations) in every file where environment-specific config is needed. Highly recommended!

1 Like

I personnaly use this repo https://github.com/simonh1000/elm-webpack-starter and added a webpack.EnvironmentPlugin along with dotenv and inject the host through flags to an AppConfig type. Since I’m building the app to a Docker container in production, I can easily infer the same variable but with a production value in build process.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.