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

flag values are not resolved until server starts #148

Closed
eggsby opened this Issue Jun 3, 2014 · 3 comments

Comments

4 participants
@eggsby

eggsby commented Jun 3, 2014

Flags are only useable at server runtime. Even if they are available earlier (for example, from a system property).

In the example at:
http://finatra.info/docs/configuration.html

class MyServer extends FinatraServer {
  val thingFlag = flag("thingEnabled", true, "Is the thing enabled")

  if (thingFlag()) {
    println("thing is enabled")
  }
}

The value thingFlag will always be the default value true and never configurable at the command line. This is because the flags are parsed in the FinatraServer main method, which runs after the class definition. The only way to get around this behavior is to only use the flags after the FinatraServer main method runs. (unlike the provided example documentation).

An undesireable workaround to enable flags:

object MyServer extends FinatraServer {
  val thingFlag = flag("thingEnabled", true, "Is the thing enabled")

  override def main() {
    // only use flags here or later in the lifecycle of the application
    if (thingFlag()) {
      println("thing is enabled")
    }
    super.main()
  }
}

Or you can use flags inside of a route:

// java ... Example -thingEnabled=false
object Example extends FinatraServer {
  val thingFlag = flag("thingEnabled", true, "Is the thing enabled")

  class ExampleController extends Controller {
    get("/thing") { request =>
      // thingFlag configureable
      render.plain(thingFlag().toString()).toFuture
    }
  }

  register(new ExampleController)

  // always true
  if (thingFlag()) {
    println("thing is enabled")
  }

}

The intended use case is to use the flags to actually configure constants about the server, rather than configuring runtime behavior.

@quard8

This comment has been minimized.

quard8 commented Jul 17, 2014

Any updates?

@bdimmick

This comment has been minimized.

Contributor

bdimmick commented Jul 26, 2014

There's one other workaround which might be more acceptable:

class ExampleController(enabled: Boolean) extends Controller {
  get("/thing") { request => render.plain(enabled.toString()).toFuture }
}

object Example extends FinatraServer {
  val thingFlag = flag("thingEnabled", true, "Is the thing enabled")

  premain {
    register(new ExampleController(thingFlag()))
    if (thingFlag()) {
      println("thing is enabled")
    }
  }
}

The reason this works is because the premain block is run before the server starts but after the command-line flags are parsed.

@cacoco

This comment has been minimized.

Member

cacoco commented Jun 3, 2015

Resolved in Finatra v2.x

@cacoco cacoco closed this Jun 3, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment