online-order/api/middlewares/setDomainDetails.go

50 lines
1.2 KiB
Go
Raw Permalink Normal View History

2023-10-29 23:33:40 +00:00
package middlewares
import (
"github.com/gin-gonic/gin"
"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)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "domain not Found"})
c.Abort()
return
}
2023-10-31 16:31:06 +00:00
server.ActiveBusiness = entity.ActiveBusiness{
BusinessID: domainModel.BusinessID,
Domain: domainModel.Domain,
DomainID: domainModel.ID,
}
c.Set("activeBusiness", &server.ActiveBusiness)
2023-10-29 23:33:40 +00:00
c.Next()
}
}