Do I understand correctly that to pass a parameter to a :start lifecycle function I need to specify it inside another defstate? or have the parameters definied in the namespace? no way to pass the argument to the function directly though?
@aleksander is it a command line argument?
no, just a configuration value
a port on which the web server is supposed to listen on
ideally I would like to pass that value when starting
which component starts the app? i.e. is it -main
usually you would create a config
state which reads a config file or reaches out to external configuration service (i.e. consul, etcd, ..)
this state becomes a map with all the application configuration
then in other states that need config, such as port, you would do something similar to (config :port)
here is an example: https://github.com/tolitius/hubble
it reads config from consul: https://github.com/tolitius/hubble/blob/master/src/clj/hubble/env.clj#L30 but it could also simply read it from a local config file, since it just becomes a map when started
and here it reads a server port to start a server state: https://github.com/tolitius/hubble/blob/master/src/clj/hubble/server.clj#L44-L49
here is an example of reading a config from a file into a state: https://github.com/tolitius/stater/blob/master/smsio/src/app/conf.clj
and then starting a server with it: https://github.com/tolitius/stater/blob/master/smsio/src/app/www.clj#L20-L27
Thanks, I'll go through the examples tomorrow!
yes, main I was thinking of reading the configuration outside of a mount component
@aleksander
> I was thinking of reading the configuration outside of a mount component
if you really do not want to have a config component (why?) instead of (mount/start)
you could use (mount/start-with-args args)
where args is a map with arguments
I wanted to explicitly pass the port to the web server component as a parameter, not having it read from other component inside the :start function
but that doesn't seem to be mount way
you wouldn't/can't pass parameters to :start functions
the motivation would be to easier see the dependencies
> the motivation would be to easier see the dependencies
since mount start components in the order they are "used", you would always see config
as a dependency:
dev=> (require '[mount.tools.graph :as graph])
dev=> (graph/states-with-deps)
({:name "#'app.conf/config",
:order 1,
:status #{:started},
:deps #{}}
{:name "#'app.db/conn",
:order 2,
:status #{:started},
:deps #{"#'app.conf/config"}}
{:name "#'app.www/nyse-app",
:order 3,
:status #{:started},
:deps #{"#'app.conf/config"}}
{:name "#'app.example/nrepl",
:order 4,
:status #{:started},
:deps #{"#'app.www/nyse-app" "#'app.conf/config"}})
later anywhere in state / functions you can access it with (mount/args)
: i.e.
(get-in (mount/args) [:server :port])