From fe7e46e4ea8d540a31fa3d696e46b1d545144eb6 Mon Sep 17 00:00:00 2001 From: Daniel Heidemann Date: Thu, 4 Jul 2024 18:32:35 +0200 Subject: [PATCH 1/2] frontend prod build; server now handles the reverse proxy (#19) --- docker-compose.yml | 8 ++++++++ frontend/.dockerignore | 2 ++ frontend/Dockerfile | 15 +++++++++++++++ server/server.go | 13 ++++++++++--- 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 frontend/.dockerignore create mode 100644 frontend/Dockerfile diff --git a/docker-compose.yml b/docker-compose.yml index b6d48d9..a568204 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -19,3 +19,11 @@ services: - 8080:8080 depends_on: - postgres + frontend: + build: + context: ./frontend + dockerfile: Dockerfile + restart: always + env_file: .env.local + depends_on: + - server diff --git a/frontend/.dockerignore b/frontend/.dockerignore new file mode 100644 index 0000000..b90a368 --- /dev/null +++ b/frontend/.dockerignore @@ -0,0 +1,2 @@ +node_modules +.next diff --git a/frontend/Dockerfile b/frontend/Dockerfile new file mode 100644 index 0000000..c70d954 --- /dev/null +++ b/frontend/Dockerfile @@ -0,0 +1,15 @@ +FROM node:22-alpine + +WORKDIR /app + +COPY package*.json ./ + +RUN npm install + +COPY . . + +RUN npm run build + +EXPOSE 3000 + +CMD [ "npm", "start" ] diff --git a/server/server.go b/server/server.go index 07e6d02..3c7f2a7 100644 --- a/server/server.go +++ b/server/server.go @@ -4,6 +4,8 @@ import ( "context" "log" "net/http" + "net/http/httputil" + "net/url" "os" "github.com/99designs/gqlgen/graphql/handler" @@ -51,9 +53,10 @@ func main() { defer c.Stop() // server configuration - // [/]: GraphQL Playground + // [/]: Next.JS frontend // [/api]: JSON API endpoint - // [/confirm/{email}]: Confirm email addresses + // [/playground]: GraphQL Playground + // [/confirm/{sessionID}]: Confirm email addresses router := chi.NewRouter() router.Use(cors.New(cors.Options{ AllowedHeaders: []string{"*"}, @@ -63,6 +66,10 @@ func main() { router.Use(middleware.Logger) + frontendUrl, _ := url.Parse("http://frontend:3000") + proxy := httputil.NewSingleHostReverseProxy(frontendUrl) + router.Handle("/*", proxy) + router.Get("/confirm/{sessionID}", func(w http.ResponseWriter, r *http.Request) { email.Confirm(ctx, w, r, db) }) @@ -79,7 +86,7 @@ func main() { }) }).Handle("/api", srv) - router.Handle("/", playground.Handler("GraphQL playground", "/api")) + router.Handle("/playground", playground.Handler("GraphQL playground", "/api")) log.Printf("connect to http://localhost:%s/ for GraphQL playground", port) log.Fatal(http.ListenAndServe(":"+port, router)) From e76075b67b99cbdf0c7e811f8712b89204736638 Mon Sep 17 00:00:00 2001 From: Daniel Heidemann Date: Thu, 4 Jul 2024 21:24:13 +0200 Subject: [PATCH 2/2] generic env var (#19) --- .env | 2 +- server/email/send.go | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.env b/.env index 33f97e3..39f830b 100644 --- a/.env +++ b/.env @@ -8,6 +8,6 @@ SMTP_PASSWORD= SMTP_PORT= FROM_ADDRESS= -API_URL=http://localhost:8080 +URL=http://localhost:8080 API_KEY= diff --git a/server/email/send.go b/server/email/send.go index 29be35a..eb89f27 100644 --- a/server/email/send.go +++ b/server/email/send.go @@ -36,7 +36,7 @@ func SendConfirmation(person models.User) error { Color: "#990000", Text: "E-Mail bestätigen", Link: fmt.Sprintf("%s/confirm/%s", - os.Getenv("API_URL"), strconv.Itoa(person.SessionID)), + os.Getenv("URL"), strconv.Itoa(person.SessionID)), }, }, }, @@ -45,6 +45,7 @@ func SendConfirmation(person models.User) error { }, }, } + from := os.Getenv("FROM_ADDRESS") smtpHost := os.Getenv("SMTP_HOST") smtpUser := os.Getenv("SMTP_USER")