Skip to content

Commit f13c8b7

Browse files
authored
feat: use PORT in React from backend .env created in frontend (#355)
1 parent fde1fee commit f13c8b7

File tree

7 files changed

+36
-5
lines changed

7 files changed

+36
-5
lines changed

.github/workflows/generate-linter-advanced.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
run: echo "PROJECT_DIRECTORY=${{ matrix.framework }}" | sed 's/\//-/g' >> $GITHUB_ENV
3636

3737
- name: build templates
38-
run: script -q /dev/null -c "go run main.go create -n ${{ env.PROJECT_DIRECTORY }} -f ${{ matrix.framework}} -d ${{ matrix.driver }} -g ${{ matrix.git}} --advanced --feature ${{ matrix.advanced }}" /dev/null
38+
run: script -q /dev/null -c "go run main.go create -n ${{ env.PROJECT_DIRECTORY }} -f ${{ matrix.framework}} -d ${{ matrix.driver }} -g ${{ matrix.git}} --advanced --feature ${{ matrix.advanced }}"
3939

4040
- if: ${{ matrix.advanced == 'htmx' || matrix.advanced == 'tailwind' }}
4141
name: Install Templ & gen templates

.github/workflows/generate-linter-core.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ jobs:
3434
run: echo "PROJECT_DIRECTORY=${{ matrix.framework }}" | sed 's/\//-/g' >> $GITHUB_ENV
3535

3636
- name: build templates
37-
run: script -q /dev/null -c "go run main.go create -n ${{ env.PROJECT_DIRECTORY }} -f ${{ matrix.framework}} -d ${{ matrix.driver }} -g ${{ matrix.git}}" /dev/null
37+
run: script -q /dev/null -c "go run main.go create -n ${{ env.PROJECT_DIRECTORY }} -f ${{ matrix.framework}} -d ${{ matrix.driver }} -g ${{ matrix.git}}"
3838

3939
- name: golangci-lint
4040
run: |

.github/workflows/testcontainers.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
git config --global user.email 'testemail@users.noreply.github.com'
2525
2626
- name: build ${{ matrix.driver }} template
27-
run: script -q /dev/null -c "go run main.go create -n ${{ matrix.driver }} -g commit -f fiber -d ${{matrix.driver}}" /dev/null
27+
run: script -q /dev/null -c "go run main.go create -n ${{ matrix.driver }} -g commit -f fiber -d ${{matrix.driver}}"
2828

2929
- name: run ${{ matrix.driver }} integration tests
3030
working-directory: ${{ matrix.driver }}

cmd/program/program.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,32 @@ func (p *Project) CreateViteReactProject(projectPath string) error {
855855
return fmt.Errorf("failed to write App.tsx template: %w", err)
856856
}
857857

858+
// Create the global `.env` file from the template
859+
err = p.CreateFileWithInjection("", projectPath, ".env", "env")
860+
if err != nil {
861+
return fmt.Errorf("failed to create global .env file: %w", err)
862+
}
863+
864+
// Read from the global `.env` file and create the frontend-specific `.env`
865+
globalEnvPath := filepath.Join(projectPath, ".env")
866+
vitePort := "8080" // Default fallback
867+
868+
// Read the global .env file
869+
if data, err := os.ReadFile(globalEnvPath); err == nil {
870+
lines := strings.Split(string(data), "\n")
871+
for _, line := range lines {
872+
if strings.HasPrefix(line, "PORT=") {
873+
vitePort = strings.SplitN(line, "=", 2)[1] // Get the backend port value
874+
break
875+
}
876+
}
877+
}
878+
879+
// Use a template to generate the frontend .env file
880+
frontendEnvContent := fmt.Sprintf("VITE_PORT=%s\n", vitePort)
881+
if err := os.WriteFile(filepath.Join(frontendPath, ".env"), []byte(frontendEnvContent), 0644); err != nil {
882+
return fmt.Errorf("failed to create frontend .env file: %w", err)
883+
}
858884
// Handle Tailwind configuration if selected
859885
if p.AdvancedOptions[string(flags.Tailwind)] {
860886
fmt.Println("Tailwind selected. Configuring with React...")

cmd/template/advanced/files/react/app.tsx.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function App() {
77
const [count, setCount] = useState(0)
88

99
const fetchData = () => {
10-
fetch('http://localhost:8080/')
10+
fetch(`http://localhost:${import.meta.env.VITE_PORT}/`)
1111
.then(response => response.text())
1212
.then(data => setMessage(data))
1313
.catch(error => console.error('Error fetching data:', error))

cmd/template/advanced/files/react/tailwind/app.tsx.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function App() {
55
const [message, setMessage] = useState<string>('')
66

77
const fetchData = () => {
8-
fetch('http://localhost:8080/')
8+
fetch(`http://localhost:${import.meta.env.VITE_PORT}/`)
99
.then(response => response.text())
1010
.then(data => setMessage(data))
1111
.catch(error => console.error('Error fetching data:', error))

docs/docs/advanced-flag/react-vite.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ The React advanced flag can be combined with the Tailwind flag for enhanced styl
77
```bash
88
/ (Root)
99
├── frontend/ # React advanced flag. Excludes HTMX.
10+
│ ├── .env # Frontend environment configuration
1011
│ ├── node_modules/ # Node dependencies.
1112
│ ├── public/
1213
│ │ ├── index.html
@@ -209,3 +210,7 @@ volumes:
209210
networks:
210211
blueprint:
211212
```
213+
214+
## Environment Variables
215+
216+
The `VITE_PORT` in .env refers `PORT` from .env in project root ( for backend ). If value of `PORT` is changed than `VITE_PORT` must also be changed so that requests to backend work fine and have no conflicts.

0 commit comments

Comments
 (0)