Verschiedene Typen in package types ausgelagert und Channels zum asynchronen Datentransport eingesetzt
This commit is contained in:
parent
fcb509c9fe
commit
1bcfbfd325
24
db/db.go
24
db/db.go
@ -8,10 +8,17 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"streifling.com/jason/sicherheitsunterweisung/types"
|
||||||
|
|
||||||
"github.com/go-sql-driver/mysql"
|
"github.com/go-sql-driver/mysql"
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type DB struct {
|
||||||
|
*sql.DB
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
func getCredentials() (string, string, error) {
|
func getCredentials() (string, string, error) {
|
||||||
fmt.Printf("DB Benutzer: ")
|
fmt.Printf("DB Benutzer: ")
|
||||||
user, err := bufio.NewReader(os.Stdin).ReadString('\n')
|
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
|
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
|
var err error
|
||||||
|
|
||||||
cfg := mysql.NewConfig()
|
cfg := mysql.NewConfig()
|
||||||
@ -40,7 +48,8 @@ func OpenDB(dbName string) (*sql.DB, error) {
|
|||||||
return nil, fmt.Errorf("OpenDB: getCredentials(): %v\n", err)
|
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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("newDB: sql.Open(\"mysql\", cfg.FormatDSN()): %v\n", err)
|
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
|
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
18
main.go
@ -3,26 +3,34 @@ package main
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
// "streifling.com/jason/sicherheitsunterweisung/db"
|
|
||||||
|
"streifling.com/jason/sicherheitsunterweisung/db"
|
||||||
"streifling.com/jason/sicherheitsunterweisung/server"
|
"streifling.com/jason/sicherheitsunterweisung/server"
|
||||||
|
"streifling.com/jason/sicherheitsunterweisung/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var i, j int64
|
var i, j int64
|
||||||
var b server.Briefing
|
|
||||||
|
|
||||||
i, j = 1, 1
|
i, j = 1, 1
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
cb := make(chan *types.Briefing)
|
||||||
|
|
||||||
/* _, err := db.OpenDB("sicherheitsunterweisung")
|
db, err := db.OpenDB("sicherheitsunterweisung")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
} */
|
}
|
||||||
|
|
||||||
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
|
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
|
||||||
mux.HandleFunc("/", server.DisplayForm(&i))
|
mux.HandleFunc("/", server.DisplayForm(&i))
|
||||||
mux.HandleFunc("/add-participant/", server.AddParticipant(&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))
|
log.Fatalln(http.ListenAndServe(":8080", mux))
|
||||||
}
|
}
|
||||||
|
@ -5,30 +5,9 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"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 {
|
func DisplayForm(i *int64) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
template.Must(template.ParseFiles("templates/index.html")).Execute(w, i)
|
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) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var b types.Briefing
|
||||||
|
|
||||||
b.FirstName = r.PostFormValue("instructor-first")
|
b.FirstName = r.PostFormValue("instructor-first")
|
||||||
b.LastName = r.PostFormValue("instructor-last")
|
b.LastName = r.PostFormValue("instructor-last")
|
||||||
b.Date = r.PostFormValue("date")
|
b.Date = r.PostFormValue("date")
|
||||||
@ -52,9 +33,9 @@ func SubmitForm(b *Briefing, i, j *int64) http.HandlerFunc {
|
|||||||
b.Location = r.PostFormValue("location")
|
b.Location = r.PostFormValue("location")
|
||||||
|
|
||||||
for ; *j <= *i; *j++ {
|
for ; *j <= *i; *j++ {
|
||||||
b.Participants = append(b.Participants, Participant{
|
b.Participants = append(b.Participants, types.Participant{
|
||||||
ID: *j,
|
ID: *j,
|
||||||
Person: Person{
|
Person: types.Person{
|
||||||
FirstName: r.PostFormValue("participant-first-" + fmt.Sprint(*j)),
|
FirstName: r.PostFormValue("participant-first-" + fmt.Sprint(*j)),
|
||||||
LastName: r.PostFormValue(("participant-last-" + 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
23
types/types.go
Normal 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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user