package main

import (
	"github.com/joho/godotenv"
	"github.com/rs/cors"
	"log"
	"net/http"
	"portfolio/api"
	"portfolio/database"
	"portfolio/web"
)

func main() {
	// load .env in runtime environment
	err := godotenv.Load()
	if err != nil {
		log.Fatalf(".env not found: %v", err)
		return
	}

	//connect to database and migrate
	database.DB()

	//init web routes
	webMux := web.Routes()
	// Run web server
	go func() {
		err = http.ListenAndServe(":4000", 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{"*"},
		AllowCredentials: true,
	})

	//init api routes
	apiMux := api.Routes()
	//run api server
	go func() {
		err = http.ListenAndServe(":4001", c.Handler(apiMux))
		if err != nil {
			log.Fatal(err)
		}
	}()

	// block main thread
	select {}
}
