vorner / spirit

Apache License 2.0
162 stars 6 forks source link

Hyper with non-config settings #40

Closed jamessewell closed 3 years ago

jamessewell commented 5 years ago

Hi,

I'm trying to work out how to bend the spirit-hyper stuff to use settings from opts, and not from config and can't seem to make it work - is this something that is supported?

To start with I just want to listen on a port specified by --port. I have the structops setup for this - just can't work out a nice way of getting to to spirit_hyper.

I have my app config in config, so it's still there just used for rendering the content.

vorner commented 5 years ago

Hello

Not directly right now. The options I see are:

.with(Pipeline::new("http server")
    .extract(|opts, cfg| {
        let mut server = cfg.server.clone();
        server.transport.listen.port = opts.port;
        server
    })
    .transform(BuildServer …)

However, the problem with the latter is that most of the fields are private. I've wanted to make them public for some time now, exactly for reasons like this, but didn't get around to it yet. If you want to send a pull request, I'll make sure to release a new version fast. Just make sure every structure has at least one private field (_dummy: () is enough) to make sure users can't list all the field in a constructor and the ability to add more fields in the future is not lost.

jamessewell commented 5 years ago

Ah, that solves the problem of how to get a port into the pipeline.

I still need to make a Server without using Config or config_defaults though.

Is there a way to make a server from a Struct directly (without using defaults or the config being passed through)?

Cheers,

On Mon, Jun 3, 2019 at 5:28 PM vorner notifications@github.com wrote:

Hello

Not directly right now. The options I see are:

  • Provide the configuration as -C server.port=1234. But I'm afraid this currently doesn't work properly due to a bug I didn't have time to fix (#36 https://github.com/vorner/spirit/issues/36).
  • Build a pipeline that modifies the HttpServer structure. Eg. something like this (typing from memory, so it likely won't compile directly).

.with(Pipeline::new("http server")

.extract(|opts, cfg| {

    let mut server = cfg.server.clone();

    server.transport.listen.port = opts.port;

    server

})

.transform(BuildServer …)

However, the problem with the latter is that most of the fields are private. I've wanted to make them public for some time now, exactly for reasons like this, but didn't get around to it yet. If you want to send a pull request, I'll make sure to release a new version fast. Just make sure every structure has at least one private field (_dummy: () is enough) to make sure users can't list all the field in a constructor and the ability to add more fields in the future is not lost.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/vorner/spirit/issues/40?email_source=notifications&email_token=AAJJDI4OC2AVUT2JDCMF5MLPYTB2DA5CNFSM4HSESTSKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWYRI4Q#issuecomment-498144370, or mute the thread https://github.com/notifications/unsubscribe-auth/AAJJDI5CO7IXOFQUDBHK3B3PYTB2DANCNFSM4HSESTSA .

vorner commented 5 years ago

Is there a way to make a server from a Struct directly (without using defaults or the config being passed through)?

I'm not sure I understand what you ask. So, no part of Server is in the configuration file (like connection limits)? If so, you can always do something like:

let mut server = HyperServer::default();
server.transport.listen.port = whatever_port;
server

However, doing this as part of a pipeline (or even using spirit-hyper for that) might be a little bit of an overkill. Most of the complexity is around being able to change the configuration at runtime, shut down the server on old ports and start it (them, you can have multiple) on another one. If you pass the port on command line, they never change and you may have easier time just firing off hyper directly.

jamessewell commented 5 years ago

Fair enough! I've got it working without a Pipeline - just wanted to try with one.

Will close this as it works :)

vorner commented 5 years ago

I'll reopen it, if you don't mind. I mean, even if it wasn't what you actually wanted, being able to augment the port or something from command line seems valuable, so I'll keep it here to remind me (or let anyone to tackle it).

vorner commented 3 years ago

I think now the fields are public it is enough, one can augment it during the pipeline.