package main import ( "fmt" "github.com/joho/godotenv" "github.com/rs/cors" "log" "net/http" "os" "os/signal" "portfolio/api" "portfolio/database" "portfolio/web" "syscall" ) func main() { log.Println("Starting application") err := godotenv.Load() if err != nil { log.Fatalf(".env not found: %v", err) return } // Catch interrupt cmd := make(chan os.Signal, 1) signal.Notify(cmd, os.Interrupt, syscall.SIGTERM) go func() { <-cmd log.Println("Shutting down application") os.Exit(0) }() //connect to database and migrate database.DB() //init web routes webMux := web.Routes() // Run web server webPort := os.Getenv("WEB_PORT") log.Printf("Starting API port [%s]", webPort) go func() { err := http.ListenAndServe(fmt.Sprintf(":%s", webPort), cors.AllowAll().Handler(webMux)) if err != nil { log.Fatal(err) } }() c := cors.New(cors.Options{ AllowedOrigins: []string{"http://localhost:4000", "https://*.dariusklein.nl", "https://*.portfolio.dariusklein.nl", "https://dariusklein.nl"}, AllowedMethods: []string{ http.MethodHead, http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete, }, AllowedHeaders: []string{"Authorization", "Content-Type", "*"}, AllowCredentials: true, Debug: true, }) //init api routes apiMux := api.Routes() //run api server apiPort := os.Getenv("API_PORT") log.Printf("Starting API port [%s]", apiPort) err = http.ListenAndServe(fmt.Sprintf(":%s", apiPort), c.Handler(apiMux)) if err != nil { log.Fatal(err) } }