Verschiedene Typen in package types ausgelagert und Channels zum asynchronen Datentransport eingesetzt

This commit is contained in:
Jason Streifling 2023-10-05 19:52:11 +02:00
parent fcb509c9fe
commit 1bcfbfd325
4 changed files with 66 additions and 33 deletions

View File

@ -8,10 +8,17 @@ import (
"strings"
"syscall"
"streifling.com/jason/sicherheitsunterweisung/types"
"github.com/go-sql-driver/mysql"
"golang.org/x/term"
)
type DB struct {
*sql.DB
Name string
}
func getCredentials() (string, string, error) {
fmt.Printf("DB Benutzer: ")
user, err := bufio.NewReader(os.Stdin).ReadString('\n')
@ -30,7 +37,8 @@ func getCredentials() (string, string, error) {
return strings.TrimSpace(user), strings.TrimSpace(pass), nil
}
func OpenDB(dbName string) (*sql.DB, error) {
func OpenDB(dbName string) (*DB, error) {
var db *DB
var err error
cfg := mysql.NewConfig()
@ -40,7 +48,8 @@ func OpenDB(dbName string) (*sql.DB, error) {
return nil, fmt.Errorf("OpenDB: getCredentials(): %v\n", err)
}
db, err := sql.Open("mysql", cfg.FormatDSN())
db.Name = dbName
db.DB, err = sql.Open("mysql", cfg.FormatDSN())
if err != nil {
return nil, fmt.Errorf("newDB: sql.Open(\"mysql\", cfg.FormatDSN()): %v\n", err)
}
@ -50,3 +59,14 @@ func OpenDB(dbName string) (*sql.DB, error) {
return db, nil
}
func (db *DB) WriteBriefing(b *types.Briefing) error {
for i := 1; i < len(b.Participants); i++ {
_, err := db.Exec("INSERT INTO ? (instructor_first, instructor_last, date, time, state, location, participant_first, participant_last, company) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", db.Name, b.FirstName, b.LastName, b.Date, b.Time, b.State, b.Location, b.Participants[i].FirstName, b.Participants[i].LastName, b.Participants[i].Company)
if err != nil {
return fmt.Errorf("*DB.WriteBriefing: db.Exec(\"INSERT INTO ? (instructor_first, instructor_last, date, time, state, location, participant_first, participant_last, company) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)\", db.Name, b.FirstName, b.LastName, b.Date, b.Time, b.State, b.Location, b.Participants[i].FirstName, b.Participants[i].LastName, b.Participants[i].Company): %v\n", err)
}
}
return nil
}

18
main.go
View File

@ -3,26 +3,34 @@ package main
import (
"log"
"net/http"
// "streifling.com/jason/sicherheitsunterweisung/db"
"streifling.com/jason/sicherheitsunterweisung/db"
"streifling.com/jason/sicherheitsunterweisung/server"
"streifling.com/jason/sicherheitsunterweisung/types"
)
func main() {
var i, j int64
var b server.Briefing
i, j = 1, 1
mux := http.NewServeMux()
cb := make(chan *types.Briefing)
/* _, err := db.OpenDB("sicherheitsunterweisung")
db, err := db.OpenDB("sicherheitsunterweisung")
if err != nil {
log.Fatalln(err)
} */
}
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
mux.HandleFunc("/", server.DisplayForm(&i))
mux.HandleFunc("/add-participant/", server.AddParticipant(&i))
mux.HandleFunc("/submit/", server.SubmitForm(&b, &i, &j))
mux.HandleFunc("/submit/", server.SubmitForm(cb, &i, &j))
go func() {
for b := range cb {
db.WriteBriefing(b)
}
}()
log.Fatalln(http.ListenAndServe(":8080", mux))
}

View File

@ -5,30 +5,9 @@ import (
"html/template"
"log"
"net/http"
"streifling.com/jason/sicherheitsunterweisung/types"
)
type Person struct {
FirstName string
LastName string
}
type Instructor Person
type Participant struct {
ID int64
Person
Company string
}
type Briefing struct {
Instructor
Date string
Time string
State string
Location string
Participants []Participant
}
func DisplayForm(i *int64) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("templates/index.html")).Execute(w, i)
@ -42,8 +21,10 @@ func AddParticipant(i *int64) http.HandlerFunc {
}
}
func SubmitForm(b *Briefing, i, j *int64) http.HandlerFunc {
func SubmitForm(ch chan<- *types.Briefing, i, j *int64) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var b types.Briefing
b.FirstName = r.PostFormValue("instructor-first")
b.LastName = r.PostFormValue("instructor-last")
b.Date = r.PostFormValue("date")
@ -52,9 +33,9 @@ func SubmitForm(b *Briefing, i, j *int64) http.HandlerFunc {
b.Location = r.PostFormValue("location")
for ; *j <= *i; *j++ {
b.Participants = append(b.Participants, Participant{
b.Participants = append(b.Participants, types.Participant{
ID: *j,
Person: Person{
Person: types.Person{
FirstName: r.PostFormValue("participant-first-" + fmt.Sprint(*j)),
LastName: r.PostFormValue(("participant-last-" + fmt.Sprint(*j))),
},
@ -62,6 +43,7 @@ func SubmitForm(b *Briefing, i, j *int64) http.HandlerFunc {
})
}
log.Println("Received ", *b)
log.Println(b)
ch <- &b
}
}

23
types/types.go Normal file
View File

@ -0,0 +1,23 @@
package types
type Person struct {
FirstName string
LastName string
}
type Instructor Person
type Participant struct {
ID int64
Person
Company string
}
type Briefing struct {
Instructor
Date string
Time string
State string
Location string
Participants []Participant
}