46 lines
1.0 KiB
Go
46 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
|
|
db "github.com/chedius/delivery-tracker/internal/db/sqlc"
|
|
"github.com/chedius/delivery-tracker/internal/delivery"
|
|
"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()
|
|
|
|
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")
|
|
}
|