Speichere Konfiguration in config/config.json
This commit is contained in:
parent
43b3631da2
commit
c38286bcc5
14 changed files with 249 additions and 78 deletions
|
@ -5,8 +5,11 @@ import (
|
|||
"bytes"
|
||||
"crypto/rand"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"math/big"
|
||||
"net/smtp"
|
||||
"sprechstundentool/config"
|
||||
"sprechstundentool/controllers"
|
||||
"sprechstundentool/models"
|
||||
)
|
||||
|
@ -14,12 +17,14 @@ import (
|
|||
type RequestRepo struct {
|
||||
db *sql.DB
|
||||
officeHourRepo models.OfficeHourRepository
|
||||
config config.Config
|
||||
}
|
||||
|
||||
func NewRequestRepo(db *sql.DB, officeHourRepo models.OfficeHourRepository) *RequestRepo {
|
||||
func NewRequestRepo(db *sql.DB, officeHourRepo models.OfficeHourRepository, config config.Config) *RequestRepo {
|
||||
return &RequestRepo{
|
||||
db: db,
|
||||
officeHourRepo: officeHourRepo,
|
||||
config: config,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -68,8 +73,8 @@ func (r *RequestRepo) Add(officeHour models.OfficeHour, action int) (int, error)
|
|||
* but don't insert new request into database.
|
||||
*/
|
||||
for _, request := range existents {
|
||||
if request.Action == action { // already covered by selection && request.OfficeHour == officeHour {
|
||||
return request.Id, sendConfirmationMail(request)
|
||||
if request.Action == action { // already covered by selection: && request.OfficeHour == officeHour {
|
||||
return request.Id, r.sendConfirmationMail(request)
|
||||
}
|
||||
}
|
||||
secret, err := r.newSecret()
|
||||
|
@ -85,7 +90,7 @@ func (r *RequestRepo) Add(officeHour models.OfficeHour, action int) (int, error)
|
|||
if err != nil {
|
||||
return request.Id, err
|
||||
}
|
||||
return request.Id, sendConfirmationMail(request)
|
||||
return request.Id, r.sendConfirmationMail(request)
|
||||
}
|
||||
|
||||
func (r *RequestRepo) Execute(request models.Request) error {
|
||||
|
@ -104,29 +109,42 @@ func (r *RequestRepo) Execute(request models.Request) error {
|
|||
}
|
||||
|
||||
func (r *RequestRepo) newSecret() (string, error) {
|
||||
secret := randomString(models.SecretLength)
|
||||
|
||||
_, err := r.FindBySecret(secret)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return "", err
|
||||
}
|
||||
var err error
|
||||
var secret string
|
||||
// find unused secret
|
||||
for err != sql.ErrNoRows {
|
||||
secret = randomString(models.SecretLength)
|
||||
secret = randomString(r.config.Request.SecretLength)
|
||||
_, err = r.FindBySecret(secret)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
return secret, nil
|
||||
}
|
||||
|
||||
func sendConfirmationMail(request models.Request) error {
|
||||
to := []string{request.OfficeHour.Tutor.Email}
|
||||
func (r *RequestRepo) sendConfirmationMail(request models.Request) error {
|
||||
var message bytes.Buffer
|
||||
err := controllers.Templates.ExecuteTemplate(&message, "confirmationMail", request)
|
||||
var data = struct {
|
||||
Config config.Config
|
||||
Request models.Request
|
||||
}{r.config, request}
|
||||
err := controllers.Templates.ExecuteTemplate(&message, "confirmationMail", data)
|
||||
if err != nil {
|
||||
log.Printf("Error parsing confirmation Mail: %s", err.Error())
|
||||
return err
|
||||
}
|
||||
err = smtp.SendMail("192.168.0.24:25", nil, "Mathebau Sprechstunden <sprechstunden@mathebau.de>", to, message.Bytes())
|
||||
return err
|
||||
switch r.config.Mailer.Type {
|
||||
case "Stdout":
|
||||
fmt.Println(message.String())
|
||||
case "Smtp":
|
||||
to := []string{request.OfficeHour.Tutor.Email}
|
||||
var auth smtp.Auth
|
||||
if r.config.Mailer.SmtpUseAuth {
|
||||
auth = smtp.PlainAuth(r.config.Mailer.SmtpIdentity, r.config.Mailer.FromAddress, r.config.Mailer.SmtpPassword, r.config.Mailer.SmtpHost)
|
||||
}
|
||||
return smtp.SendMail(fmt.Sprintf("%s:%d", r.config.Mailer.SmtpHost, r.config.Mailer.SmtpPort), auth, string(r.config.Mailer.FromName), to, message.Bytes())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func randomString(n int) string {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue