Use CLI flags in addition to env vars for config
This commit is contained in:
parent
041df160a9
commit
09a6698d26
3 changed files with 94 additions and 20 deletions
|
@ -8,28 +8,79 @@ import (
|
|||
"os"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
_ "github.com/go-sql-driver/mysql"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
func getEnvOrDefault(env string, defaultVal string) string {
|
||||
envVal := os.Getenv(env)
|
||||
if envVal == "" {
|
||||
return defaultVal
|
||||
} else {
|
||||
return envVal
|
||||
}
|
||||
}
|
||||
const ARG_PORT = "port"
|
||||
const ARG_DB_NAME = "db-name"
|
||||
const ARG_DB_USER = "db-user"
|
||||
const ARG_DB_PASS = "db-pass"
|
||||
const ARG_DB_HOST = "db-host"
|
||||
const ARG_DB_PORT = "db-port"
|
||||
|
||||
func main() {
|
||||
dbName := getEnvOrDefault("TWIGS_DB_NAME", "budget")
|
||||
dbUser := getEnvOrDefault("TWIGS_DB_USER", "budget")
|
||||
dbPass := getEnvOrDefault("TWIGS_DB_PASS", "budget")
|
||||
dbHost := getEnvOrDefault("TWIGS_DB_HOST", "localhost")
|
||||
dbPort := getEnvOrDefault("TWIGS_DB_PORT", "3306")
|
||||
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@%s:%s/%s", dbUser, dbPass, dbHost, dbPort, dbName))
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to database %s on %s", dbName, dbHost)
|
||||
app := &cli.App{
|
||||
Name: "twigs",
|
||||
Usage: "personal/family finance app server",
|
||||
Flags: []cli.Flag{
|
||||
&cli.IntFlag{
|
||||
Name: ARG_PORT,
|
||||
Value: 8080,
|
||||
Usage: "the `PORT` for the server to listen on",
|
||||
Aliases: []string{"p"},
|
||||
EnvVars: []string{"TWIGS_PORT"},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: ARG_DB_NAME,
|
||||
Value: "twigs",
|
||||
Usage: "the `NAME` of the database to connect to",
|
||||
EnvVars: []string{"TWIGS_DB_NAME"},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: ARG_DB_USER,
|
||||
Value: "twigs",
|
||||
Usage: "the `USERNAME` to use to connect to the database",
|
||||
EnvVars: []string{"TWIGS_DB_USER"},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: ARG_DB_PASS,
|
||||
Value: "twigs",
|
||||
Usage: "the `PASSWORD` to use to connect to the database",
|
||||
EnvVars: []string{"TWIGS_DB_PASS"},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: ARG_DB_HOST,
|
||||
Value: "twigs",
|
||||
Usage: "the `HOST` to use to connect to the database",
|
||||
EnvVars: []string{"TWIGS_DB_HOST"},
|
||||
},
|
||||
&cli.IntFlag{
|
||||
Name: ARG_DB_PORT,
|
||||
Value: 5432,
|
||||
Usage: "the `PORT` to use to connect to the database",
|
||||
EnvVars: []string{"TWIGS_DB_PORT"},
|
||||
},
|
||||
},
|
||||
Action: func(c *cli.Context) error {
|
||||
_, err := sql.Open("postgres", fmt.Sprintf(
|
||||
"%s:%s@%s:%d/%s",
|
||||
c.String(ARG_DB_USER),
|
||||
c.String(ARG_DB_PASS),
|
||||
c.String(ARG_DB_HOST),
|
||||
c.Int(ARG_DB_PORT),
|
||||
c.String(ARG_DB_NAME),
|
||||
))
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to connect to database %s on %s", c.String(ARG_DB_NAME), c.String(ARG_DB_HOST))
|
||||
}
|
||||
_ = mux.NewRouter()
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
return nil
|
||||
},
|
||||
}
|
||||
router := mux.NewRouter()
|
||||
log.Fatal(http.ListenAndServe(":8080", nil))
|
||||
}
|
||||
err := app.Run(os.Args)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
|
6
go.mod
6
go.mod
|
@ -1,3 +1,9 @@
|
|||
module wbrawner.com/twigs
|
||||
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
github.com/gorilla/mux v1.8.0
|
||||
github.com/lib/pq v1.10.6
|
||||
github.com/urfave/cli/v2 v2.8.0
|
||||
)
|
||||
|
|
17
go.sum
Normal file
17
go.sum
Normal file
|
@ -0,0 +1,17 @@
|
|||
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||
github.com/antzucaro/matchr v0.0.0-20210222213004-b04723ef80f0 h1:R/qAiUxFT3mNgQaNqJe0IVznjKRNm23ohAIh9lgtlzc=
|
||||
github.com/antzucaro/matchr v0.0.0-20210222213004-b04723ef80f0/go.mod h1:v3ZDlfVAL1OrkKHbGSFFK60k0/7hruHPDq2XMs9Gu6U=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
|
||||
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
|
||||
github.com/lib/pq v1.10.6 h1:jbk+ZieJ0D7EVGJYpL9QTz7/YW6UHbmdnZWYyK5cdBs=
|
||||
github.com/lib/pq v1.10.6/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/urfave/cli/v2 v2.8.0 h1:EZsAB20oRW4nHcB99TTL6PrXpBGIEujMEKdjwruY9KQ=
|
||||
github.com/urfave/cli/v2 v2.8.0/go.mod h1:TYFbtzt/azQoJOrGH5mDfZtS0jIkl/OeFwlRWPR9KRM=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
Loading…
Reference in a new issue