github.com/jbendotnet/noms@v0.0.0-20190904222105-c43e4293ea92/cmd/noms/noms_serve.go (about)

     1  // Copyright 2016 Attic Labs, Inc. All rights reserved.
     2  // Licensed under the Apache License, version 2.0:
     3  // http://www.apache.org/licenses/LICENSE-2.0
     4  
     5  package main
     6  
     7  import (
     8  	"os"
     9  	"os/signal"
    10  	"syscall"
    11  
    12  	"github.com/attic-labs/kingpin"
    13  	"github.com/attic-labs/noms/cmd/util"
    14  	"github.com/attic-labs/noms/go/config"
    15  	"github.com/attic-labs/noms/go/d"
    16  	"github.com/attic-labs/noms/go/datas"
    17  	"github.com/attic-labs/noms/go/util/profile"
    18  )
    19  
    20  func nomsServe(noms *kingpin.Application) (*kingpin.CmdClause, util.KingpinHandler) {
    21  	cmd := noms.Command("serve", "Serves a Noms database over HTTP.")
    22  	port := cmd.Flag("port", "port to listen on").Default("8080").Int()
    23  	db := cmd.Arg("db", "database to work with - see Spelling Databases at https://github.com/attic-labs/noms/blob/master/doc/spelling.md").Required().String()
    24  
    25  	return cmd, func(_ string) int {
    26  		cfg := config.NewResolver()
    27  		cs, err := cfg.GetChunkStore(*db)
    28  		d.CheckError(err)
    29  		server := datas.NewRemoteDatabaseServer(cs, *port)
    30  
    31  		// Shutdown server gracefully so that profile may be written
    32  		c := make(chan os.Signal, 1)
    33  		signal.Notify(c, os.Interrupt)
    34  		signal.Notify(c, syscall.SIGTERM)
    35  		go func() {
    36  			<-c
    37  			server.Stop()
    38  		}()
    39  
    40  		d.Try(func() {
    41  			defer profile.MaybeStartProfile().Stop()
    42  			server.Run()
    43  		})
    44  		return 0
    45  	}
    46  }