Repository: ChaosChain/chaoschain-launchpad
Pull request #109: Chore/support windows env file
Author: login: ginrod
Labels: name: bug
description: Something isn't working
Created: 2025-03-28T21:59:28Z | Merged: 2025-03-29T18:56:50Z

closes #107

Linked issue #107: NODE_ENV=production syntax not working on Windows
Most Windows command prompts will choke when you set environment variables with NODE_ENV=production like that. (The exception is [Bash on Windows](https://msdn.microsoft.com/en-us/commandline/wsl/about), which uses native Bash.)

Thus:  `PORT=4000 next dev --turbopack` and `PORT=4000 next start` will fail when running on Windows

We've agreed to use a separate environment file specifically for Windows systems

See more about it in [ChaosChain's Telegarm Group](https://t.me/thechaoschain/1451)

Diff:
diff --git a/api/routes.go b/api/routes.go
index 3b5c07c..5e1cb10 100644
--- a/api/routes.go
+++ b/api/routes.go
@@ -1,6 +1,8 @@
 package api
 
 import (
+	"os"
+
 	"github.com/NethermindEth/chaoschain-launchpad/api/handlers"
 	"github.com/gin-gonic/gin"
 )
@@ -22,7 +24,8 @@ func chainIDMiddleware(chainID string) gin.HandlerFunc {
 func SetupRoutes(router *gin.Engine, chainID string) {
 	// Add CORS middleware
 	router.Use(func(c *gin.Context) {
-		c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost:4000")
+		port := os.Getenv("PORT")
+		c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost:"+port)
 		c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
 		c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, Authorization, X-Chain-Id")
 
diff --git a/client/agent-launchpad/package-lock.json b/client/agent-launchpad/package-lock.json
index 711cf81..6024302 100644
--- a/client/agent-launchpad/package-lock.json
+++ b/client/agent-launchpad/package-lock.json
@@ -22,6 +22,7 @@
         "@types/react": "^19",
         "@types/react-dom": "^19",
         "autoprefixer": "^10.4.21",
+        "dotenv-cli": "^8.0.0",
         "eslint": "^9",
         "eslint-config-next": "15.2.1",
         "tailwindcss": "^4.0.13",
@@ -2111,6 +2112,45 @@
         "node": ">=0.10.0"
       }
     },
+    "node_modules/dotenv": {
+      "version": "16.4.7",
+      "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.4.7.tgz",
+      "integrity": "sha512-47qPchRCykZC03FhkYAhrvwU4xDBFIj1QPqaarj6mdM/hgUzfPHcpkHJOn3mJAufFeeAxAzeGsr5X0M4k6fLZQ==",
+      "dev": true,
+      "license": "BSD-2-Clause",
+      "engines": {
+        "node": ">=12"
+      },
+      "funding": {
+        "url": "https://dotenvx.com"
+      }
+    },
+    "node_modules/dotenv-cli": {
+      "version": "8.0.0",
+      "resolved": "https://registry.npmjs.org/dotenv-cli/-/dotenv-cli-8.0.0.tgz",
+      "integrity": "sha512-aLqYbK7xKOiTMIRf1lDPbI+Y+Ip/wo5k3eyp6ePysVaSqbyxjyK3dK35BTxG+rmd7djf5q2UPs4noPNH+cj0Qw==",
+      "dev": true,
+      "license": "MIT",
+      "dependencies": {
+        "cross-spawn": "^7.0.6",
+        "dotenv": "^16.3.0",
+        "dotenv-expand": "^10.0.0",
+        "minimist": "^1.2.6"
+      },
+      "bin": {
+        "dotenv": "cli.js"
+      }
+    },
+    "node_modules/dotenv-expand": {
+      "version": "10.0.0",
+      "resolved": "https://registry.npmjs.org/dotenv-expand/-/dotenv-expand-10.0.0.tgz",
+      "integrity": "sha512-GopVGCpVS1UKH75VKHGuQFqS1Gusej0z4FyQkPdwjil2gNIv+LNsqBlboOzpJFZKVT95GkCyWJbBSdFEFUWI2A==",
+      "dev": true,
+      "license": "BSD-2-Clause",
+      "engines": {
+        "node": ">=12"
+      }
+    },
     "node_modules/dunder-proto": {
       "version": "1.0.1",
       "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz",
diff --git a/client/agent-launchpad/package.json b/client/agent-launchpad/package.json
index ce083f3..32772d2 100644
--- a/client/agent-launchpad/package.json
+++ b/client/agent-launchpad/package.json
@@ -3,9 +3,9 @@
   "version": "0.1.0",
   "private": true,
   "scripts": {
-    "dev": "PORT=4000 next dev --turbopack",
+    "dev": "dotenv -c -- next dev --turbopack",
     "build": "next build",
-    "start": "PORT=4000 next start",
+    "start": "dotenv -c -- next start",
     "lint": "next lint"
   },
   "dependencies": {
@@ -23,6 +23,7 @@
     "@types/react": "^19",
     "@types/react-dom": "^19",
     "autoprefixer": "^10.4.21",
+    "dotenv-cli": "^8.0.0",
     "eslint": "^9",
     "eslint-config-next": "15.2.1",
     "tailwindcss": "^4.0.13",
diff --git a/cmd/main.go b/cmd/main.go
index 9036332..f7de4b0 100644
--- a/cmd/main.go
+++ b/cmd/main.go
@@ -13,6 +13,7 @@ import (
 	"github.com/NethermindEth/chaoschain-launchpad/mempool"
 	"github.com/NethermindEth/chaoschain-launchpad/p2p"
 	"github.com/gin-gonic/gin"
+	"github.com/joho/godotenv"
 )
 
 func main() {
@@ -64,4 +65,11 @@ func main() {
 	router := gin.New()
 	api.SetupRoutes(router, *chainID)
 	log.Fatal(router.Run(fmt.Sprintf(":%d", *apiPort)))
+
+	// Load front-end port env variable
+	err := godotenv.Load("../client/agent-launchpad/.env")
+	if err != nil {
+		log.Fatal("Error loading .env file")
+	}
+
 }


Reviews:
user: login: Deeptanshu-sankhwar
body: LGTM! 🚀 
state: APPROVED
author_association: CONTRIBUTOR
submitted_at: 2025-03-29T18:54:03Z
user: login: ginrod
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-03-29T18:58:06Z
user: login: Deeptanshu-sankhwar
state: COMMENTED
author_association: CONTRIBUTOR
submitted_at: 2025-03-29T19:00:50Z

Review comments:
diff_hunk: @@ -22,7 +24,8 @@ func chainIDMiddleware(chainID string) gin.HandlerFunc {
 func SetupRoutes(router *gin.Engine, chainID string) {
 	// Add CORS middleware
 	router.Use(func(c *gin.Context) {
-		c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost:4000")
+		port := os.Getenv("PORT")
+		c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost:"+port)
path: api/routes.go
user: login: ginrod
body: OH, I see I did not push a rollback for this change, as I mentioned [here](https://t.me/thechaoschain/1482) the `port` variable in Go is not getting the port. I've zero knowledge of Go. I would need your help on this issue

![image](https://github.com/user-attachments/assets/8b684e7e-9973-4609-9591-86a3cd72d21d)



created_at: 2025-03-29T18:58:06Z
author_association: CONTRIBUTOR
diff_hunk: @@ -22,7 +24,8 @@ func chainIDMiddleware(chainID string) gin.HandlerFunc {
 func SetupRoutes(router *gin.Engine, chainID string) {
 	// Add CORS middleware
 	router.Use(func(c *gin.Context) {
-		c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost:4000")
+		port := os.Getenv("PORT")
+		c.Writer.Header().Set("Access-Control-Allow-Origin", "http://localhost:"+port)
path: api/routes.go
user: login: Deeptanshu-sankhwar
body: that's fine @ginrod , we just need to set it in .env at root directory for our go project. I won't stress about it, we need to add `PORT=` in the .env for go, if you wish, you can commit an `.env.example`, or else I can push it today!
created_at: 2025-03-29T19:00:50Z
author_association: CONTRIBUTOR

PR comments:
user: login: Deeptanshu-sankhwar
created_at: 2025-03-29T04:07:48Z
body: This is importing the standard dotenv package, we would need it ahead too. LGTM

please review @shenhcs @Keiii25 
author_association: CONTRIBUTOR
user: login: Deeptanshu-sankhwar
created_at: 2025-03-29T04:08:30Z
body: I hope these changes are tested @ginrod ? :)
author_association: CONTRIBUTOR
user: login: ginrod
created_at: 2025-03-29T18:46:29Z
body: > I hope these changes are tested @ginrod ? :)

TEST in Windows. The port 4000 was loaded from .env file
![image](https://github.com/user-attachments/assets/b7e92ab8-fb7b-45d6-b673-5279fe7a060f)

author_association: CONTRIBUTOR
user: login: Deeptanshu-sankhwar
created_at: 2025-03-29T18:56:43Z
body: It's working fine on mac for me @ginrod. I gonna merge the PR now, congrats on your first contribution 🎉 

Later - let's have github workflow actions so we don't need to test it cross environments - feel free to take a shot at #31 if you're interested @ginrod :)
author_association: CONTRIBUTOR