Footer in eigenes Template verschoben und Logging verbessert

This commit is contained in:
Gonne 2022-09-19 17:00:19 +02:00
parent c38286bcc5
commit 4f5dc053a0
14 changed files with 40 additions and 44 deletions

View File

@ -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") {

View File

@ -14,7 +14,7 @@
"mailer": {
"type": "Stdout",
"fromAddress": "sprechstunden@localhost",
"fromName": "Mathebau Sprechstunden <sprechstunden@localhost>",
"fromName": "Sprechstunden <sprechstunden@localhost>",
"smtpHost": "localhost",
"smtpPort": 25,
"smtpUseAuth": false,
@ -29,5 +29,8 @@
"mysqlHost": "localhost",
"mysqlPort": 3306,
"mysqlDatabase": "officeHours"
},
"tutor": {
"mailSuffix": ""
}
}

View File

@ -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
}

View File

@ -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")).

View File

@ -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())
}

View File

@ -6,9 +6,6 @@
Irgendetwas ist schief gegangen. Bitte sende folgende Daten an <a href="mailto:sprechstundentool@mathebau.de">sprechstundentool@mathebau.de</a> mit einer Beschreibung, was du tun wolltest.
<br />
{{.}}
<footer>
<a href="/addOfficeHour">Sprechstunde anlegen</a><br />
Technische Fragen an <a href="mailto:sprechstundentool@mathebau.de">sprechstundentool@mathebau.de</a>
</footer>
{{template "footer.html" .}}
</body>
</html>

View File

@ -39,15 +39,12 @@
<input type="submit">
</form>
</p>
Du musst hier eine Email-Adresse angeben, die auf „tu-darmstadt.de“ endet.<br />
{{if ne .Config.Tutor.MailSuffix ""}}Du musst hier eine Email-Adresse angeben, die auf „{{.Config.Tutor.MailSuffix}}“ endet.<br />{{end}}
Außerdem dürfen in Räumen nur begrenzt viele Sprechstunden gleichzeitig stattfinden, nämlich
<dl>
{{range $room := .Rooms}}
<dt>{{$room.Name}}</dt><dl>{{$room.MaxOccupy}} Sprechstunde{{if gt $room.MaxOccupy 1}}n{{end}}</dl><br />{{end}}
</dl>
<footer>
<a href="/addOfficeHour">Sprechstunde anlegen</a><br />
Technische Fragen an <a href="mailto:sprechstundentool@mathebau.de">sprechstundentool@mathebau.de</a>
</footer>
{{template "footer.html" .}}
</body>
</html>

View File

@ -5,9 +5,6 @@
<body>
Die Sprechstunde wurde angelegt. Du solltest eine Mail mit einem Aktivierungslink erhalten haben.
<br />
<footer>
<a href="/addOfficeHour">Sprechstunde anlegen</a><br />
Technische Fragen an <a href="mailto:sprechstundentool@mathebau.de">sprechstundentool@mathebau.de</a>
</footer>
{{template "footer.html" .}}
</body>
</html>

View File

@ -5,9 +5,6 @@
<body>
Du solltest eine Mail mit einem Bestätigungslink erhalten haben.
<br />
<footer>
<a href="/addOfficeHour">Sprechstunde anlegen</a><br />
Technische Fragen an <a href="mailto:sprechstundentool@mathebau.de">sprechstundentool@mathebau.de</a>
</footer>
{{template "footer.html" .}}
</body>
</html>

View File

@ -6,9 +6,6 @@
Irgendetwas ist schief gegangen. Bitte sende folgende Daten an <a href="mailto:sprechstundentool@mathebau.de">sprechstundentool@mathebau.de</a> mit einer Beschreibung, was du tun wolltest.
<br />
{{.}}
<footer>
<a href="/addOfficeHour">Sprechstunde anlegen</a><br />
Technische Fragen an <a href="mailto:sprechstundentool@mathebau.de">sprechstundentool@mathebau.de</a>
</footer>
{{template "footer.html" .}}
</body>
</html>

View File

@ -4,9 +4,6 @@
</head>
<body>
Deine Anfrage wurde ausgeführt.
<footer>
<a href="/addOfficeHour">Sprechstunde anlegen</a><br />
Technische Fragen an <a href="mailto:sprechstundentool@mathebau.de">sprechstundentool@mathebau.de</a>
</footer>
{{template "footer.html" .}}
</body>
</html>

5
templates/footer.html Normal file
View File

@ -0,0 +1,5 @@
<footer>
<a href="/addOfficeHour">Sprechstunde anlegen</a><br />
<a href="/deleteOfficeHour">Sprechstunde löschen</a><br />
Technische Fragen an <a href="mailto:sprechstundentool@mathebau.de">sprechstundentool@mathebau.de</a>
</footer>

View File

@ -22,10 +22,6 @@
<input type="submit" value="Auswählen" />
</form>
{{.Timetable}}
<footer>
<a href="/addOfficeHour">Sprechstunde anlegen</a><br />
<a href="/deleteOfficeHour">Sprechstunde löschen</a><br />
Technische Fragen an <a href="mailto:sprechstundentool@mathebau.de">sprechstundentool@mathebau.de</a>
</footer>
{{template "footer.html" .}}
</body>
</html>

View File

@ -11,9 +11,6 @@
<label for="code">Bestätigungscode</label>: <input type="text" name="code"/>
<input type="submit" />
</form>
<footer>
<a href="/addOfficeHour">Sprechstunde anlegen</a><br />
Technische Fragen an <a href="mailto:sprechstundentool@mathebau.de">sprechstundentool@mathebau.de</a>
</footer>
{{template "footer.html" .}}
</body>
</html>