Added initial support for MySQL databases

This commit is contained in:
Jason Streifling 2024-02-18 16:37:13 +01:00
parent 2ccc9c7397
commit 5d41543543
5 changed files with 102 additions and 0 deletions

34
cmd/data/db.go Normal file
View File

@ -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
}

52
cmd/data/helpers.go Normal file
View File

@ -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
}

4
go.mod
View File

@ -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

6
go.sum
View File

@ -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=

View File

@ -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)