github.com/ethersphere/bee/v2@v2.2.0/pkg/api/subdomain.go (about)

     1  // Copyright 2022 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package api
     6  
     7  import (
     8  	"net/http"
     9  	"strings"
    10  
    11  	"github.com/ethersphere/bee/v2/pkg/swarm"
    12  	"github.com/ethersphere/bee/v2/pkg/tracing"
    13  	"github.com/gorilla/mux"
    14  )
    15  
    16  func (s *Service) subdomainHandler(w http.ResponseWriter, r *http.Request) {
    17  	logger := tracing.NewLoggerWithTraceID(r.Context(), s.logger.WithName("get_subdomain").Build())
    18  
    19  	paths := struct {
    20  		Subdomain swarm.Address `map:"subdomain,resolve" validate:"required"`
    21  		Path      string        `map:"path"`
    22  	}{}
    23  	if response := s.mapStructure(mux.Vars(r), &paths); response != nil {
    24  		response("invalid path params", logger, w)
    25  		return
    26  	}
    27  	if strings.HasSuffix(paths.Path, "/") {
    28  		paths.Path = strings.TrimRight(paths.Path, "/") + "/" // NOTE: leave one slash if there was some.
    29  	}
    30  
    31  	s.serveReference(logger, paths.Subdomain, paths.Path, w, r, false)
    32  }