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

Include environment variables in lustre bundle #61

Open
dinkelspiel opened this issue Jul 29, 2024 · 1 comment
Open

Include environment variables in lustre bundle #61

dinkelspiel opened this issue Jul 29, 2024 · 1 comment
Assignees

Comments

@dinkelspiel
Copy link
Contributor

Feature request for including environment variables in lustre bundles. Useful for changing behaviors or values between development and production etc.

@giacomocavalieri giacomocavalieri self-assigned this Jul 29, 2024
@andriishupta
Copy link

Hello, I wanted to get env variables to SPA(like API_URL) and found a discussion on Discord

looking that quite some time has passed since this ticket was created, I wonder how this PROPOSAL would fit Gleam's language design:

  1. populate env variables
  • create a new template in lustre_dev/priv/template for env variables
  • in build.gleam inside do_app create a new populated file from cli.template("env.mjs")
    inside env.mjs, we would need to add an Object with all populated env values and some code(could be extended with get_or)
export function lustre_env() {
  // populated
  const vals = {
    ...
  }
  return {
    get(key) {
      return vals[key] ? Ok(vals[key]) : Error(null)
    },
    get_or() {}
  }
}

Inside this file we could automatically add LUSTRE_PUBLIC_* and additionally, populate flags from --env: gleam run -m lustre/dev build --minify --outdir=../server/priv/static --env=ADDITIONAL_ENV_VAR=true,ANOTHER=example

  • add lustre_env() to entry-with-main.mjs inside main(..): context({ env })
  1. lustre/context, simplified
pub type LustreEnv {
  LustreEnv(get: fn(String) -> Result(String, Nil))
}

pub type LustreContext { // in case you want to add something more than `env`
  LustreContext(env: LustreEnv)
}
  1. usage
import lustre/context.{LustreContext, LustreEnv}

pub fn main(context: Option(LustreContext)) {
  let assert Some(context) = context
  let env = context.env
  let api_url_result = env.get("LUSTRE_PUBLIC_API_URL")
   ...
   
  let flags =
    dict.new()
    |> dict.insert("api_url", result.unwrap(api_url_result, "http://api:3030")

  let app = lustre.application(init, update, view)
  let assert Ok(_) = lustre.start(app, "#app", flags)

with this design, we won't use the generation of gleam files

ANOTHER option I was thinking about, which is worse, is to use esbuild --define=[...] that will replace env variables inline in the generated code
in Gleam it could also be called with something like lustre_env("LUSTRE_PUBLIC_API") that will be replaced in JS with the value

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

3 participants