This is the service implementation for the api for hello world
- Implementation via Cobra
- Configuration via Viper
- Error handling via the Catalyst Platform Utils library which includes support for sentry
- Utilities from catalyst squad such as logging via logrus
- Command configuration validation via Go validator
- Basic skaffold.yaml included
- Basic non-root Dockerfile included
- Github workflows for release and image uploading included
You can do it manually by adding a new .go file in the cmd
directory, but I'd recommend using the cobra cli generator
- Install the generator globally with
go install github.com/spf13/cobra-cli@latest
- Run
cobra-cli add myCommand
. This will generate a new go file and the skeleton for your new command. - Add your flags if any in the
init()
method. See cmd/example.go for an example - Add your config struct with validators
- Implement your run function. If you have config, make sure you call
GetConfigFromViper(config)
in the run function, it will not work in theinit()
function. - See
cmd/example.go
for an example
Viper's global config is configured in cmd/root.go#initConfig()
. We're using it to attempt to read configuration files as well as calling viper.AutomaticEnv()
which will bind environment variables to flags when viper.BindPFlags(flags)
is called
Binding flags should be done in each command's init()
function. Calling viper.BindPFlags(flags)
binds whatever configuration viper has found to viper. That could be a .yaml file, or env vars, or cli flags like --my_flag, this call makes viper aware of the flags and configs.
Simplicity. If your flag names are all strings separated with _
then you can simply use an identically named environment variable to set the setting. If you use -
then there's other viper config and trickery you have to do to get it to read the env vars correctly. We don't believe it's worth the extra stuff, just use _
and it works well out of the box.
If you need more help, create an issue, or reference the more detailed linked documentation to all the components comprising this app
The easiest way to run locally is with skaffold. Have a local cluster of your choice and run:
skaffold dev --tolerate-failures-until-deadline=true
If you want to run it locally without that, you just need to have the DATABASE_URI env var set appropriately (see tests for a testing example) and a postgres database running somewhere at that URI