11 Commits

10 changed files with 117 additions and 43 deletions

1
go.mod
View File

@ -4,7 +4,6 @@ go 1.21.1
require ( require (
github.com/go-sql-driver/mysql v1.7.1 github.com/go-sql-driver/mysql v1.7.1
github.com/google/uuid v1.3.1
golang.org/x/term v0.13.0 golang.org/x/term v0.13.0
) )

2
go.sum
View File

@ -1,7 +1,5 @@
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI= 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/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 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 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek=

26
main.go
View File

@ -1,29 +1,45 @@
package main package main
import ( import (
"html/template"
"log" "log"
"net/http" "net/http"
"streifling.com/jason/sicherheitsunterweisung/packages/data"
"streifling.com/jason/sicherheitsunterweisung/packages/db"
"streifling.com/jason/sicherheitsunterweisung/packages/server" "streifling.com/jason/sicherheitsunterweisung/packages/server"
) )
func addUUIDs(uc *chan string, uuids *[]string) {
for uuid := range *uc {
(*uuids) = append(*uuids, uuid)
}
}
func main() { func main() {
var i, j int64 var i, j int64
uuids := make([]string, 0)
i, j = 1, 1 i, j = 1, 1
mux := http.NewServeMux() mux := http.NewServeMux()
db, err := db.Open("sicherheitsunterweisung") db, err := data.OpenDB("sicherheitsunterweisung")
if err != nil { if err != nil {
log.Fatalln(err) log.Fatalln(err)
} }
uuidChan := make(chan string)
defer close(uuidChan)
go addUUIDs(&uuidChan, &uuids)
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.DisplayTable(db)) mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("templates/index.html", "templates/login.html")).Execute(w, nil)
})
mux.HandleFunc("/search/", server.DisplayResults(db)) mux.HandleFunc("/search/", server.DisplayResults(db))
mux.HandleFunc("/new-briefing/", server.DisplayForm(&i)) mux.HandleFunc("/new-briefing/", server.DisplayForm(&i))
mux.HandleFunc("/add-participant/", server.AddParticipant(&i)) mux.HandleFunc("/add-participant/", server.AddParticipant(&i))
mux.HandleFunc("/submit/", server.SubmitForm(db, &i, &j)) mux.HandleFunc("/submit-form/", server.SubmitForm(db, &i, &j))
mux.HandleFunc("/generate-uuid/", server.GenerateUUID(uuidChan))
mux.HandleFunc("/internal-login/", server.DisplayTable(db))
mux.HandleFunc("/external-login/", server.DisplayQuestionsIfOK(&uuids))
log.Fatalln(http.ListenAndServe(":8080", mux)) log.Fatalln(http.ListenAndServe(":8080", mux))
} }

View File

@ -1,4 +1,4 @@
package db package data
import ( import (
"bufio" "bufio"
@ -43,7 +43,7 @@ func reverseOrder(bs *[]types.Briefing) {
} }
} }
func Open(dbName string) (*DB, error) { func OpenDB(dbName string) (*DB, error) {
var err error var err error
db := new(DB) db := new(DB)

View File

@ -0,0 +1 @@
package data

View File

@ -1,34 +1,31 @@
package server package server
import ( import (
"crypto/rand"
"encoding/hex"
"fmt" "fmt"
"github.com/google/uuid"
"html/template" "html/template"
"io"
"log" "log"
"net/http" "net/http"
"os" "streifling.com/jason/sicherheitsunterweisung/packages/data"
"streifling.com/jason/sicherheitsunterweisung/packages/db"
"streifling.com/jason/sicherheitsunterweisung/packages/types" "streifling.com/jason/sicherheitsunterweisung/packages/types"
) )
func DisplayTable(db *db.DB) http.HandlerFunc { func DisplayTable(db *data.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
bs, err := db.ReadAll() bs, err := db.ReadAll()
if err != nil { if err != nil {
_ = fmt.Errorf("DisplayTable: %v\n", err) _ = fmt.Errorf("DisplayTable: %v\n", err)
return
} }
template.Must(template.ParseFiles("templates/index.html", "templates/table.html")).Execute(w, bs) template.Must(template.ParseFiles("templates/index.html", "templates/table.html")).Execute(w, bs)
} }
} }
func DisplayResults(db *db.DB) http.HandlerFunc { func DisplayResults(db *data.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
bs, err := db.ReadByName(r.PostFormValue("search")) bs, err := db.ReadByName(r.PostFormValue("search"))
if err != nil { if err != nil {
_ = fmt.Errorf("DisplayResults: db.ReadByName(r.PostFormValue()): %v\n", err) _ = fmt.Errorf("DisplayResults: db.ReadByName(r.PostFormValue()): %v\n", err)
return
} }
template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "rows", bs) template.Must(template.ParseFiles("templates/table.html")).ExecuteTemplate(w, "rows", bs)
} }
@ -47,7 +44,7 @@ func AddParticipant(i *int64) http.HandlerFunc {
} }
} }
func SubmitForm(db *db.DB, i, j *int64) http.HandlerFunc { func SubmitForm(db *data.DB, i, j *int64) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
b := new(types.Briefing) b := new(types.Briefing)
@ -74,33 +71,44 @@ func SubmitForm(db *db.DB, i, j *int64) http.HandlerFunc {
bs, err := db.ReadAll() bs, err := db.ReadAll()
if err != nil { if err != nil {
_ = fmt.Errorf("SubmitForm: db.ReadAll(): %v\n", err) _ = fmt.Errorf("SubmitForm: db.ReadAll(): %v\n", err)
return
} }
template.Must(template.ParseFiles("templates/index.html", "templates/table.html")).Execute(w, bs)
template.Must(template.ParseFiles("templates/table.html")).Execute(w, bs)
} }
} }
func Upload(fn, ln, d, t string) http.HandlerFunc { func GenerateUUID(ch chan<- string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
r.ParseMultipartForm(10 << 20) // 10 MiB bs := make([]byte, 4)
tmpFile, _, err := r.FormFile("questionaire") _, err := rand.Read(bs)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) _ = fmt.Errorf("GenerateUUID: rand.Read(bs): %v\n", err)
return
} }
defer tmpFile.Close()
file, err := os.Create(ln + fn + d + t + uuid.New().String() + ".png") uuid := hex.EncodeToString(bs)
if err != nil { ch <- uuid
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer file.Close()
if _, err := io.Copy(file, tmpFile); err != nil { template.Must(template.ParseFiles("templates/form.html")).ExecuteTemplate(w, "uuid", uuid)
http.Error(w, err.Error(), http.StatusInternalServerError) }
return }
func DisplayQuestionsIfOK(uuids *[]string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if uuidIsOK(r.PostFormValue("login"), uuids) {
template.Must(template.ParseFiles("templates/question.html")).ExecuteTemplate(w, "content", nil)
} else {
template.Must(template.ParseFiles("templates/login.html")).ExecuteTemplate(w, "content", nil)
} }
} }
} }
// TODO: Delete uuid from uuids
func uuidIsOK(uuid string, uuids *[]string) bool {
for _, u := range *uuids {
if uuid == u {
return true
}
}
return false
}

View File

@ -1,7 +1,5 @@
package types package types
import "os"
type Person struct { type Person struct {
FirstName string FirstName string
LastName string LastName string
@ -9,11 +7,28 @@ type Person struct {
type Instructor Person type Instructor Person
type QAElement struct {
ID int
Text string
}
type Answer QAElement
type Question struct {
QAElement
Answers []Answer
}
type Questionaire struct {
UUID string
Questions []Question
}
type Participant struct { type Participant struct {
ID int64 ID int64
Person Person
Company string Company string
Questionaire string QuestionaireUUID string
} }
type Briefing struct { type Briefing struct {

View File

@ -1,3 +1,7 @@
{{ define "uuid" }}
<span>{{ . }}</span>
{{ end }}
{{ define "participant" }} {{ define "participant" }}
<div id="participant-{{ . }}"> <div id="participant-{{ . }}">
<label for="participant-first-input-{{ . }}">Vorname</label> <label for="participant-first-input-{{ . }}">Vorname</label>
@ -9,7 +13,9 @@
<label for="participant-company-input-{{ . }}">Firma</label> <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-{{ . }}" />
<button type="button" hx-post="/upload/">Fragebogen hochladen</button> <button type="button" hx-post="/generate-uuid/" hx-target="this" hx-swap="outerHTML">
Einladen
</button>
</div> </div>
{{ end }} {{ end }}
@ -45,13 +51,13 @@
<div id="participants"> <div id="participants">
<button type="button" hx-post="/add-participant/" hx-target="#participants" hx-swap="beforeend" hx-trigger="click"> <button type="button" hx-post="/add-participant/" hx-target="#participants" hx-swap="beforeend" hx-trigger="click">
+ Neuer Teilnehmer
</button> </button>
{{ template "participant" . }} {{ template "participant" . }}
</div> </div>
<button type="submit" hx-post="/submit/" hx-target="#content" hx-swap="innerHTML"> <button type="submit" hx-post="/submit-form/" hx-target="#content" hx-swap="innerHTML">
Senden Senden
</button> </button>
</form> </form>

16
templates/login.html Normal file
View File

@ -0,0 +1,16 @@
{{ define "content" }}
<form>
<h2>Login</h2>
<label for="login-input">Code</label>
<input type="text" name="login" id="login-input" />
<button type="submit" hx-post="/external-login/" hx-target="#content">
Anmelden
</button>
<button type="submit" hx-post="/internal-login/" hx-target="#content">
Intern
</button>
</form>
{{ end }}

15
templates/question.html Normal file
View File

@ -0,0 +1,15 @@
{{ define "answers" }}
{{ range .Answers }}
<label for="answer-{{ .ID }}">{{ .Text }}</label>
<input type="radio" name="answer-{{ .ID }}" id="answer-{{ .ID }}" />
{{ end }}
{{ end }}
{{ define "content" }}
<h2>Frage {{ .Question.ID }}</h2>
<p>{{ .Question.Text }}</p>
{{ template "answers" . }}
<button type="submit" hx-post="/submit-{{ .UUID }}-{{ .Question.ID }}/" hx-target="#content" hx-swap="innerHTML">
{{ end }}