Skip to content

Commit

Permalink
frontend prod build; server now handles the reverse proxy (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
dheidemann committed Jul 4, 2024
1 parent 7a7f2e6 commit fe7e46e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
8 changes: 8 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions frontend/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.next
15 changes: 15 additions & 0 deletions frontend/Dockerfile
Original file line number Diff line number Diff line change
@@ -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" ]
13 changes: 10 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"log"
"net/http"
"net/http/httputil"
"net/url"
"os"

"github.com/99designs/gqlgen/graphql/handler"
Expand Down Expand Up @@ -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{"*"},
Expand All @@ -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)
})
Expand All @@ -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))
Expand Down

0 comments on commit fe7e46e

Please sign in to comment.