kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/go/test/tools/http_server/http_server.go (about)

     1  /*
     2   * Copyright 2015 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  // Binary http_server exposes an HTTP interface for testing the xrefs
    18  // and filetree services backed by a combined serving table.  The server places
    19  // the port on which it listens into the given --port_file.  Requesting
    20  // "http://localhost:$(<"$PORT_FILE")/quitquitquit" will forcibly exit the
    21  // server successfully.  Requesting /alive will return a 200 HTTP status once
    22  // the server is launched.
    23  package main
    24  
    25  import (
    26  	"context"
    27  	"flag"
    28  	"fmt"
    29  	"io/ioutil"
    30  	"net"
    31  	"net/http"
    32  	"os"
    33  
    34  	"kythe.io/kythe/go/services/filetree"
    35  	"kythe.io/kythe/go/services/graph"
    36  	"kythe.io/kythe/go/services/web"
    37  	"kythe.io/kythe/go/services/xrefs"
    38  	ftsrv "kythe.io/kythe/go/serving/filetree"
    39  	gsrv "kythe.io/kythe/go/serving/graph"
    40  	xsrv "kythe.io/kythe/go/serving/xrefs"
    41  	"kythe.io/kythe/go/storage/leveldb"
    42  	"kythe.io/kythe/go/storage/table"
    43  	"kythe.io/kythe/go/util/log"
    44  )
    45  
    46  var (
    47  	servingTable = flag.String("serving_table", "", "LevelDB serving table")
    48  	portFile     = flag.String("port_file", "", "File to output listening port")
    49  )
    50  
    51  func main() {
    52  	flag.Parse()
    53  	if *servingTable == "" {
    54  		log.Fatal("Missing --serving_table argument")
    55  	} else if *portFile == "" {
    56  		log.Fatal("Missing --port_file argument")
    57  	}
    58  
    59  	ctx := context.Background()
    60  	db, err := leveldb.Open(*servingTable, nil)
    61  	if err != nil {
    62  		log.Fatalf("Error opening db at %q: %v", *servingTable, err)
    63  	}
    64  	defer db.Close(ctx)
    65  	xs := xsrv.NewService(ctx, db)
    66  	tbl := &table.KVProto{db}
    67  	gs := gsrv.NewCombinedTable(tbl)
    68  	ft := &ftsrv.Table{Proto: tbl, PrefixedKeys: true}
    69  
    70  	xrefs.RegisterHTTPHandlers(ctx, xs, http.DefaultServeMux)
    71  	graph.RegisterHTTPHandlers(ctx, gs, http.DefaultServeMux)
    72  	filetree.RegisterHTTPHandlers(ctx, ft, http.DefaultServeMux)
    73  	web.RegisterQuitHandler(http.DefaultServeMux)
    74  	http.HandleFunc("/alive", func(w http.ResponseWriter, r *http.Request) {
    75  		fmt.Fprintln(w, "ok")
    76  	})
    77  
    78  	l, err := net.Listen("tcp", "localhost:0")
    79  	if err != nil {
    80  		log.Fatal(err)
    81  	}
    82  
    83  	_, port, err := net.SplitHostPort(l.Addr().String())
    84  	if err != nil {
    85  		log.Fatal(err)
    86  	}
    87  
    88  	if err := ioutil.WriteFile(*portFile, []byte(port+"\n"), 0777); err != nil {
    89  		log.Fatal(err)
    90  	}
    91  	defer os.Remove(*portFile) // ignore errors
    92  	log.Fatal(http.Serve(l, nil))
    93  }