From 905a0ac6dcf5b5489186650f91308211b89fad66 Mon Sep 17 00:00:00 2001 From: "$(pass /github/name)" <$(pass /github/email)> Date: Wed, 3 Jul 2024 21:27:50 +0200 Subject: [PATCH] containerized; first fixes; html templates added --- .gitignore | 5 ++ Dockerfile | 32 ++++++++++ cmd/membership/main.go | 6 +- compose.yml | 10 +++ configs/config.template.json | 3 + internal/config/config.go | 10 ++- internal/server/server.go | 2 +- internal/services/email_service.go | 10 +-- internal/templates/mail_newRegistration.html | 67 ++++++++++++++++++++ internal/templates/mail_welcome.html | 67 ++++++++++++++++++++ pkg/logger/logger.go | 10 +-- templates/email/mail_newRegistration.html | 67 ++++++++++++++++++++ templates/email/mail_welcome.html | 67 ++++++++++++++++++++ 13 files changed, 342 insertions(+), 14 deletions(-) create mode 100644 Dockerfile create mode 100644 compose.yml create mode 100644 internal/templates/mail_newRegistration.html create mode 100644 internal/templates/mail_welcome.html create mode 100644 templates/email/mail_newRegistration.html create mode 100644 templates/email/mail_welcome.html diff --git a/.gitignore b/.gitignore index 6c2fc61..916adce 100644 --- a/.gitignore +++ b/.gitignore @@ -35,6 +35,7 @@ go.work !/.gitignore !*.go +!*.html !go.sum !go.mod @@ -43,6 +44,10 @@ go.work # all template files: !*.template* + +# Docker stuff +!compose.yml +!Dockerfile # !Makefile # ...even if they are in subdirectories diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2b16208 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,32 @@ +# Stage 1: Build the binary +FROM golang:alpine AS builder +LABEL maintainer="Alex Stölting " +# Set the Current Working Directory inside the container +WORKDIR /app + +# Copy go mod and sum files +COPY go.mod go.sum ./ + +# Download all dependencies. Dependencies will be cached if the go.mod and go.sum files are not changed +RUN go mod download + +# Copy the source code into the container +COPY . . +ENV CGO_ENABLED=1 +RUN apk add --no-cache gcc musl-dev +# Build the Go app +RUN go build -o main ./cmd/membership +FROM golang:alpine + +# Set the Current Working Directory inside the container +WORKDIR /root/ + +# Copy the Pre-built binary file from the builder stage +COPY --from=builder /app/main . + +# Expose port 8080 to the outside world +EXPOSE 8080 + +# Command to run the executable +CMD ["./main"] + diff --git a/cmd/membership/main.go b/cmd/membership/main.go index 374075c..fdefd35 100644 --- a/cmd/membership/main.go +++ b/cmd/membership/main.go @@ -1,8 +1,12 @@ package main -import "GoMembership/internal/server" +import ( + "GoMembership/internal/server" + "log" +) func main() { + log.Println("startup...") server.Run() } diff --git a/compose.yml b/compose.yml new file mode 100644 index 0000000..69dbf22 --- /dev/null +++ b/compose.yml @@ -0,0 +1,10 @@ +services: + app: + build: . + container_name: carsharingBackend + ports: + - "8080:8080" + volumes: + - ./configs/config.json:/root/configs/config.json:ro + - ./data/db.sqlite3:/root/data/db.sqlite3 + - ./templates:/root/templates:ro diff --git a/configs/config.template.json b/configs/config.template.json index 5756bdf..041f2c4 100644 --- a/configs/config.template.json +++ b/configs/config.template.json @@ -9,5 +9,8 @@ "Port": 465, "Mailtype": "html", "AdminEmail": "admin@server.com" + }, + "templates": { + "MailDir": "templates" } } diff --git a/internal/config/config.go b/internal/config/config.go index d4b4db3..ad5233a 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -27,10 +27,14 @@ type SMTPConfig struct { Port int `json:"Port"` } +type TemplateConfig struct { + MailDir string `json:"MailDir"` +} type Config struct { - DB DatabaseConfig `json:"db"` - Auth AuthenticationConfig - SMTP SMTPConfig `json:"smtp"` + DB DatabaseConfig `json:"db"` + Auth AuthenticationConfig + SMTP SMTPConfig `json:"smtp"` + Templates TemplateConfig `json:"templates"` } var ( diff --git a/internal/server/server.go b/internal/server/server.go index 05b1913..9369bab 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -16,7 +16,7 @@ import ( func Run() { cfg := config.LoadConfig() - + logger.Info.Printf("Config: %v", cfg) db := database.Connect(cfg.DB) defer db.Close() diff --git a/internal/services/email_service.go b/internal/services/email_service.go index 6180968..4a520aa 100644 --- a/internal/services/email_service.go +++ b/internal/services/email_service.go @@ -1,6 +1,7 @@ package services import ( + "GoMembership/internal/config" "GoMembership/internal/models" "GoMembership/pkg/logger" "bytes" @@ -33,9 +34,11 @@ func (s *EmailService) SendEmail(to string, subject string, body string) error { return nil } -func ParseTemplate(path string, data interface{}) (string, error) { +func ParseTemplate(filename string, data interface{}) (string, error) { // Read the email template file - tpl, err := template.ParseFiles(path) + + templateDir := config.LoadConfig().Templates.MailDir + tpl, err := template.ParseFiles(templateDir + "/" + filename) if err != nil { logger.Error.Printf("Failed to parse email template: %v", err) return "", err @@ -51,7 +54,6 @@ func ParseTemplate(path string, data interface{}) (string, error) { return tplBuffer.String(), nil } func (s *EmailService) SendWelcomeEmail(user models.User) error { - // Prepare data to be injected into the template data := struct { FirstName string @@ -62,7 +64,7 @@ func (s *EmailService) SendWelcomeEmail(user models.User) error { } subject := "Willkommen beim Dörpsmobil Hasloh e.V." - body, err := ParseTemplate("internal/templates/mail_welcome.html", data) + body, err := ParseTemplate("mail_welcome.html", data) if err != nil { logger.Error.Print("Couldn't send welcome mail") return err diff --git a/internal/templates/mail_newRegistration.html b/internal/templates/mail_newRegistration.html new file mode 100644 index 0000000..6008718 --- /dev/null +++ b/internal/templates/mail_newRegistration.html @@ -0,0 +1,67 @@ + + + + + + Willkommen beim Dörpsmobil Hasloh e.V. + + + +
+

{{.Firstname}} {{.LastName}} hat sich registriert

+

Ein neues Vereinsmitglied hat sich registriert

+

{{.Firstname}} {{.LastName}} hat sich registriert. Hier sind die Daten:

+
+
+ Registrierungsdaten: + Name: {{.Firstname}} {{.LastName}}
+ Email: {{.Email}}
+ IBAN: {{.IBAN}}
+ BIC: {{.BIC}}
+ Mandatsreferenz: {{.MandateReference}}
+
+

Mit freundlichen Grüßen,
+ der Server +

+
+ + + + diff --git a/internal/templates/mail_welcome.html b/internal/templates/mail_welcome.html new file mode 100644 index 0000000..883d321 --- /dev/null +++ b/internal/templates/mail_welcome.html @@ -0,0 +1,67 @@ + + + + + + Willkommen beim Dörpsmobil Hasloh e.V. + + + +
+

Willkommen beim Dörpsmobil Hasloh e.V.

+

Hallo {{.FirstName}},

+

herzlich willkommen beim Dörpsmobil Hasloh e.V.! Vielen Dank für Ihre Registrierung und Ihre Unterstützung unseres Projekts.

+

Wir freuen uns, Sie als Mitglied begrüßen zu dürfen und wünschen Ihnen stets eine sichere und angenehme Fahrt mit unseren Fahrzeugen.

+

Für weitere Fragen stehen wir Ihnen gerne zur Verfügung:

+
+ Kontakt: + Name: Anke Freitag
+ Vorsitzende Doerpsmobil-Hasloh e.V.
+ E-Mail: info@doerpsmobil-hasloh.de
+ Telefon: +49 174 870 1392 +
+

Besuchen Sie auch unsere Webseite unter https://carsharing-hasloh.de für weitere Informationen.

+

Mit freundlichen Grüßen,
+ Ihr Team von Dörpsmobil Hasloh e.V. +

+
+ + + diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index dd06150..ceff9a6 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -12,12 +12,12 @@ var ( ) func init() { - file, err := os.OpenFile("gomember.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) + /* file, err := os.OpenFile("gomember.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) if err != nil { log.Fatal(err) - } + } */ - Info = log.New(file, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile) - Warning = log.New(file, "WARNING: ", log.Ldate|log.Ltime|log.Lshortfile) - Error = log.New(file, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) + Info = log.New(os.Stderr, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile) + Warning = log.New(os.Stderr, "WARNING: ", log.Ldate|log.Ltime|log.Lshortfile) + Error = log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) } diff --git a/templates/email/mail_newRegistration.html b/templates/email/mail_newRegistration.html new file mode 100644 index 0000000..6008718 --- /dev/null +++ b/templates/email/mail_newRegistration.html @@ -0,0 +1,67 @@ + + + + + + Willkommen beim Dörpsmobil Hasloh e.V. + + + +
+

{{.Firstname}} {{.LastName}} hat sich registriert

+

Ein neues Vereinsmitglied hat sich registriert

+

{{.Firstname}} {{.LastName}} hat sich registriert. Hier sind die Daten:

+
+
+ Registrierungsdaten: + Name: {{.Firstname}} {{.LastName}}
+ Email: {{.Email}}
+ IBAN: {{.IBAN}}
+ BIC: {{.BIC}}
+ Mandatsreferenz: {{.MandateReference}}
+
+

Mit freundlichen Grüßen,
+ der Server +

+
+ + + + diff --git a/templates/email/mail_welcome.html b/templates/email/mail_welcome.html new file mode 100644 index 0000000..883d321 --- /dev/null +++ b/templates/email/mail_welcome.html @@ -0,0 +1,67 @@ + + + + + + Willkommen beim Dörpsmobil Hasloh e.V. + + + +
+

Willkommen beim Dörpsmobil Hasloh e.V.

+

Hallo {{.FirstName}},

+

herzlich willkommen beim Dörpsmobil Hasloh e.V.! Vielen Dank für Ihre Registrierung und Ihre Unterstützung unseres Projekts.

+

Wir freuen uns, Sie als Mitglied begrüßen zu dürfen und wünschen Ihnen stets eine sichere und angenehme Fahrt mit unseren Fahrzeugen.

+

Für weitere Fragen stehen wir Ihnen gerne zur Verfügung:

+
+ Kontakt: + Name: Anke Freitag
+ Vorsitzende Doerpsmobil-Hasloh e.V.
+ E-Mail: info@doerpsmobil-hasloh.de
+ Telefon: +49 174 870 1392 +
+

Besuchen Sie auch unsere Webseite unter https://carsharing-hasloh.de für weitere Informationen.

+

Mit freundlichen Grüßen,
+ Ihr Team von Dörpsmobil Hasloh e.V. +

+
+ + +