diff --git a/cmd/data/db.go b/cmd/data/db.go new file mode 100644 index 0000000..5b09579 --- /dev/null +++ b/cmd/data/db.go @@ -0,0 +1,34 @@ +package data + +import ( + "database/sql" + "fmt" + + "github.com/go-sql-driver/mysql" +) + +type DB struct { + *sql.DB +} + +func OpenDB(dbName string) (*DB, error) { + var err error + db := DB{DB: &sql.DB{}} + + cfg := mysql.NewConfig() + cfg.DBName = dbName + cfg.User, cfg.Passwd, err = getCredentials() + if err != nil { + return nil, fmt.Errorf("error: user credentials for DB could not be read: %v", err) + } + + db.DB, err = sql.Open("mysql", cfg.FormatDSN()) + if err != nil { + return nil, fmt.Errorf("error: DB could not be opened: %v", err) + } + if err = db.Ping(); err != nil { + return nil, fmt.Errorf("error: DB could not be reached: %v", err) + } + + return &db, nil +} diff --git a/cmd/data/helpers.go b/cmd/data/helpers.go new file mode 100644 index 0000000..028fd2a --- /dev/null +++ b/cmd/data/helpers.go @@ -0,0 +1,52 @@ +package data + +import ( + "bufio" + "fmt" + "os" + "strings" + "syscall" + + "golang.org/x/term" +) + +func getUsername() (string, error) { + user := os.Getenv("DB_USER") + if user == "" { + var err error + fmt.Printf("DB Benutzer: ") + user, err = bufio.NewReader(os.Stdin).ReadString('\n') + if err != nil { + return fmt.Errorf("error: cmd/data/helpers.go getUsername bufio.NewReader().ReadString(): %v", err) + } + } + return strings.TrimSpace(user), nil +} + +func getPassword() (string, error) { + pass := os.Getenv("DB_PASS") + if pass == "" { + fmt.Printf("DB Passwort: ") + bytePass, err := term.ReadPassword(int(syscall.Stdin)) + if err != nil { + return fmt.Errorf("error: cmd/data/helpers.go getPassword term.ReadPassword(): %v", err) + } + fmt.Println() + pass = strings.TrimSpace(string(bytePass)) + } + return pass, nil +} + +func getCredentials() (string, string, error) { + user, err := getUsername() + if err != nil { + return "", "", fmt.Errorf("error: cmd/data/helpers.go getCredentials getUsername(): %v", err) + } + + pass, err := getPassword() + if err != nil { + return "", "", fmt.Errorf("error: cmd/data/helpers.go getCredentials getPassword(): %v", err) + } + + return user, pass, nil +} diff --git a/go.mod b/go.mod index 195a33f..f2966ab 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,10 @@ module streifling.com/jason/cpolis go 1.22.0 require ( + github.com/go-sql-driver/mysql v1.7.1 github.com/gorilla/feeds v1.1.2 github.com/yuin/goldmark v1.7.0 + golang.org/x/term v0.17.0 ) + +require golang.org/x/sys v0.17.0 // indirect diff --git a/go.sum b/go.sum index 4a42e4c..a62d835 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= +github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/gorilla/feeds v1.1.2 h1:pxzZ5PD3RJdhFH2FsJJ4x6PqMqbgFk1+Vez4XWBW8Iw= github.com/gorilla/feeds v1.1.2/go.mod h1:WMib8uJP3BbY+X8Szd1rA5Pzhdfh+HCCAYT2z7Fza6Y= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= @@ -8,3 +10,7 @@ github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZV github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/yuin/goldmark v1.7.0 h1:EfOIvIMZIzHdB/R/zVrikYLPPwJlfMcNczJFMs1m6sA= github.com/yuin/goldmark v1.7.0/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= +golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y= +golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U= +golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= diff --git a/main.go b/main.go index 793d888..ccc938f 100644 --- a/main.go +++ b/main.go @@ -5,11 +5,17 @@ import ( "log" "net/http" + "streifling.com/jason/cpolis/cmd/data" "streifling.com/jason/cpolis/cmd/feed" "streifling.com/jason/cpolis/cmd/handlers" ) func main() { + db, err := data.OpenDB("cpolis") + if err != nil { + log.Fatalln(err) + } + f, err := feed.OpenFeed("tmp/rss.gob") if err != nil { log.Println(err)