diff --git a/API.go b/API.go index a4d0f60..4f6d303 100644 --- a/API.go +++ b/API.go @@ -24,21 +24,23 @@ import ( // startAPI starts the API if enabled via command line parameter or if the settings are set in the config file. // Using the command line option always ignores any API settings from the config (including timeout settings). -func startAPI(apiListen []string, apiKey uuid.UUID) { +func startAPI(backend *core.Backend, apiListen []string, apiKey uuid.UUID) { + var api *webapi.WebapiInstance + if len(apiListen) > 0 { // API listen parameter via command line argument. // Note that read and write timeouts are set to 0 which means they are not used. SSL is not enabled. - webapi.Start(apiListen, false, "", "", 0, 0, apiKey) + api = webapi.Start(backend, apiListen, false, "", "", 0, 0, apiKey) } else if len(config.APIListen) != 0 { // API settings via config file. - webapi.Start(config.APIListen, config.APIUseSSL, config.APICertificateFile, config.APICertificateKey, parseDuration(config.APITimeoutRead), parseDuration(config.APITimeoutWrite), config.APIKey) + api = webapi.Start(backend, config.APIListen, config.APIUseSSL, config.APICertificateFile, config.APICertificateKey, parseDuration(config.APITimeoutRead), parseDuration(config.APITimeoutWrite), config.APIKey) } else { return } - webapi.Router.HandleFunc("/console", apiConsole).Methods("GET") - webapi.Router.HandleFunc("/shutdown", apiShutdown).Methods("GET") + api.Router.HandleFunc("/console", apiConsole).Methods("GET") + api.Router.HandleFunc("/shutdown", apiShutdown).Methods("GET") } // parseCmdParams parses the "-webapi", "-apikey", and "-watchpid" command line parameters. diff --git a/Command Line.go b/Command Line.go index 08e5e56..70a2787 100644 --- a/Command Line.go +++ b/Command Line.go @@ -48,6 +48,7 @@ func showHelp(output io.Writer) { "get block Get block from remote peer\n"+ "log error Set error log output\n"+ "exit Exit\n"+ + "search file Search globally for files using the local search index\n"+ "\n") } @@ -445,6 +446,32 @@ func userCommands(input io.Reader, output io.Writer, terminateSignal chan struct case "exit": core.Filters.LogError("userCommands", "graceful exit via user terminal command\n") os.Exit(core.ExitGraceful) + + case "search file": + text, _, terminate := getUserOptionString(reader, terminateSignal) + if terminate { + return + } + + results := backend.SearchIndex.Search(text) + if len(results) == 0 { + fmt.Printf("No results found.\n") + break + } + + for _, result := range results { + fmt.Printf("- File ID %s\n", result.FileID.String()) + fmt.Printf(" Public Key %s\n", hex.EncodeToString(result.PublicKey.SerializeCompressed())) + fmt.Printf(" Block Number %d\n", result.BlockNumber) + keywords := "" + for n, selector := range result.Selectors { + if n > 0 { + keywords += ", " + } + keywords += selector.Word + } + fmt.Printf(" Found via keywords %s\n", keywords) + } } } } diff --git a/Main.go b/Main.go index 5731298..ba22ec0 100644 --- a/Main.go +++ b/Main.go @@ -70,12 +70,14 @@ func init() { userAgent := appName + "/" + core.Version - core.Init(userAgent) + backend = core.Init(userAgent) } +var backend *core.Backend + func main() { apiListen, apiKey, watchPID := parseCmdParams() - startAPI(apiListen, apiKey) + startAPI(backend, apiListen, apiKey) go processExitMonitor(watchPID) diff --git a/go.mod b/go.mod index 584d014..4c672be 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/PeernetOfficial/Cmd go 1.16 require ( - github.com/PeernetOfficial/core v0.0.0-20211211194338-c12b68c35cb8 + github.com/PeernetOfficial/core v0.0.0-20211213031602-388343dc3eb3 github.com/google/uuid v1.3.0 github.com/gorilla/websocket v1.4.3-0.20210424162022-e8629af678b7 ) diff --git a/go.sum b/go.sum index 635b5fd..ae00917 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/PeernetOfficial/core v0.0.0-20211211194338-c12b68c35cb8 h1:pzgjxUhS8+FlsOlETO57N9oUb4/+pPOWpxVj+NAAONo= -github.com/PeernetOfficial/core v0.0.0-20211211194338-c12b68c35cb8/go.mod h1:tRL13e2o4/h55LDd+aM9QoGukc+3L3Zy0yK8jAh+PsE= +github.com/PeernetOfficial/core v0.0.0-20211213031602-388343dc3eb3 h1:DH1Q3pNE0cODEptmQRAUErTsrTdIK2Mifa/3b6bxWtk= +github.com/PeernetOfficial/core v0.0.0-20211213031602-388343dc3eb3/go.mod h1:tRL13e2o4/h55LDd+aM9QoGukc+3L3Zy0yK8jAh+PsE= github.com/akrylysov/pogreb v0.10.1 h1:FqlR8VR7uCbJdfUob916tPM+idpKgeESDXOA1K0DK4w= github.com/akrylysov/pogreb v0.10.1/go.mod h1:pNs6QmpQ1UlTJKDezuRWmaqkgUE2TuU0YTWyqJZ7+lI= github.com/enfipy/locker v1.1.0 h1:2zVJ0ky7cS1Vjs0x6OQWFiT2dSEiHrI5/O2KCz1fgGc=