ivpusic / neo

Go Web Framework
http://ivpusic.github.io/neo/
MIT License
420 stars 43 forks source link

improved configuration handling #9

Closed Netherdrake closed 9 years ago

Netherdrake commented 9 years ago

Me and my friend were hacking on neo, and we ran into an issue where setting a configuration file is really hard.

There are only 2 ways to do it in current version of neo.

1.) Injecting a fake argument into os.Args:

    neoConfigFlag := []string{"--config", util.RelativePath("config", "neo_config.toml")}
    os.Args = append(os.Args, neoConfigFlag...)

The problem with this approach is that if the app is a CLI application using a framework like codegangsta/cli, the injection has to be done at a later time or it will break cli. Also its pretty non-idiomatic.

2.) Manual Override:

    app := neo.App()
    defer app.Start()

    // config := &neo.Conf{}
    // config.Parse(util.RelativePath("config", "neo_config.toml"))
    // app.Conf = config

This has an issue that the logger will complain since the neo.App() will try to set "" config file. Also, 3 lines of code is kinda ugly.

This PR allows the user to set their config like so:

    app := neo.App()
    app.SetConfigFile(util.RelativePath("config", "neo_config.toml")) // $REPO/config/neo_config.toml
    defer app.Start()

Its just 1 line of code. Also, if this isn't set, it will fall-back to --config.

If you agree with the idea, I will look into further improving the code and testing it out. I literally hacked this PR in 10min @ 4am so it might break something.

Netherdrake commented 9 years ago

I broke the build, tests don't pass. However, please let me know if you're ok with the general idea.

ivpusic commented 9 years ago

Thank you for your contribution.

I like an idea. There are also two more ways to set configuration. Neo will by default look at file named config.toml in the same directory as main.go file, or you can override this by passing --config flag to neo CLI tool.

Just please fix the build :D, and add some tests that covers new feature.

And also can you please update configuration docs (http://ivpusic.github.io/neo/tutorials/2015/01/22/configuration.html) with the third way of setting configuration file. To update it should be really easy, just click on Improve this document at the bottom.

Netherdrake commented 9 years ago

Aight. I got an idea how to solve this problem in a clean fashion. WIll open a new PR when ready.