github.com/qubitproducts/logspray@v0.2.14/server/mux.go (about)

     1  // Copyright 2016 Qubit Digital Ltd.
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  // Package logspray is a collection of tools for streaming and indexing
    14  // large volumes of dynamic logs.
    15  
    16  package server
    17  
    18  import (
    19  	"fmt"
    20  	"io"
    21  	"mime"
    22  	"net/http"
    23  	"strings"
    24  
    25  	"github.com/QubitProducts/logspray/proto/logspray"
    26  	"github.com/grpc-ecosystem/grpc-gateway/runtime"
    27  	"github.com/tcolgate/grafanasj"
    28  	"golang.org/x/net/context"
    29  	"google.golang.org/grpc"
    30  )
    31  
    32  // grpcHandlerFunc returns an http.Handler that delegates to grpcServer on incoming gRPC
    33  // connections or otherHandler otherwise. Copied from cockroachdb.
    34  func grpcHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler {
    35  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    36  		if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
    37  			grpcServer.ServeHTTP(w, r)
    38  		} else {
    39  			otherHandler.ServeHTTP(w, r)
    40  		}
    41  	})
    42  }
    43  
    44  // HandleSwagger adds the swagger-ui to the provided mux
    45  func HandleSwagger(mux *http.ServeMux) {
    46  	mime.AddExtensionType(".svg", "image/svg+xml")
    47  
    48  	// Expose files in third_party/swagger-ui/ on <host>/swagger-ui
    49  	fileServer := http.FileServer(assetFS())
    50  	prefix := "/swagger-ui/"
    51  	mux.Handle(prefix, http.StripPrefix(prefix, fileServer))
    52  
    53  	mux.HandleFunc("/swagger-ui/swagger.json", func(w http.ResponseWriter, req *http.Request) {
    54  		lsj, _ := swaggerJsonSwaggerJsonBytes()
    55  		io.Copy(w, strings.NewReader(string(lsj)))
    56  	})
    57  }
    58  
    59  // Register sets up the log server on the provided http and grpc servers. THe
    60  // dial options will be used for all outbound gRPC reuqests.
    61  func Register(ctx context.Context, srv *http.Server, grpcServer *grpc.Server, dopts []grpc.DialOption, opts ...serverOpt) error {
    62  	lsrv := new(opts...)
    63  
    64  	logspray.RegisterLogServiceServer(grpcServer, lsrv)
    65  
    66  	gwmux := runtime.NewServeMux()
    67  
    68  	err := logspray.RegisterLogServiceHandlerFromEndpoint(ctx, gwmux, srv.Addr, dopts)
    69  	if err != nil {
    70  		fmt.Printf("serve: %v\n", err)
    71  		return err
    72  	}
    73  
    74  	sjch := grafanasj.New(lsrv.indx, grafanasj.WithGrafanaBasicAuth(lsrv.grafanaUser, lsrv.grafanaPass))
    75  
    76  	mux := http.NewServeMux()
    77  	mux.Handle("/", http.HandlerFunc(sjch.HandleRoot))
    78  	mux.Handle("/v1/", gwmux)
    79  	mux.Handle("/query", http.HandlerFunc(sjch.HandleQuery))
    80  	mux.Handle("/search", http.HandlerFunc(sjch.HandleSearch))
    81  	mux.Handle("/annotations", http.HandlerFunc(sjch.HandleAnnotations))
    82  
    83  	srv.Handler = grpcHandlerFunc(grpcServer, mux)
    84  
    85  	return nil
    86  }