Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React_bug_fix #332

Merged
merged 13 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/generate-linter-advanced.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
framework: [chi, gin, fiber, gorilla/mux, httprouter, standard-library, echo]
driver: [postgres]
git: [commit]
advanced: [htmx, githubaction, websocket, tailwind, docker]
advanced: [htmx, githubaction, websocket, tailwind, docker, react]

runs-on: ubuntu-latest
steps:
Expand Down
14 changes: 14 additions & 0 deletions cmd/template/advanced/files/docker/docker_compose.yml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,21 @@ services:
BLUEPRINT_DB_URL: ${BLUEPRINT_DB_URL}
volumes:
- sqlite_bp:/app/db
{{- end }}
{{- if .AdvancedOptions.react }}
frontend:
build:
context: .
dockerfile: Dockerfile
target: frontend
restart: unless-stopped
ports:
- 5173:5173
depends_on:
- app
{{- end }}

{{- if and (.AdvancedOptions.docker) (eq .DBDriver "sqlite") }}
volumes:
sqlite_bp:
{{- end }}
16 changes: 16 additions & 0 deletions cmd/template/advanced/files/docker/dockerfile.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,19 @@ WORKDIR /app
COPY --from=build /app/main /app/main
EXPOSE ${PORT}
CMD ["./main"]

{{ if .AdvancedOptions.react}}
FROM node:20 AS frontend_builder
WORKDIR /frontend

COPY frontend/package*.json ./
RUN npm install
COPY frontend/. .
RUN npm run build

FROM node:23-slim AS frontend
RUN npm install -g serve
COPY --from=frontend_builder /frontend/dist /app/dist
EXPOSE 5173
CMD ["serve", "-s", "/app/dist", "-l", "5173"]
{{- end}}
6 changes: 6 additions & 0 deletions cmd/template/advanced/files/react/app.tsx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ function App() {
<button onClick={fetchData}>
Click to fetch from Go server
</button>
{message && (
<div>
<h2>Server Response:</h2>
<p>{message}</p>
</div>
)}
</>
)
}
Expand Down
14 changes: 14 additions & 0 deletions cmd/template/docker/files/docker-compose/mongo.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ services:
condition: service_healthy
networks:
- blueprint
{{- end }}
{{- if and .AdvancedOptions.react .AdvancedOptions.docker }}
frontend:
build:
context: .
dockerfile: Dockerfile
target: frontend
restart: unless-stopped
depends_on:
- app
ports:
- 5173:5173
networks:
- blueprint
{{- end }}
mongo_bp:
image: mongo:latest
Expand Down
14 changes: 14 additions & 0 deletions cmd/template/docker/files/docker-compose/mysql.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ services:
condition: service_healthy
networks:
- blueprint
{{- end }}
{{- if and .AdvancedOptions.react .AdvancedOptions.docker }}
frontend:
build:
context: .
dockerfile: Dockerfile
target: frontend
restart: unless-stopped
depends_on:
- app
ports:
- 5173:5173
networks:
- blueprint
{{- end }}
mysql_bp:
image: mysql:latest
Expand Down
14 changes: 14 additions & 0 deletions cmd/template/docker/files/docker-compose/postgres.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ services:
condition: service_healthy
networks:
- blueprint
{{- end }}
{{- if and .AdvancedOptions.react .AdvancedOptions.docker }}
frontend:
build:
context: .
dockerfile: Dockerfile
target: frontend
restart: unless-stopped
depends_on:
- app
ports:
- 5173:5173
networks:
- blueprint
{{- end }}
psql_bp:
image: postgres:latest
Expand Down
14 changes: 14 additions & 0 deletions cmd/template/docker/files/docker-compose/redis.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ services:
condition: service_healthy
networks:
- blueprint
{{- end }}
{{- if and .AdvancedOptions.react .AdvancedOptions.docker }}
frontend:
build:
context: .
dockerfile: Dockerfile
target: frontend
restart: unless-stopped
depends_on:
- app
ports:
- 5173:5173
networks:
- blueprint
{{- end }}
redis_bp:
image: redis:7.2.4
Expand Down
22 changes: 7 additions & 15 deletions cmd/template/framework/files/routes/gin.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,20 @@ import (
{{end}}

"github.com/gin-gonic/gin"
"github.com/gin-contrib/cors"

{{.AdvancedTemplates.TemplateImports}}
)

func (s *Server) RegisterRoutes() http.Handler {
r := gin.Default()

// Apply CORS middleware
r.Use(func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "http://localhost:5173") // Add specific origins here or change localhost accordingly
c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH")
c.Header("Access-Control-Allow-Headers", "Accept, Authorization, Content-Type")
c.Header("Access-Control-Allow-Credentials", "true")

// Handle preflight OPTIONS requests
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(http.StatusNoContent)
return
}

c.Next()
})
r.Use(cors.New(cors.Config{
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this is right? This isnt handling the gin.Context at all

AllowOrigins: []string{"http://localhost:5173"}, // Add your frontend URL
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"},
AllowHeaders: []string{"Accept", "Authorization", "Content-Type"},
AllowCredentials: true, // Enable cookies/auth
}))

r.GET("/", s.HelloWorldHandler)
{{if ne .DBDriver "none"}}
Expand Down
41 changes: 41 additions & 0 deletions docs/docs/advanced-flag/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,42 @@ COPY --from=build /app/main /app/main
EXPOSE ${PORT}
CMD ["./main"]
```

Docker config if React flag is used:

```dockerfile
FROM golang:1.23-alpine AS build

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . .

RUN go build -o main cmd/api/main.go

FROM alpine:3.20.1 AS prod
WORKDIR /app
COPY --from=build /app/main /app/main
EXPOSE ${PORT}
CMD ["./main"]


FROM node:20 AS frontend_builder
WORKDIR /frontend

COPY frontend/package*.json ./
RUN npm install
COPY frontend/. .
RUN npm run build

FROM node:20-slim AS frontend
RUN npm install -g serve
COPY --from=frontend_builder /frontend/dist /app/dist
EXPOSE 5173
CMD ["serve", "-s", "/app/dist", "-l", "5173"]
```
## Docker compose
Docker and docker-compose.yml pull environment variables from the .env file.

Expand Down Expand Up @@ -87,3 +123,8 @@ networks:
If you are testing more than one framework locally, be aware of Docker leftovers such as volumes.
For proper cleaning and building, use `docker compose down --volumes` and `docker compose up --build`.

or

```bash
docker compose build --no-cache && docker compose up
```
Loading