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 } server.ActiveBusiness = entity.ActiveBusiness{ BusinessID: domainModel.BusinessID, Domain: domainModel.Domain, DomainID: domainModel.ID, } c.Set("activeBusiness", &server.ActiveBusiness) c.Next() } }