Skip to content

Commit d8adbd9

Browse files
committed
updated logic to rely on commandline arguments instead of ENV variables
1 parent 30d8c97 commit d8adbd9

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

Makefile

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,13 @@ check-and-copy-out:
5454
fi
5555

5656
run-backend: check-and-copy-out
57-
run-backend: export PORT = $(SERVER_PORT)
58-
run-backend: export DATASTORE_DATASET = $(PROJECT_ID)
59-
run-backend: export DATASTORE_EMULATOR_HOST = localhost:$(EMU_PORT)
60-
run-backend: export DATASTORE_EMULATOR_HOST_PATH = localhost:$(EMU_PORT)/datastore
61-
run-backend: export DATASTORE_HOST = http://localhost:$(EMU_PORT)
62-
run-backend: export DATASTORE_PROJECT_ID = $(PROJECT_ID)
6357
run-backend:
6458
@echo "Starting the backend server on port $(PORT)..."
65-
cd $(BACKEND_PATH) && go run .
59+
cd $(BACKEND_PATH) && go run . -port $(SERVER_PORT)\
60+
-project $(PROJECT_ID)\
61+
-emuHost localhost:$(EMU_PORT)\
62+
-emuHostPath localhost:$(EMU_PORT)/datastore\
63+
-dsHost http://localhost:$(EMU_PORT)
6664

6765
run-frontend:
6866
@echo "Starting the frontend server..."
@@ -85,4 +83,4 @@ clean-run:
8583
@echo "Copying out directory from frontend to backend..."
8684
@cp -r $(FRONTEND_PATH)/out $(BACKEND_PATH)/
8785

88-
setup: setup-backend setup-frontend check-and-copy-out
86+
setup: setup-backend setup-frontend check-and-copy-out

backend/main.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"context"
5+
"flag"
56
"fmt"
67
"log"
78
"os"
@@ -18,11 +19,6 @@ func frontendRouting(server *gin.Engine) {
1819
server.StaticFile("/vercel.svg", "./out/vercel.svg")
1920
}
2021

21-
// func frontendRouting(server *gin.Engine) {
22-
// server.Static("/assets", "./dist/assets")
23-
// server.StaticFile("/", "./dist/index.html")
24-
// server.StaticFile("/vite.svg", "./dist/vite.svg")
25-
// }
2622
func GetAllKindsRoute(client *datastore.Client) gin.HandlerFunc {
2723
return func(c *gin.Context) {
2824
ctx := context.Background()
@@ -83,13 +79,38 @@ func CORSMiddleware() gin.HandlerFunc {
8379
}
8480
}
8581

86-
func main() {
82+
func usage() {
83+
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
84+
flag.PrintDefaults()
85+
}
8786

88-
port := os.Getenv("PORT")
87+
func main() {
8988

90-
if port == "" {
91-
log.Fatal("$PORT must be set")
89+
files, err := content.ReadDir("out")
90+
if err != nil {
91+
fmt.Println("Error reading embedded directory:", err)
92+
} else {
93+
fmt.Println("Files in embedded directory:")
94+
for _, file := range files {
95+
fmt.Println(" - ", file.Name())
96+
}
9297
}
98+
port := flag.String("port", "8080", "Port for the emulator")
99+
projectId := flag.String("project", "my-project", "Project ID for the emulator")
100+
emulatorHost := flag.String("emuHost", "localhost:8081", "Host for the emulator")
101+
emulatorHostPath := flag.String("emuHostPath", "localhost:8081/datastore", "Host path for the emulator")
102+
datastoreHost := flag.String("dsHost", "http://localhost:8081", "Host for the datastore")
103+
104+
flag.Usage = usage
105+
106+
flag.Parse()
107+
108+
os.Setenv("DATASTORE_DATASET", *projectId)
109+
os.Setenv("DATASTORE_EMULATOR_HOST", *emulatorHost)
110+
os.Setenv("DATASTORE_EMULATOR_HOST_PATH", *emulatorHostPath)
111+
os.Setenv("DATASTORE_HOST", *datastoreHost)
112+
os.Setenv("DATASTORE_PROJECT_ID", *projectId)
113+
93114
fmt.Println("Starting server on port:", port)
94115

95116
ctx := context.Background()
@@ -110,7 +131,7 @@ func main() {
110131
server.GET("/api/kinds", GetAllKindsRoute(client))
111132
server.GET("/api/entities/:kind/", GetAllEntitiesRoute(client))
112133

113-
if err := server.Run(":" + port); err != nil {
134+
if err := server.Run(":" + *port); err != nil {
114135
log.Fatal("Error starting server: ", err)
115136
}
116137

0 commit comments

Comments
 (0)