Skip to content

Commit 2550c02

Browse files
committed
feat: Update Makefile and server configuration; improve CORS settings and static file serving
1 parent ce0b3ef commit 2550c02

5 files changed

Lines changed: 50 additions & 60 deletions

File tree

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ build:
1111

1212
# Run the application
1313
run:
14-
@go run cmd/api/main.go &
15-
@npm install --prefer-offline --no-fund --prefix ./frontend
16-
@npm run dev --prefix ./frontend
14+
@go run cmd/api/main.go
15+
# @npm install --prefer-offline --no-fund --prefix ./frontend
16+
# @npm run dev --prefix ./frontend
1717
# Create DB container
1818
docker-run:
1919
@if docker compose up --build 2>/dev/null; then \

cmd/api/main.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ import (
99
"syscall"
1010
"time"
1111
"trakrlog/internal/server"
12-
13-
"github.com/gin-contrib/static"
14-
"github.com/gin-gonic/gin"
1512
)
1613

1714
func gracefulShutdown(apiServer *http.Server, done chan bool) {
@@ -52,23 +49,13 @@ func main() {
5249
// store.Options.Secure = false // set to true in production (HTTPS)
5350
// store.Options.SameSite = http.SameSiteLaxMode // helps prevent CSRF
5451

55-
// Serve static files
56-
var router = server.Handler.(*gin.Engine)
57-
router.Use(static.Serve("/", static.LocalFile("../apps/frontend/dist", true)))
58-
59-
// Serve React app for client-side routes (SPA fallback) - only for non-dashboard routes
60-
router.NoRoute(func(c *gin.Context) {
61-
c.File("../apps/frontend/dist/index.html")
62-
})
63-
6452
// Create a done channel to signal when the shutdown is complete
6553
done := make(chan bool, 1)
6654

6755
// Run graceful shutdown in a separate goroutine
6856
go gracefulShutdown(server, done)
6957

7058
// Start the server
71-
log.Println("Starting server on localhost:4000")
7259
err := server.ListenAndServe()
7360
if err != nil && err != http.ErrServerClosed {
7461
panic(fmt.Sprintf("http server error: %s", err))

internal/server/routes.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,34 @@ import (
44
"net/http"
55

66
"github.com/gin-contrib/cors"
7+
"github.com/gin-contrib/static"
78
"github.com/gin-gonic/gin"
89
)
910

1011
func (s *Server) RegisterRouter() http.Handler {
1112
router := gin.Default()
1213

1314
router.Use(cors.New(cors.Config{
14-
AllowOrigins: []string{"http://localhost:5173"}, // Add your frontend URL
15+
AllowOrigins: []string{"http://localhost:4000", "https://trakrlog.com", "https://www.trakrlog.com"}, // Add your frontend URL
1516
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"},
1617
AllowHeaders: []string{"Accept", "Authorization", "Content-Type"},
1718
AllowCredentials: true, // Enable cookies/auth
1819
}))
1920

20-
router.GET("/", s.HelloWorldHandler)
21+
// Serve static files from the React app build directory
22+
router.Use(static.Serve("/", static.LocalFile("frontend/dist", true)))
2123

22-
router.GET("/health", s.healthHandler)
24+
// Serve React app for client-side routes (SPA fallback) - only for non-dashboard routes
25+
router.NoRoute(func(c *gin.Context) {
26+
c.File("frontend/dist/index.html")
27+
})
2328

2429
return router
2530
}
2631

27-
func (s *Server) HelloWorldHandler(c *gin.Context) {
28-
resp := make(map[string]string)
29-
resp["message"] = "Hello World"
32+
// func (s *Server) HelloWorldHandler(c *gin.Context) {
33+
// resp := make(map[string]string)
34+
// resp["message"] = "Hello World"
3035

31-
c.JSON(http.StatusOK, resp)
32-
}
33-
34-
func (s *Server) healthHandler(c *gin.Context) {
35-
c.JSON(http.StatusOK, s.db.Health())
36-
}
36+
// c.JSON(http.StatusOK, resp)
37+
// }

internal/server/routes_test.go

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,25 @@
11
package server
22

3-
import (
4-
"github.com/gin-gonic/gin"
5-
"net/http"
6-
"net/http/httptest"
7-
"testing"
8-
)
9-
10-
func TestHelloWorldHandler(t *testing.T) {
11-
s := &Server{}
12-
r := gin.New()
13-
r.GET("/", s.HelloWorldHandler)
14-
// Create a test HTTP request
15-
req, err := http.NewRequest("GET", "/", nil)
16-
if err != nil {
17-
t.Fatal(err)
18-
}
19-
// Create a ResponseRecorder to record the response
20-
rr := httptest.NewRecorder()
21-
// Serve the HTTP request
22-
r.ServeHTTP(rr, req)
23-
// Check the status code
24-
if status := rr.Code; status != http.StatusOK {
25-
t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusOK)
26-
}
27-
// Check the response body
28-
expected := "{\"message\":\"Hello World\"}"
29-
if rr.Body.String() != expected {
30-
t.Errorf("Handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
31-
}
32-
}
3+
// func TestHelloWorldHandler(t *testing.T) {
4+
// s := &Server{}
5+
// r := gin.New()
6+
// r.GET("/", s.HelloWorldHandler)
7+
// // Create a test HTTP request
8+
// req, err := http.NewRequest("GET", "/", nil)
9+
// if err != nil {
10+
// t.Fatal(err)
11+
// }
12+
// // Create a ResponseRecorder to record the response
13+
// rr := httptest.NewRecorder()
14+
// // Serve the HTTP request
15+
// r.ServeHTTP(rr, req)
16+
// // Check the status code
17+
// if status := rr.Code; status != http.StatusOK {
18+
// t.Errorf("Handler returned wrong status code: got %v want %v", status, http.StatusOK)
19+
// }
20+
// // Check the response body
21+
// expected := "{\"message\":\"Hello World\"}"
22+
// if rr.Body.String() != expected {
23+
// t.Errorf("Handler returned unexpected body: got %v want %v", rr.Body.String(), expected)
24+
// }
25+
// }

internal/server/server.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package server
22

33
import (
44
"fmt"
5+
"log"
56
"net/http"
7+
"os"
8+
"strconv"
69
"time"
710

8-
_ "github.com/joho/godotenv/autoload"
11+
"github.com/joho/godotenv"
912

1013
"trakrlog/internal/database"
1114
)
@@ -17,7 +20,13 @@ type Server struct {
1720
}
1821

1922
func NewServer() *http.Server {
20-
port := 4000 // strconv.Atoi(os.Getenv("PORT"))
23+
// Load .env file explicitly
24+
if err := godotenv.Load(); err != nil {
25+
log.Println("Warning: .env file not found, using system environment variables")
26+
//panic(err)
27+
}
28+
29+
port, _ := strconv.Atoi(os.Getenv("PORT"))
2130
fmt.Printf("Starting server on port %d\n", port)
2231
NewServer := &Server{
2332
port: port,

0 commit comments

Comments
 (0)