func TenantHandler(handler http.Handler, loader TenantLoader) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		name := domainutil.Subdomain(r.Host)
		if name == "" {
			http.Error(w, "missing tenant name", http.StatusBadRequest)
			return
		}
		if idx := strings.LastIndex(name, "."); idx != -1 {
			name = name[idx+1:]
		}
		ctx := r.Context()
		switch t, err := loader.Load(ctx, name); err.(type) {
		case nil:
			ctx = context.WithValue(ctx, tenantCtxKey{}, t)
			ctx = log.NewFieldsContext(ctx, zap.String("tenant", name))
			trace.FromContext(ctx).AddAttributes(
				trace.StringAttribute("tenant", name),
			)
			handler.ServeHTTP(w, r.WithContext(ctx))
		case *ent.ErrNotFound:
			http.Error(w, "tenant not found", http.StatusNotFound)
		default:
			http.Error(w, "cannot load tenant", http.StatusInternalServerError)
		}
	})
}
// TenantHandler returns a Handler that populates request tenant from subdomain.
TenantHandler returns a Handler that populates request tenant from subdomain.
// TenantHandler returns a Handler that populates request tenant from subdomain .
func TenantHandler(handler http.Handler, loader TenantLoader) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		name := domainutil.Subdomain(r.Host)
		if name == "" {
			http.Error(w, "missing tenant name", http.StatusBadRequest)
			return
		}
		if idx := strings.LastIndex(name, "."); idx != -1 {
			name = name[idx+1:]
		}
		ctx := r.Context()
		switch t, err := loader.Load(ctx, name); err.(type) {
		case nil:
			ctx = context.WithValue(ctx, tenantCtxKey{}, t)
			ctx = log.NewFieldsContext(ctx, zap.String("tenant", name))
			trace.FromContext(ctx).AddAttributes(
				trace.StringAttribute("tenant", name),
			)
			handler.ServeHTTP(w, r.WithContext(ctx))
		case *ent.ErrNotFound:
			http.Error(w, "tenant not found", http.StatusNotFound)
		default:
			http.Error(w, "cannot load tenant", http.StatusInternalServerError)
		}
	})
}
func TenantHandler ( handler http . Handler , loader TenantLoader ) http . Handler { return http . HandlerFunc ( func ( w http . ResponseWriter , r * http . Request ) { name := domainutil . Subdomain ( r . Host ) if name == " " { http . Error ( w , " " , http . StatusBadRequest ) return } if idx := strings . LastIndex ( name , " " ) ; idx != - 1 { name = name [ idx + 1 : ] } ctx := r . Context ( ) switch t , err := loader . Load ( ctx , name ) ; err . ( type ) { case nil : ctx = context . WithValue ( ctx , tenantCtxKey { } , t ) ctx = log . NewFieldsContext ( ctx , zap . String ( " " , name ) ) trace . FromContext ( ctx ) . AddAttributes ( trace . StringAttribute ( " " , name ) , ) handler . ServeHTTP ( w , r . WithContext ( ctx ) ) case * ent . ErrNotFound : http . Error ( w , " " , http . StatusNotFound ) default : http . Error ( w , " " , http . StatusInternalServerError ) } } ) }
TenantHandler returns a Handler that populates request tenant from subdomain.
TenantHandler returns a Handler that populates request tenant from subdomain .