config für datenbankwerte angelegt und dessen lesen ermöglicht

This commit is contained in:
xoy 2023-01-28 19:00:25 +01:00
parent d39248f77b
commit dbf038fd15
4 changed files with 50 additions and 1 deletions

5
config/database.yml Normal file
View File

@ -0,0 +1,5 @@
username: root
password:
address: localhost
port: 3306
database: events

View File

@ -1,7 +1,9 @@
package main package main
func getEvents() []event { func getEvents() []event {
db := dbConnect("root", "", "localhost", "3306", "ctdo") dbValues := readDatabaseYML()
db := dbConnect(dbValues.username, dbValues.password, dbValues.address, dbValues.port, dbValues.database)
rows := dbQuerry(db, "SELECT * FROM events;") rows := dbQuerry(db, "SELECT * FROM events;")

34
func.go
View File

@ -206,3 +206,37 @@ func getNextTopic() topic {
return output return output
} }
func readDatabaseYML() database {
file := fileRead("./config/database.yml")
rows := [][]string{}
for _, row := range strings.Split(file, "\n") {
rows = append(rows, strings.Split(row, ": "))
}
output := new(database)
for i, row := range rows {
switch i {
case 0:
output.username = row[1]
break
case 1:
output.password = row[1]
break
case 2:
output.address = row[1]
break
case 3:
output.port = row[1]
break
case 4:
output.database = row[1]
break
}
}
return *output
}

View File

@ -22,3 +22,11 @@ type topic struct {
date string date string
days int days int
} }
type database struct {
username string
password string
address string
port string
database string
}