Compare commits

...

3 Commits

8 changed files with 45 additions and 45 deletions

12
main.go
View File

@ -17,7 +17,7 @@ import (
"net/http" "net/http"
"streifling.com/jason/sicherheitsunterweisung/packages/data" "streifling.com/jason/sicherheitsunterweisung/packages/data"
"streifling.com/jason/sicherheitsunterweisung/packages/session" "streifling.com/jason/sicherheitsunterweisung/packages/server"
) )
func main() { func main() {
@ -26,16 +26,16 @@ func main() {
log.Fatalln(err) log.Fatalln(err)
} }
mux := session.NewMux() mux := server.NewMux()
sessions := make([]*session.Session, 0) sessions := make([]*server.Session, 0)
sessionChan := make(chan *session.Session) sessionChan := make(chan *server.Session)
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/")))) mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static/"))))
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
template.Must(template.ParseFiles("templates/index.html", "templates/login.html")).Execute(w, nil) template.Must(template.ParseFiles("templates/index.html", "templates/login.html")).Execute(w, nil)
}) })
mux.HandleFunc("/internal-login/", session.HandleInternalLogin(db, &sessions, sessionChan)) mux.HandleFunc("/internal-login/", server.HandleInternalLogin(db, &sessions, sessionChan))
mux.HandleFunc("/external-login/", session.HandleExternalLogin(&sessions)) mux.HandleFunc("/external-login/", server.HandleExternalLogin(&sessions))
go mux.HandleSessions(db, sessionChan, &sessions) go mux.HandleSessions(db, sessionChan, &sessions)

View File

@ -18,38 +18,38 @@ type DB struct {
} }
type Person struct { type Person struct {
ID int64
FirstName string FirstName string
LastName string LastName string
ID int64
} }
type Instructor Person type Instructor Person
type Participant struct { type Participant struct {
Person
Company string Company string
Person
} }
type Briefing struct { type Briefing struct {
ID int64
DateTime string DateTime string
Location string Location string
Document string Document string
AsOf string AsOf string
InstructorID int64 InstructorID int64
ID int64
} }
type Answer struct { type Answer struct {
ID int64
Text string Text string
IsImage bool // TODO: relocate to sessionStructs if possible IsImage bool // TODO: relocate to sessionStructs if possible
ID int64
} }
type Question struct { type Question struct {
ID int64
Text string Text string
Answers []Answer Answers []Answer
Correct int Correct int
ID int64
} }
type GivenAnswer struct { type GivenAnswer struct {

View File

@ -9,7 +9,7 @@
* You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. * You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package session package server
import ( import (
"crypto/rand" "crypto/rand"
@ -41,7 +41,7 @@ func generateLogin() (string, error) {
return hex.EncodeToString(bs), nil return hex.EncodeToString(bs), nil
} }
func (b *Briefing) HandleNewParticipant(cp chan<- *Participant) http.HandlerFunc { func (b *Briefing) handleNewParticipant(cp chan<- *Participant) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
var err error var err error
participant := Participant{ participant := Participant{
@ -67,7 +67,7 @@ func (b *Briefing) HandleNewParticipant(cp chan<- *Participant) http.HandlerFunc
} }
} }
func (b *Briefing) HandleBriefingForm(db *data.DB, s *Session) http.HandlerFunc { func (b *Briefing) handleBriefingForm(db *data.DB, s *Session) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
b.DateTime = time.Now().Format("2006-01-02 15:04:05") b.DateTime = time.Now().Format("2006-01-02 15:04:05")
b.Location = r.PostFormValue("location") b.Location = r.PostFormValue("location")

View File

@ -9,7 +9,7 @@
* You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. * You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package session package server
import ( import (
"github.com/google/uuid" "github.com/google/uuid"
@ -17,27 +17,27 @@ import (
) )
type tableHTMLData struct { type tableHTMLData struct {
SessionID uuid.UUID
OTD []data.OverviewTableData OTD []data.OverviewTableData
SessionID uuid.UUID
} }
type participantHTMLData struct { type participantHTMLData struct {
BriefingID uuid.UUID
Participant Participant
BriefingID uuid.UUID
} }
type questionHTMLData struct { type questionHTMLData struct {
Question data.Question
participantHTMLData participantHTMLData
QuestionID int64 QuestionID int64
Question data.Question
} }
type resultAnswer struct { type resultAnswer struct {
ID int64
Text string Text string
Correct bool Correct bool
Chosen bool Chosen bool
IsImage bool // TODO: relocate to sessionStructs if possible IsImage bool // TODO: relocate to sessionStructs if possible
ID int64
} }
type resultQuestion struct { type resultQuestion struct {
@ -46,12 +46,12 @@ type resultQuestion struct {
} }
type resultHTMLData struct { type resultHTMLData struct {
participantHTMLData
Questions []resultQuestion Questions []resultQuestion
participantHTMLData
} }
type summaryHTMLData struct { type summaryHTMLData struct {
ParticipantsData []participantHTMLData
SessionID uuid.UUID SessionID uuid.UUID
BriefingID uuid.UUID BriefingID uuid.UUID
ParticipantsData []participantHTMLData
} }

View File

@ -9,7 +9,7 @@
* You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. * You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package session package server
import ( import (
"fmt" "fmt"

View File

@ -9,7 +9,7 @@
* You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. * You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package session package server
import ( import (
"fmt" "fmt"
@ -32,15 +32,15 @@ func (mux *Mux) handleParticipants(db *data.DB, cp <-chan *Participant, b *Brief
for p := range cp { for p := range cp {
p.GivenAnswers = make([]int, len(b.Questions)) p.GivenAnswers = make([]int, len(b.Questions))
mux.HandleFunc("/submit-participant/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleParticipant(db, b)) mux.HandleFunc("/submit-participant/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.handleParticipant(db, b))
mux.HandleFunc("/end-video/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleEndOfVideo(b)) mux.HandleFunc("/end-video/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.handleEndOfVideo(b))
var i int var i int
for i = range b.Questions { for i = range b.Questions {
mux.HandleFunc("/submit-answer/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/"+fmt.Sprint(i+1)+"/", p.HandleAnswer(db, b, int64(i+1))) mux.HandleFunc("/submit-answer/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/"+fmt.Sprint(i+1)+"/", p.handleAnswer(db, b, int64(i+1)))
} }
mux.HandleFunc("/allow-retry/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleAllowRetry()) mux.HandleFunc("/allow-retry/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.handleAllowRetry())
mux.HandleFunc("/retry/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleRetry(b, &i)) mux.HandleFunc("/retry/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.handleRetry(b, &i))
mux.HandleFunc("/refresh-summary/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.HandleRefresh(b)) mux.HandleFunc("/refresh-summary/"+fmt.Sprint(b.UUID)+"/"+fmt.Sprint(p.Login)+"/", p.handleRefresh(b))
} }
} }
@ -74,8 +74,8 @@ func (mux *Mux) handleBriefings(db *data.DB, cb <-chan *Briefing, s *Session) {
for b := range cb { for b := range cb {
getQuestions(db, b) getQuestions(db, b)
mux.HandleFunc("/new-participant/"+fmt.Sprint(b.UUID)+"/", b.HandleNewParticipant(participantChan)) mux.HandleFunc("/new-participant/"+fmt.Sprint(b.UUID)+"/", b.handleNewParticipant(participantChan))
mux.HandleFunc("/submit-form/"+fmt.Sprint(b.UUID)+"/", b.HandleBriefingForm(db, s)) mux.HandleFunc("/submit-form/"+fmt.Sprint(b.UUID)+"/", b.handleBriefingForm(db, s))
go mux.handleParticipants(db, participantChan, b) go mux.handleParticipants(db, participantChan, b)
} }
@ -86,9 +86,9 @@ func (mux *Mux) HandleSessions(db *data.DB, cs <-chan *Session, ss *[]*Session)
for s := range cs { for s := range cs {
(*ss) = append((*ss), s) (*ss) = append((*ss), s)
mux.HandleFunc("/search/"+fmt.Sprint(s.UUID)+"/", s.HandleSearch(db)) mux.HandleFunc("/search/"+fmt.Sprint(s.UUID)+"/", s.handleSearch(db))
mux.HandleFunc("/new-briefing/"+fmt.Sprint(s.UUID)+"/", s.HandleNewBriefing(briefingChan)) mux.HandleFunc("/new-briefing/"+fmt.Sprint(s.UUID)+"/", s.handleNewBriefing(briefingChan))
mux.HandleFunc("/briefing-done/"+fmt.Sprint(s.UUID)+"/", s.HandleBriefingDone(db)) mux.HandleFunc("/briefing-done/"+fmt.Sprint(s.UUID)+"/", s.handleBriefingDone(db))
go mux.handleBriefings(db, briefingChan, s) go mux.handleBriefings(db, briefingChan, s)
} }

View File

@ -9,7 +9,7 @@
* You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. * You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package session package server
import ( import (
"fmt" "fmt"
@ -67,7 +67,7 @@ func makeResultQuestions(sq []data.Question, givenAnswers []int) []resultQuestio
return questions return questions
} }
func (p *Participant) HandleParticipant(db *data.DB, b *Briefing) http.HandlerFunc { func (p *Participant) handleParticipant(db *data.DB, b *Briefing) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
p.FirstName = r.PostFormValue("first-" + fmt.Sprint(p.Login)) p.FirstName = r.PostFormValue("first-" + fmt.Sprint(p.Login))
p.LastName = r.PostFormValue("last-" + fmt.Sprint(p.Login)) p.LastName = r.PostFormValue("last-" + fmt.Sprint(p.Login))
@ -88,7 +88,7 @@ func (p *Participant) HandleParticipant(db *data.DB, b *Briefing) http.HandlerFu
} }
} }
func (p *Participant) HandleAnswer(db *data.DB, b *Briefing, i int64) http.HandlerFunc { func (p *Participant) handleAnswer(db *data.DB, b *Briefing, i int64) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if i < int64(len(b.Questions)) { if i < int64(len(b.Questions)) {
if err := handleGivenAnswer(p, i-1, r); err != nil { if err := handleGivenAnswer(p, i-1, r); err != nil {
@ -139,14 +139,14 @@ func (p *Participant) HandleAnswer(db *data.DB, b *Briefing, i int64) http.Handl
} }
} }
func (p *Participant) HandleAllowRetry() http.HandlerFunc { func (p *Participant) handleAllowRetry() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
p.NoIncorrect = -1 p.NoIncorrect = -1
p.AllowRetry = true p.AllowRetry = true
} }
} }
func (p *Participant) HandleRetry(b *Briefing, i *int) http.HandlerFunc { func (p *Participant) handleRetry(b *Briefing, i *int) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
if p.AllowRetry { if p.AllowRetry {
p.AllowRetry = false p.AllowRetry = false
@ -176,7 +176,7 @@ func (p *Participant) HandleRetry(b *Briefing, i *int) http.HandlerFunc {
} }
} }
func (p *Participant) HandleRefresh(b *Briefing) http.HandlerFunc { func (p *Participant) handleRefresh(b *Briefing) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
data := participantHTMLData{ data := participantHTMLData{
BriefingID: b.UUID, BriefingID: b.UUID,
@ -187,7 +187,7 @@ func (p *Participant) HandleRefresh(b *Briefing) http.HandlerFunc {
} }
} }
func (p *Participant) HandleEndOfVideo(b *Briefing) http.HandlerFunc { func (p *Participant) handleEndOfVideo(b *Briefing) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
data := questionHTMLData{ data := questionHTMLData{
participantHTMLData: participantHTMLData{ participantHTMLData: participantHTMLData{

View File

@ -9,7 +9,7 @@
* You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. * You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
package session package server
import ( import (
"html/template" "html/template"
@ -26,7 +26,7 @@ type Session struct {
Briefings []*Briefing Briefings []*Briefing
} }
func (s *Session) HandleSearch(db *data.DB) http.HandlerFunc { func (s *Session) handleSearch(db *data.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
var err error var err error
data := tableHTMLData{} data := tableHTMLData{}
@ -41,7 +41,7 @@ func (s *Session) HandleSearch(db *data.DB) http.HandlerFunc {
} }
} }
func (s *Session) HandleNewBriefing(cb chan<- *Briefing) http.HandlerFunc { func (s *Session) handleNewBriefing(cb chan<- *Briefing) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
briefing := Briefing{Briefing: &data.Briefing{InstructorID: s.Instructor.ID}, UUID: uuid.New()} briefing := Briefing{Briefing: &data.Briefing{InstructorID: s.Instructor.ID}, UUID: uuid.New()}
s.Briefings = append(s.Briefings, &briefing) s.Briefings = append(s.Briefings, &briefing)
@ -51,7 +51,7 @@ func (s *Session) HandleNewBriefing(cb chan<- *Briefing) http.HandlerFunc {
} }
} }
func (s *Session) HandleBriefingDone(db *data.DB) http.HandlerFunc { func (s *Session) handleBriefingDone(db *data.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
data := tableHTMLData{SessionID: s.UUID} data := tableHTMLData{SessionID: s.UUID}
var err error var err error