github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/utils/remotesrv/main.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"context"
    19  	"flag"
    20  	"fmt"
    21  	"log"
    22  	"os"
    23  	"os/signal"
    24  
    25  	remotesapi "github.com/dolthub/dolt/go/gen/proto/dolt/services/remotesapi/v1alpha1"
    26  
    27  	"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
    28  	"github.com/dolthub/dolt/go/libraries/doltcore/env"
    29  	"github.com/dolthub/dolt/go/libraries/doltcore/remotesrv"
    30  	"github.com/dolthub/dolt/go/libraries/utils/filesys"
    31  	"github.com/dolthub/dolt/go/store/datas"
    32  )
    33  
    34  var result []byte
    35  
    36  func main() {
    37  	readOnlyParam := flag.Bool("read-only", false, "run a read-only server which does not allow writes")
    38  	repoModeParam := flag.Bool("repo-mode", false, "act as a remote for an existing dolt directory, instead of stand alone")
    39  	dirParam := flag.String("dir", "", "root directory that this command will run in; default cwd")
    40  	grpcPortParam := flag.Int("grpc-port", -1, "the port the grpc server will listen on; default 50051")
    41  	httpPortParam := flag.Int("http-port", -1, "the port the http server will listen on; default 80; if http-port is equal to grpc-port, both services will serve over the same port")
    42  	httpHostParam := flag.String("http-host", "", "hostname to use in the host component of the URLs that the server generates; default ''; if '', server will echo the :authority header")
    43  	flag.Parse()
    44  
    45  	if dirParam != nil && len(*dirParam) > 0 {
    46  		err := os.Chdir(*dirParam)
    47  
    48  		if err != nil {
    49  			log.Fatalln("failed to chdir to:", *dirParam, "error:", err.Error())
    50  		} else {
    51  			log.Println("cwd set to " + *dirParam)
    52  		}
    53  	} else {
    54  		log.Println("'dir' parameter not provided. Using the current working dir.")
    55  	}
    56  
    57  	if *httpPortParam != -1 {
    58  		*httpHostParam = fmt.Sprintf("%s:%d", *httpHostParam, *httpPortParam)
    59  	} else {
    60  		*httpPortParam = 80
    61  		*httpHostParam = ":80"
    62  		log.Println("'http-port' parameter not provided. Using default port 80")
    63  	}
    64  
    65  	if *grpcPortParam == -1 {
    66  		*grpcPortParam = 50051
    67  		log.Println("'grpc-port' parameter not provided. Using default port 50051")
    68  	}
    69  
    70  	fs, err := filesys.LocalFilesysWithWorkingDir(".")
    71  	if err != nil {
    72  		log.Fatalln("could not get cwd path:", err.Error())
    73  	}
    74  
    75  	var dbCache remotesrv.DBCache
    76  	if *repoModeParam {
    77  		dEnv := env.Load(context.Background(), env.GetCurrentUserHomeDir, fs, doltdb.LocalDirDoltDB, "remotesrv")
    78  		if !dEnv.Valid() {
    79  			log.Fatalln("repo-mode failed to load repository")
    80  		}
    81  		db := doltdb.HackDatasDatabaseFromDoltDB(dEnv.DoltDB)
    82  		cs := datas.ChunkStoreFromDatabase(db)
    83  		dbCache = SingletonCSCache{cs.(remotesrv.RemoteSrvStore)}
    84  	} else {
    85  		dbCache = NewLocalCSCache(fs)
    86  	}
    87  
    88  	server, err := remotesrv.NewServer(remotesrv.ServerArgs{
    89  		HttpHost:           *httpHostParam,
    90  		HttpListenAddr:     fmt.Sprintf(":%d", *httpPortParam),
    91  		GrpcListenAddr:     fmt.Sprintf(":%d", *grpcPortParam),
    92  		FS:                 fs,
    93  		DBCache:            dbCache,
    94  		ReadOnly:           *readOnlyParam,
    95  		ConcurrencyControl: remotesapi.PushConcurrencyControl_PUSH_CONCURRENCY_CONTROL_IGNORE_WORKING_SET,
    96  	})
    97  	if err != nil {
    98  		log.Fatalf("error creating remotesrv Server: %v\n", err)
    99  	}
   100  	listeners, err := server.Listeners()
   101  	if err != nil {
   102  		log.Fatalf("error starting remotesrv Server listeners: %v\n", err)
   103  	}
   104  	go func() {
   105  		server.Serve(listeners)
   106  	}()
   107  	waitForSignal()
   108  	server.GracefulStop()
   109  }
   110  
   111  func waitForSignal() {
   112  	c := make(chan os.Signal, 1)
   113  	signal.Notify(c, os.Interrupt, os.Kill)
   114  	<-c
   115  }