3 Commits

Author SHA1 Message Date
fdf68adb0d getCredentials() und OpenDB() zum öffnen einer sql.DB erstellt 2023-10-05 17:42:47 +02:00
ea0fdee0e0 i und j sollten jetzt korrekt verwendet werden 2023-10-05 17:01:49 +02:00
61c895d53f Eingabemaske funktioniert soweit
i und j sollten jetzt richtig verwendet werden
2023-10-05 17:01:24 +02:00
6 changed files with 110 additions and 9 deletions

52
db.go Normal file
View File

@ -0,0 +1,52 @@
package sicherheitsunterweisung
import (
"bufio"
"database/sql"
"fmt"
"os"
"strings"
"syscall"
"github.com/go-sql-driver/mysql"
"golang.org/x/term"
)
func getCredentials() (string, string, error) {
fmt.Printf("DB Benutzer: ")
user, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
return "", "", fmt.Errorf("getCredentials: bufio.NewReader(os.Stdin).ReadString('\n'): %v", err)
}
fmt.Printf("DB Passwort: ")
bytePass, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", "", fmt.Errorf("getCredentials: term.ReadPassword(int(syscall.Stdin)): %v", err)
}
fmt.Println()
pass := string(bytePass)
return strings.TrimSpace(user), strings.TrimSpace(pass), nil
}
func OpenDB(dbName string) (*sql.DB, error) {
var err error
cfg := mysql.NewConfig()
cfg.DBName = dbName
cfg.User, cfg.Passwd, err = getCredentials()
if err != nil {
return nil, fmt.Errorf("OpenDB: getCredentials(): %v\n", err)
}
db, err := sql.Open("mysql", cfg.FormatDSN())
if err != nil {
return nil, fmt.Errorf("newDB: sql.Open(\"mysql\", cfg.FormatDSN()): %v\n", err)
}
if err := db.Ping(); err != nil {
return nil, fmt.Errorf("newDB: db.Ping(): %v\n", err)
}
return db, nil
}

7
go.mod
View File

@ -1,3 +1,10 @@
module streifling.com/jason/sicherheitsunterweisung
go 1.21.1
require (
github.com/go-sql-driver/mysql v1.7.1
golang.org/x/term v0.13.0
)
require golang.org/x/sys v0.13.0 // indirect

6
go.sum Normal file
View File

@ -0,0 +1,6 @@
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=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=

34
main.go
View File

@ -1,6 +1,7 @@
package main
package sicherheitsunterweisung
import (
"fmt"
"html/template"
"log"
"net/http"
@ -14,7 +15,7 @@ type Person struct {
type Instructor Person
type Participant struct {
id int64
ID int64
Person
Company string
}
@ -29,8 +30,16 @@ type Briefing struct {
}
func main() {
var i, j int64
var b Briefing
i, j = 1, 1
mux := http.NewServeMux()
i := 1
db, err := OpenDB("sicherheitsunterweisung")
if err != nil {
log.Fatalln(err)
}
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
@ -40,6 +49,25 @@ func main() {
i++
template.Must(template.ParseFiles("templates/index.html", "templates/participant.html")).ExecuteTemplate(w, "participant", i)
})
mux.HandleFunc("/submit/", func(w http.ResponseWriter, r *http.Request) {
b.Instructor.FirstName = r.PostFormValue("instructor-first")
b.Instructor.LastName = r.PostFormValue("instructor-last")
b.Date = r.PostFormValue("date")
b.Time = r.PostFormValue("time")
b.State = r.PostFormValue("state")
b.Location = r.PostFormValue("location")
for ; j <= i; j++ {
b.Participants = append(b.Participants, Participant{
ID: j,
Person: Person{
FirstName: r.PostFormValue("participant-first-" + fmt.Sprint(j)),
LastName: r.PostFormValue(("participant-last-" + fmt.Sprint(j))),
},
Company: r.PostFormValue(("participant-company-" + fmt.Sprint(j))),
})
}
})
log.Fatalln(http.ListenAndServe(":8080", mux))
}

View File

@ -12,6 +12,14 @@
<h1>Sicherheitsunterweisung</h1>
<form>
<div id="instructor">
<label for="instructor-first-input">Unterweiser Vorname</label>
<input type="text" name="instructor-first" id="instructor-first-input" />
<label for="instructor-last-input">Unterweiser Nachname</label>
<input type="text" name="instructor-last" id="instructor-last-input" />
</div>
<div id="date">
<label for="date-input">Datum</label>
<input type="date" name="date" id="date-input" />
@ -40,13 +48,13 @@
<div id="participant-{{ . }}">
<label for="participant-first-input-{{ . }}">Vorname</label>
<input type="text" name="participant-first" id="participant-first-input-{{ . }}" />
<input type="text" name="participant-first-{{ . }}" id="participant-first-input-{{ . }}" />
<label for="participant-last-input-{{ . }}">Nachname</label>
<input type="text" name="participant-last" id="participant-last-input-{{ . }}" />
<input type="text" name="participant-last-{{ . }}" id="participant-last-input-{{ . }}" />
<label for="participant-company-input-{{ . }}">Firma</label>
<input type="text" name="participant-company" id="participant-company-input-{{ . }}" />
<input type="text" name="participant-company-{{ . }}" id="participant-company-input-{{ . }}" />
</div>
</div>

View File

@ -1,12 +1,12 @@
{{ define "participant" }}
<div id="participant-{{ . }}">
<label for="participant-first-input-{{ . }}">Vorname</label>
<input type="text" name="participant-first" id="participant-first-input-{{ . }}" />
<input type="text" name="participant-first-{{ . }}" id="participant-first-input-{{ . }}" />
<label for="participant-last-input-{{ . }}">Nachname</label>
<input type="text" name="participant-last" id="participant-last-input-{{ . }}" />
<input type="text" name="participant-last-{{ . }}" id="participant-last-input-{{ . }}" />
<label for="participant-company-input-{{ . }}">Firma</label>
<input type="text" name="participant-company" id="participant-company-input-{{ . }}" />
<input type="text" name="participant-company-{{ . }}" id="participant-company-input-{{ . }}" />
</div>
{{ end }}