3

I have a simple go server that works, and gets most of its configuration settings from a toml file.
The current process involves restarting the go build source every time the settings are changed.
What is the correct/most preferred/tested and working way to ship only binary and the config.toml file?
I am still a newbie when it comes to compiling, and i have been reading a lot of texts and still not having a clear understanding on this issue.
Any useful comments will be appreciated.

Edwinner
  • 2,458
  • 1
  • 22
  • 33

1 Answers1

6

Config files aren't meant to be embedded in executables. It'd be better to have them reside alongside executables. Since I couldn't get your point on rebuilding complete app just for reloading configuration, I made up my former sentences presuming you're hardcoding.

If we get to the “reloading” topic, I would surely restart my program or send a signal to re-load the configuration. You don't have to do this, because there is a nice library doing this: https://github.com/spf13/viper. It is easy to use and supports live watching for changes on config file. Besides supporting JSON, YAML, TOML and HCL, it can read from environment variables, remote config systems (like Consul and etcd). It's trusted and used by big projects, such as Kubernetes.

Berkant İpek
  • 1,077
  • 12
  • 20
  • I actually use viper. I will be testing the live watch functionality today. – Edwinner Oct 14 '18 at 14:16
  • Perfect, it works now. I had to update the config after reload. Now I need it to work with the binary. – Edwinner Oct 14 '18 at 17:57
  • Actually, the assertion that "Config files aren't meant to be embedded in executables" is at best contentious. There are cases where this is a useful thing to do - see here: https://stackoverflow.com/questions/17796043/how-to-embed-files-into-golang-binaries – Graham Nicholls Sep 12 '19 at 20:20
  • @GrahamNicholls They are just not supposed to. Embedding static files to serve directly from the executable inside is acceptable. However configuration is different and does change even if it's not often. I believe they should be loaded at runtime for the best efficiency. There is an architectural pattern that tackles this. https://learn.microsoft.com/en-us/azure/architecture/patterns/external-configuration-store – Berkant İpek Sep 13 '19 at 09:36
  • We'll have to agree to disagree. Sometimes it's extremely useful to embed config. I'm sure that MS have all sorts of patterns, and in general, that they are worth following. Just not in every case. – Graham Nicholls Sep 19 '19 at 13:13
  • This was actually the correct answer. It is possible to get it into binary, but it works better alongside binary. I have been using it that way for 4 years. – Edwinner Nov 04 '22 at 11:45