github.com/go-kivik/kivik/v4@v4.3.2/x/kivikd/routes.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  //go:build !js
    14  
    15  package kivikd
    16  
    17  import (
    18  	"fmt"
    19  	"net/http"
    20  	"os"
    21  
    22  	"github.com/go-chi/chi"
    23  	"github.com/go-chi/chi/middleware"
    24  
    25  	"github.com/go-kivik/kivik/v4/x/kivikd/couchserver"
    26  	"github.com/go-kivik/kivik/v4/x/kivikd/logger"
    27  )
    28  
    29  func (s *Service) setupRoutes() (http.Handler, error) {
    30  	h := couchserver.NewHandler(s.Client)
    31  	h.Vendor = s.VendorName
    32  	h.VendorVersion = s.VendorVersion
    33  	h.Favicon = s.Favicon
    34  	h.SessionKey = SessionKey
    35  
    36  	rlog := s.RequestLogger
    37  	if rlog == nil {
    38  		rlog = logger.DefaultLogger
    39  	}
    40  
    41  	return chi.Chain(
    42  		setContext(s),
    43  		setSession(),
    44  		loggerMiddleware(rlog),
    45  		gzipHandler(s),
    46  		authHandler,
    47  	).Handler(h.Main()), nil
    48  }
    49  
    50  func gzipHandler(s *Service) func(http.Handler) http.Handler {
    51  	level := s.Conf().GetInt("httpd.compression_level")
    52  	if level == 0 {
    53  		level = 8
    54  	}
    55  	fmt.Fprintf(os.Stderr, "Enabling gzip compression, level %d\n", level)
    56  	return middleware.Compress(level, "text/plain", "text/html", "application/json")
    57  }