portfolio/main.go

74 lines
1.5 KiB
Go
Raw Permalink Normal View History

2024-02-13 18:45:08 +01:00
package main
import (
2025-05-07 22:33:21 +02:00
"fmt"
2025-02-24 00:18:04 +01:00
"github.com/joho/godotenv"
2024-05-19 23:56:53 +02:00
"github.com/rs/cors"
2025-02-24 00:18:04 +01:00
"log"
"net/http"
2025-05-07 22:33:21 +02:00
"os"
"os/signal"
2024-05-16 18:42:31 +02:00
"portfolio/api"
2024-05-16 17:36:44 +02:00
"portfolio/database"
2024-05-16 18:42:31 +02:00
"portfolio/web"
2025-05-07 22:33:21 +02:00
"syscall"
)
2024-02-13 18:45:08 +01:00
func main() {
2025-05-07 22:33:21 +02:00
log.Println("Starting application")
2025-02-24 00:18:04 +01:00
err := godotenv.Load()
if err != nil {
log.Fatalf(".env not found: %v", err)
return
}
2024-05-15 15:49:39 +02:00
2025-05-07 22:33:21 +02:00
// 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()
2024-05-16 18:42:31 +02:00
//init web routes
2025-02-24 00:18:04 +01:00
webMux := web.Routes()
2024-05-16 18:42:31 +02:00
// Run web server
2025-05-07 22:33:21 +02:00
webPort := os.Getenv("WEB_PORT")
log.Printf("Starting API port [%s]", webPort)
2025-02-24 00:18:04 +01:00
go func() {
2025-05-07 22:33:21 +02:00
err := http.ListenAndServe(fmt.Sprintf(":%s", webPort), cors.AllowAll().Handler(webMux))
2025-02-24 00:18:04 +01:00
if err != nil {
log.Fatal(err)
}
}()
2024-05-19 23:56:53 +02:00
c := cors.New(cors.Options{
2024-06-25 01:46:54 +02:00
AllowedOrigins: []string{"http://localhost:4000", "https://*.dariusklein.nl", "https://*.portfolio.dariusklein.nl", "https://dariusklein.nl"},
2024-05-19 23:56:53 +02:00
AllowedMethods: []string{
http.MethodHead,
http.MethodGet,
http.MethodPost,
http.MethodPut,
http.MethodPatch,
http.MethodDelete,
},
2025-02-25 22:12:29 +01:00
AllowedHeaders: []string{"Authorization", "Content-Type", "*"},
2024-05-19 23:56:53 +02:00
AllowCredentials: true,
2025-02-25 22:12:29 +01:00
Debug: true,
2024-05-19 23:56:53 +02:00
})
2024-05-16 18:42:31 +02:00
//init api routes
2025-02-24 00:18:04 +01:00
apiMux := api.Routes()
2024-05-16 18:42:31 +02:00
//run api server
2025-05-07 22:33:21 +02:00
apiPort := os.Getenv("API_PORT")
log.Printf("Starting API port [%s]", apiPort)
err = http.ListenAndServe(fmt.Sprintf(":%s", apiPort), c.Handler(apiMux))
2025-02-25 22:12:29 +01:00
if err != nil {
log.Fatal(err)
}
2024-02-13 18:45:08 +01:00
}