commit 3936ee1ef8d92d90272cc13e14035cf9a3bb9c85 Author: William Brawner Date: Thu Apr 8 17:22:34 2021 -0700 Initial commit Signed-off-by: William Brawner diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d1dcd00 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module wbrawner.com/twigs + +go 1.15 + +require github.com/go-sql-driver/mysql v1.6.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..20c16d6 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= +github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= diff --git a/server.go b/server.go new file mode 100644 index 0000000..5c02c7e --- /dev/null +++ b/server.go @@ -0,0 +1,34 @@ +package twigs + +import ( + "database/sql" + "fmt" + "log" + "net/http" + "os" + + _ "github.com/go-sql-driver/mysql" +) + +func getEnvOrDefault(env string, defaultVal string) string { + envVal := os.Getenv(env) + if envVal == "" { + return defaultVal + } else { + return envVal + } +} + +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) + } + http.HandleFunc("/users", user) + log.Fatal(http.ListenAndServe(":8080", nil)) +}