diff --git a/config/config.go b/config/config.go index 3b144b6..1eba4b1 100644 --- a/config/config.go +++ b/config/config.go @@ -11,7 +11,7 @@ import ( type Config struct { Server struct { ListenAddress string - ListenPort int + ListenPort int Protocol string Domain string } @@ -40,6 +40,9 @@ type Config struct { MysqlPort int MysqlDatabase string } + Tutor struct { + MailSuffix string + } } // ReadConfigFile takes a file path as an argument and attempts to @@ -48,15 +51,18 @@ type Config struct { func ReadConfigFile(filename string, conf *Config) error { configData, err := ioutil.ReadFile(filename) if err != nil { + log.Printf("Error reading config file: %s", err.Error()) return err } err = json.Unmarshal(configData, conf) if err != nil { + log.Printf("Error parsing config file as json: %s", err.Error()) return err } return validateConfig(conf) } +// Checks config for sane values (e.g. correct port range or database type). func validateConfig(conf *Config) error { var err error if !(conf.Server.ListenPort >= 1 && conf.Server.ListenPort <= 65535) { @@ -64,7 +70,7 @@ func validateConfig(conf *Config) error { log.Println(err.Error()) } if !(conf.Server.Protocol == "http" || conf.Server.Protocol == "https") { - err = fmt.Errorf("Validating config: Server protocol must be http or https, but is '%s'.", conf.Server.Protocol) + err = fmt.Errorf("Validating config: Server protocol must be 'http' or 'https', but is '%s'.", conf.Server.Protocol) log.Println(err.Error()) } if !(conf.Date.MinuteGranularity >= 1 && conf.Date.MinuteGranularity <= 60) { @@ -76,7 +82,7 @@ func validateConfig(conf *Config) error { log.Println(err.Error()) } if !(conf.Mailer.Type == "Stdout" || conf.Mailer.Type == "Smtp") { - err = fmt.Errorf("Validating config: Mailer type must be 'stdout' or 'smtp', but is '%s'.", conf.Mailer.Type) + err = fmt.Errorf("Validating config: Mailer type must be 'Stdout' or 'Smtp', but is '%s'.", conf.Mailer.Type) log.Println(err.Error()) } if !(conf.SQL.Type == "SQLite" || conf.SQL.Type == "Mysql") { diff --git a/config/config.json b/config/config.json index 2e64ad1..009bc61 100644 --- a/config/config.json +++ b/config/config.json @@ -14,7 +14,7 @@ "mailer": { "type": "Stdout", "fromAddress": "sprechstunden@localhost", - "fromName": "Mathebau Sprechstunden ", + "fromName": "Sprechstunden ", "smtpHost": "localhost", "smtpPort": 25, "smtpUseAuth": false, @@ -29,5 +29,8 @@ "mysqlHost": "localhost", "mysqlPort": 3306, "mysqlDatabase": "officeHours" + }, + "tutor": { + "mailSuffix": "" } } \ No newline at end of file diff --git a/controllers/addOfficeHourHandler.go b/controllers/addOfficeHourHandler.go index 7573d9c..ade5424 100644 --- a/controllers/addOfficeHourHandler.go +++ b/controllers/addOfficeHourHandler.go @@ -2,8 +2,10 @@ package controllers import ( "fmt" + "log" "net/http" "net/mail" + "sprechstundentool/config" "sprechstundentool/models" "strconv" "strings" @@ -22,6 +24,7 @@ type maskData struct { Email string Info string Errors []string + Config config.Config } func (b *BaseHandler) AddOfficeHourHandler(w http.ResponseWriter, req *http.Request) { @@ -105,8 +108,8 @@ func (b *BaseHandler) AddOfficeHourHandler(w http.ResponseWriter, req *http.Requ if err != nil { email = &mail.Address{"", req.FormValue("email")} errors = append(errors, "Mailaddresse konnte nicht geparst werden.") - } else if !strings.HasSuffix(email.Address, "tu-darmstadt.de") { - errors = append(errors, "Mailaddresse muss auf „tu-darmstadt.de“ enden.") + } else if !strings.HasSuffix(email.Address, b.config.Tutor.MailSuffix) { + errors = append(errors, fmt.Sprintf("Mailaddresse muss auf „%s“ enden.", b.config.Tutor.MailSuffix)) } info := req.FormValue("info") @@ -130,6 +133,7 @@ func (b *BaseHandler) AddOfficeHourHandler(w http.ResponseWriter, req *http.Requ email.Address, info, errors, + b.config, } b.writeAddOfficeHourMask(w, req, data) } else { @@ -155,14 +159,15 @@ func (b *BaseHandler) AddOfficeHourHandler(w http.ResponseWriter, req *http.Requ } func (b *BaseHandler) writeAddOfficeHourMask(w http.ResponseWriter, req *http.Request, data maskData) { - if len(data.Errors) != 0 { - w.WriteHeader(http.StatusBadRequest) - } if req.Method == http.MethodGet { data.Errors = []string{} } + if len(data.Errors) != 0 { + w.WriteHeader(http.StatusBadRequest) + } err := Templates.ExecuteTemplate(w, "addMask.html", data) if err != nil { + log.Printf("Template addMask.html could not be parsed: %s", err.Error()) w.Write([]byte(fmt.Sprintf("Template konnte nicht geparst werden : %s", err.Error()))) return } diff --git a/controllers/templates.go b/controllers/templates.go index 9b143c3..4659e26 100644 --- a/controllers/templates.go +++ b/controllers/templates.go @@ -12,6 +12,7 @@ var Templates, _ = template.Must(template.ParseFiles( "templates/deleteSuccess.html", "templates/executeFailure.html", "templates/executeSuccess.html", + "templates/footer.html", "templates/index.html", "templates/officeHourTable.html", "templates/requestNotFound.html")). diff --git a/main.go b/main.go index 703f98c..83e3065 100644 --- a/main.go +++ b/main.go @@ -1,3 +1,4 @@ +// main package main import ( @@ -51,5 +52,5 @@ func main() { http.HandleFunc("/", h.RootHandler) err = http.ListenAndServe(fmt.Sprintf("%s:%d", conf.Server.ListenAddress, conf.Server.ListenPort), nil) - fmt.Println(err.Error()) + log.Println(err.Error()) } diff --git a/templates/addFailure.html b/templates/addFailure.html index bd00fde..e032c42 100644 --- a/templates/addFailure.html +++ b/templates/addFailure.html @@ -6,9 +6,6 @@ Irgendetwas ist schief gegangen. Bitte sende folgende Daten an sprechstundentool@mathebau.de mit einer Beschreibung, was du tun wolltest.
{{.}} - + {{template "footer.html" .}} \ No newline at end of file diff --git a/templates/addMask.html b/templates/addMask.html index 700899d..e4e4e2b 100644 --- a/templates/addMask.html +++ b/templates/addMask.html @@ -39,15 +39,12 @@

- Du musst hier eine Email-Adresse angeben, die auf „tu-darmstadt.de“ endet.
+ {{if ne .Config.Tutor.MailSuffix ""}}Du musst hier eine Email-Adresse angeben, die auf „{{.Config.Tutor.MailSuffix}}“ endet.
{{end}} Außerdem dürfen in Räumen nur begrenzt viele Sprechstunden gleichzeitig stattfinden, nämlich
{{range $room := .Rooms}}
{{$room.Name}}
{{$room.MaxOccupy}} Sprechstunde{{if gt $room.MaxOccupy 1}}n{{end}}

{{end}}
- + {{template "footer.html" .}} \ No newline at end of file diff --git a/templates/addSuccess.html b/templates/addSuccess.html index f0a7a03..b253965 100644 --- a/templates/addSuccess.html +++ b/templates/addSuccess.html @@ -5,9 +5,6 @@ Die Sprechstunde wurde angelegt. Du solltest eine Mail mit einem Aktivierungslink erhalten haben.
- + {{template "footer.html" .}} \ No newline at end of file diff --git a/templates/deleteSuccess.html b/templates/deleteSuccess.html index 668f9e4..43502ac 100644 --- a/templates/deleteSuccess.html +++ b/templates/deleteSuccess.html @@ -5,9 +5,6 @@ Du solltest eine Mail mit einem Bestätigungslink erhalten haben.
- + {{template "footer.html" .}} \ No newline at end of file diff --git a/templates/executeFailure.html b/templates/executeFailure.html index 63eb321..c1d86ca 100644 --- a/templates/executeFailure.html +++ b/templates/executeFailure.html @@ -6,9 +6,6 @@ Irgendetwas ist schief gegangen. Bitte sende folgende Daten an sprechstundentool@mathebau.de mit einer Beschreibung, was du tun wolltest.
{{.}} - + {{template "footer.html" .}} \ No newline at end of file diff --git a/templates/executeSuccess.html b/templates/executeSuccess.html index 86b2f2f..a99efbb 100644 --- a/templates/executeSuccess.html +++ b/templates/executeSuccess.html @@ -4,9 +4,6 @@ Deine Anfrage wurde ausgeführt. - + {{template "footer.html" .}} \ No newline at end of file diff --git a/templates/footer.html b/templates/footer.html new file mode 100644 index 0000000..8b85b85 --- /dev/null +++ b/templates/footer.html @@ -0,0 +1,5 @@ + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html index 6e199a6..efd3b06 100644 --- a/templates/index.html +++ b/templates/index.html @@ -22,10 +22,6 @@ {{.Timetable}} - + {{template "footer.html" .}} \ No newline at end of file diff --git a/templates/requestNotFound.html b/templates/requestNotFound.html index 3763b2c..7270f7b 100644 --- a/templates/requestNotFound.html +++ b/templates/requestNotFound.html @@ -11,9 +11,6 @@ : - + {{template "footer.html" .}} \ No newline at end of file