aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md12
-rw-r--r--cmd/main/main.go22
-rw-r--r--config/config.go12
-rw-r--r--factory/factory.go22
4 files changed, 41 insertions, 27 deletions
diff --git a/README.md b/README.md
index 78052d6..86dc307 100644
--- a/README.md
+++ b/README.md
@@ -23,5 +23,13 @@ also do `make lint` to get a linting / critique of all the source
files (`lint` runs `fmt` first, so you need to have installed the
packages above as well).
-Note that the `Makefile` is *for development*, it's not used on
-production machines.
+Note that the `Makefile` is *for development*, it's not used after the
+code is built.
+
+## Structure
+
+* the entry points go under `cmd/`, with a matching target in the
+ `Makefile`
+* the `factory` constructs all the singletons that actually do things
+ (e.g. listeners, models, clients, business logic objects)
+* the `config` handles all configuration for the entire application
diff --git a/cmd/main/main.go b/cmd/main/main.go
index ac7f788..7ad978b 100644
--- a/cmd/main/main.go
+++ b/cmd/main/main.go
@@ -1,33 +1,17 @@
package main
import (
- "fmt"
- "os"
-
- "www.thenautilus.net/cgit/go-example/config"
factorypkg "www.thenautilus.net/cgit/go-example/factory"
- "www.thenautilus.net/cgit/go-example/logging"
)
func main() {
- // this is the main binary, so the config file is in
- // the same directory as the executable
- config, err := config.GetMainConfig(".")
- if err != nil {
- fmt.Fprintf(os.Stderr, "%s\n", err)
- os.Exit(1)
- }
-
- log := logging.Logger(config.Logger)
-
- log.Info().Object("config", &config).Msg("configuration")
-
- factory := factorypkg.New(log, &config)
+ factory := factorypkg.NewFromConfig()
something := factory.Something()
- err = something.DoSomething()
+ err := something.DoSomething()
if err != nil {
+ log := factory.Logger()
log.Error().Err(err).Msg("Can't do the thing")
}
}
diff --git a/config/config.go b/config/config.go
index 3c93d53..c28fa14 100644
--- a/config/config.go
+++ b/config/config.go
@@ -87,10 +87,10 @@ func addConfigFromFile(configRelPath string) error {
return nil
}
-func loadConfigFromFile(path string) (MainConfig, error) {
- pathFromEnv := os.Getenv("EXAMPLE_CONFIG")
- if pathFromEnv != "" {
- path = pathFromEnv
+func loadConfigFromFile() (MainConfig, error) {
+ path := os.Getenv("EXAMPLE_CONFIG")
+ if path == "" {
+ path = "."
}
err := addConfigFromFile(path)
@@ -115,12 +115,12 @@ func loadConfigFromFile(path string) (MainConfig, error) {
return config, nil
}
-func GetMainConfig(path string) (MainConfig, error) {
+func GetMainConfig() (MainConfig, error) {
addLoggerOptions()
addSomethingOptions()
pflag.Parse()
viper.BindPFlags(pflag.CommandLine) //nolint:errcheck
- return loadConfigFromFile(path)
+ return loadConfigFromFile()
}
diff --git a/factory/factory.go b/factory/factory.go
index ae84911..2acac39 100644
--- a/factory/factory.go
+++ b/factory/factory.go
@@ -1,9 +1,13 @@
package factory
import (
+ "fmt"
+ "os"
+
"github.com/rs/zerolog"
"www.thenautilus.net/cgit/go-example/config"
+ "www.thenautilus.net/cgit/go-example/logging"
"www.thenautilus.net/cgit/go-example/something"
)
@@ -21,6 +25,24 @@ func New(logger zerolog.Logger, config *config.MainConfig) Factory {
}
}
+func NewFromConfig() Factory {
+ config, err := config.GetMainConfig()
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "%s\n", err)
+ os.Exit(1)
+ }
+
+ log := logging.Logger(config.Logger)
+
+ log.Info().Object("config", &config).Msg("configuration")
+
+ return New(log, &config)
+}
+
+func (f *Factory) Logger() zerolog.Logger {
+ return f.logger
+}
+
func (f *Factory) Something() *something.Something {
if f.something == nil {
something := something.New(