-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
124 lines (109 loc) · 3.21 KB
/
main.go
File metadata and controls
124 lines (109 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/Harshjosh361/GopherPing/config"
"github.com/Harshjosh361/GopherPing/migrations"
"github.com/Harshjosh361/GopherPing/routes"
"github.com/Harshjosh361/GopherPing/service"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
)
func main() {
appEnv := os.Getenv("APP_ENV")
if appEnv == "" {
appEnv = "development"
}
// Only load .env file in development environment
if appEnv == "development" {
if err := godotenv.Load(); err != nil {
log.Fatal("Failed to load env")
}
log.Println("Loaded .env file for development environment")
} else {
log.Printf("Running in %s environment - skipping .env file loading", appEnv)
}
// db
config.ConnectDb()
// migrate tables
migrations.Run()
config.InitSendGrid()
// Monitoring - start worker in a goroutine to avoid blocking
log.Println("Starting monitoring worker...")
worker := service.GetMonitorWorker()
go worker.Start()
log.Println("Monitoring worker started in background")
r := gin.Default()
// CORS configuration: allow localhost dev and Vercel domains
frontendBase := os.Getenv("FRONTEND_BASE_URL")
allowed := []string{"http://localhost:3000", "https://gopher-ping.vercel.app"}
if frontendBase != "" {
allowed = append(allowed, frontendBase)
}
corsConfig := cors.Config{
AllowOrigins: allowed,
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Content-Type", "Authorization"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
}
// Also allow any *.vercel.app preview deployments
corsConfig.AllowOriginFunc = func(origin string) bool {
if strings.HasSuffix(origin, ".vercel.app") {
return true
}
for _, o := range allowed {
if origin == o {
return true
}
}
return false
}
r.Use(cors.New(corsConfig))
api := r.Group("/api")
// register routes
routes.AuthRoute(api)
routes.MonitorRoutes(api)
log.Println("Routes registered successfully")
port := os.Getenv("PORT")
if port == "" {
port = "8000"
}
// http server with graceful shutdown
srv := &http.Server{
Addr: "0.0.0.0:" + port,
Handler: r,
}
// channel to catch server errors
serverErrors := make(chan error, 1)
// start server in a separate goroutine
go func() {
log.Printf("Server listening on %s", srv.Addr)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
serverErrors <- err
}
}()
// wait for termination signal or server error
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
select {
case err := <-serverErrors:
log.Fatalf("Server failed to start: %v", err)
case <-quit:
log.Println("Shutdown signal received, shutting down server...")
// graceful shutdown with timeout
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
log.Fatalf("Server forced to shutdown: %v", err)
}
log.Println("Server exited gracefully")
}
}