From fe7e46e4ea8d540a31fa3d696e46b1d545144eb6 Mon Sep 17 00:00:00 2001 From: Daniel Heidemann Date: Thu, 4 Jul 2024 18:32:35 +0200 Subject: [PATCH] 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))