Files
delivery-tracker/backend/cmd/api/main.go
Egor Pozharov 7f410e814b add CORS
2026-04-14 16:17:22 +06:00

57 lines
1.3 KiB
Go

package main
import (
"context"
"log"
"net/http"
"os"
"time"
db "github.com/chedius/delivery-tracker/internal/db/sqlc"
"github.com/chedius/delivery-tracker/internal/delivery"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/joho/godotenv"
)
func main() {
ctx := context.Background()
godotenv.Load()
dsn := os.Getenv("DATABASE_URL")
pool, err := pgxpool.New(ctx, dsn)
if err != nil {
log.Fatalf("db connect: %v", err)
}
defer pool.Close()
queries := db.New(pool)
h := delivery.NewHandler(queries)
r := gin.Default()
// CORS middleware - allow all origins in development
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"},
AllowMethods: []string{"GET", "POST", "PATCH", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"*"},
AllowCredentials: false,
MaxAge: 12 * time.Hour,
}))
r.GET("/health", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "ok"})
})
r.GET("/api/deliveries", h.GetDeliveries)
r.GET("/api/deliveries/:id", h.GetDeliveryByID)
r.GET("/api/deliveries/count", h.GetDeliveryCount)
r.POST("/api/deliveries", h.CreateDelivery)
r.PATCH("/api/deliveries/:id", h.UpdateDelivery)
r.PATCH("/api/deliveries/:id/status", h.UpdateDeliveryStatus)
r.DELETE("/api/deliveries/:id", h.DeleteDelivery)
r.Run(":8080")
}