online-order/api/middlewares/setDomainDetails.go
2023-10-30 03:03:40 +03:30

46 lines
1.1 KiB
Go

package middlewares
import (
"github.com/gin-gonic/gin"
"log"
"net/http"
"online-order/configs"
"online-order/domain"
"online-order/entity"
repository_domain "online-order/repository/domain"
service_domain "online-order/usecase/domain"
"strings"
)
func DomainMiddleware(server *entity.Routers) gin.HandlerFunc {
return func(c *gin.Context) {
// get domain from request
conf := configs.LoadConfigEnv()
hostName := strings.Replace(c.Request.Host, "www.", "", 1)
domainName := strings.Replace(hostName, ":"+conf.ServerPort, "", 1)
if domainName == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "domain not valid"})
c.Abort()
return
}
domainRepo := repository_domain.NewDomainClient(server.Database)
domainService := service_domain.NewDomainService(domainRepo)
// get domain from database
domainModel, err := domain.DomainService.GetByDomain(domainService, domainName)
log.Printf("domainModel name: %v", domainModel)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "domain not Found"})
c.Abort()
return
}
c.Set("domain", domainModel)
c.Next()
}
}