portfolio/main.go
darius ee1be74eba
All checks were successful
build and deploy portfolio / build (push) Successful in 37s
build and deploy portfolio / publish-portfolio (push) Successful in 3s
build and deploy portfolio / publish-docs (push) Successful in 5s
cors
2025-02-25 22:48:44 +01:00

56 lines
1.0 KiB
Go

package main
import (
"github.com/joho/godotenv"
"github.com/rs/cors"
"log"
"net/http"
"portfolio/api"
"portfolio/database"
"portfolio/web"
)
func main() {
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
err = http.ListenAndServe(":4001", c.Handler(apiMux))
if err != nil {
log.Fatal(err)
}
}