-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (123 loc) · 3.56 KB
/
main.go
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/OliPou/s3are/internal/common"
"github.com/OliPou/s3are/internal/database"
"github.com/OliPou/s3are/middleware"
s3uploadfile "github.com/OliPou/s3are/s3UploadFile"
"github.com/OliPou/s3are/s3client"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
_ "github.com/lib/pq"
)
func main() {
// Set Gin to release mode
gin.SetMode(gin.ReleaseMode)
// Load environment variables
if err := godotenv.Load(); err != nil {
fmt.Println("Error loading .env file:", err)
// Continue execution as .env file might not exist in production
}
// Get PORT from environment variables with default fallback
portString := os.Getenv("PORT")
if portString == "" {
portString = "8080"
}
var ginRouterGroupName string = os.Getenv("GIN_ROUTER_GROUP_NAME")
dbUrl := os.Getenv("DB_URL")
if dbUrl == "" {
log.Fatal("DB_URL not found in environment variables")
}
db, err := sql.Open("postgres", dbUrl)
if err != nil {
log.Fatal("Failed to connect to database:", err)
}
// Check if the database is ready
if err := checkDatabase(db); err != nil {
log.Fatal("Database is not ready:", err)
}
region := os.Getenv("AWS_REGION")
bucket := os.Getenv("S3_BUCKET")
if region == "" || bucket == "" {
log.Fatal("AWS_REGION or S3_BUCKET not found in environment variables")
}
s3Client, err := s3client.NewS3Client(region, bucket)
if err != nil {
log.Fatal(err)
}
dbQueries := database.New(db)
apiCfg := &s3uploadfile.ApiConfig{
DB: dbQueries,
S3Client: s3Client,
}
fmt.Printf("Server starting on port: %s\n", portString)
// Initialize the router
router := gin.Default()
// Configure CORS
config := cors.DefaultConfig()
// Get allowed origins from environment variable or use default
allowedOrigins := os.Getenv("ALLOWED_ORIGINS")
if allowedOrigins == "" {
// For development, you might want to allow all origins
config.AllowAllOrigins = true
} else {
config.AllowOrigins = []string{allowedOrigins}
}
// Additional CORS configurations
config.AllowMethods = []string{"GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}
config.AllowHeaders = []string{
"Origin",
"Content-Length",
"Content-Type",
"Authorization",
}
config.AllowCredentials = true
config.ExposeHeaders = []string{"Content-Length"}
config.MaxAge = 12 * 60 * 60 // 12 hours
// Add CORS middleware
router.Use(cors.New(config))
// Define routes
// router.GET("/ping", func(c *gin.Context) {
// c.JSON(200, gin.H{
// "message": "pong",
// })
// })
v1Router := router.Group(fmt.Sprintf("/%s", ginRouterGroupName))
v1Router.GET("/healthz", handlerHealthz)
v1Router.POST("/upload-file-request", middleware.Auth(apiCfg.HandlerRequestUpload))
v1Router.PUT("/file-uploaded", middleware.Auth(apiCfg.HandlerRequestUploadCompleted))
v1Router.GET("/file-status", middleware.Auth(apiCfg.HandlerFileStatus))
// Start the server
if err := router.Run(":" + portString); err != nil {
fmt.Printf("Failed to start server: %v\n", err)
os.Exit(1)
}
}
// checkDatabase tries to ping the database until it succeeds or times out
func checkDatabase(db *sql.DB) error {
for i := 0; i < 10; i++ {
err := db.Ping()
if err == nil {
return nil
}
fmt.Println("Waiting for database to be ready...")
time.Sleep(2 * time.Second)
}
return fmt.Errorf("database is not ready")
}
func handlerHealthz(c *gin.Context) {
status := struct {
Status string `json:"status"`
Ready bool `json:"ready"`
}{
Status: "ok",
Ready: true,
}
common.RespondWithJSON(c, http.StatusOK, status)
}