-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
48 lines (37 loc) · 1.11 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
package main
import (
"fmt"
"log"
"net/http"
"os"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/joho/godotenv"
database "github.com/sahilchauhan0603/backend/config"
routes "github.com/sahilchauhan0603/backend/routes"
)
func main() {
// Load environment variables
err := godotenv.Load()
if err != nil {
log.Printf("Error loading .env file: %v", err)
}
// Initialize database connection
database.DatabaseConnector()
// Create a new router
router := mux.NewRouter()
routes.InitializeRoutes(router)
// Enable CORS
cors := handlers.CORS(
handlers.AllowedOrigins([]string{"*"}), // Change to specific origins in production
handlers.AllowedMethods([]string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}),
handlers.AllowedHeaders([]string{"Authorization", "Content-Type"}),
)
// Set the port for the server
port := os.Getenv("PORT")
if port == "" {
port = "8080" // default port if not specified
}
fmt.Printf("Server is running on port %s\n", port)
log.Fatalf("Failed to start server: %v", http.ListenAndServe(fmt.Sprintf(":%s", port), cors(router)))
}