portfolio/main.go
Darius klein 5e1cd15c07
All checks were successful
build and deploy portfolio / build (pull_request) Successful in 51s
build and deploy portfolio / publish-docs (pull_request) Successful in 4s
build and deploy portfolio / publish-portfolio (pull_request) Successful in 3s
Version bump + migration dependencies
2025-05-07 22:33:21 +02:00

74 lines
1.5 KiB
Go

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)
}
}