github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/daemon/output.go (about) 1 // Copyright 2016, 2024 The TrueBlocks Authors. All rights reserved. 2 // Use of this source code is governed by a license that can 3 // be found in the LICENSE file. 4 /* 5 * Parts of this file were auto generated. Edit only those parts of 6 * the code inside of 'EXISTING_CODE' tags. 7 */ 8 9 package daemonPkg 10 11 // EXISTING_CODE 12 import ( 13 "fmt" 14 "net/http" 15 16 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/internal/globals" 17 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/colors" 18 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/config" 19 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger" 20 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output" 21 outputHelpers "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output/helpers" 22 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/tslib" 23 "github.com/spf13/cobra" 24 ) 25 26 // EXISTING_CODE 27 28 // RunDaemon handles the daemon command for the command line. Returns error only as per cobra. 29 func RunDaemon(cmd *cobra.Command, args []string) error { 30 opts := daemonFinishParse(args) 31 rCtx := output.NewRenderContext() 32 // EXISTING_CODE 33 // EXISTING_CODE 34 outputHelpers.SetWriterForCommand("daemon", &opts.Globals) 35 return opts.DaemonInternal(rCtx) 36 } 37 38 // ServeDaemon handles the daemon command for the API. Returns an error. 39 func ServeDaemon(w http.ResponseWriter, r *http.Request) error { 40 opts := daemonFinishParseApi(w, r) 41 rCtx := output.NewRenderContext() 42 // EXISTING_CODE 43 if true { // defeats linter 44 logger.Fatal("should not happen ==> Daemon is an invalid route for server") 45 } 46 // EXISTING_CODE 47 outputHelpers.InitJsonWriterApi("daemon", w, &opts.Globals) 48 err := opts.DaemonInternal(rCtx) 49 outputHelpers.CloseJsonWriterIfNeededApi("daemon", err, &opts.Globals) 50 return err 51 } 52 53 // DaemonInternal handles the internal workings of the daemon command. Returns an error. 54 func (opts *DaemonOptions) DaemonInternal(rCtx *output.RenderCtx) error { 55 var err error 56 if err = opts.validateDaemon(); err != nil { 57 return err 58 } 59 60 timer := logger.NewTimer() 61 msg := "chifra daemon" 62 // EXISTING_CODE 63 chain := opts.Globals.Chain 64 provider := config.GetChain(chain).RpcProvider 65 66 logger.InfoTable("Server URL: ", opts.Url) 67 logger.InfoTable("RPC Provider: ", provider) 68 logger.InfoTable("Root Config Path: ", config.PathToRootConfig()) 69 logger.InfoTable("Chain Config Path: ", config.MustGetPathToChainConfig(chain)) 70 logger.InfoTable("Cache Path: ", config.PathToCache(chain)) 71 logger.InfoTable("Index Path: ", config.PathToIndex(chain)) 72 73 meta, err := opts.Conn.GetMetaData(false) 74 if err != nil { 75 msg := fmt.Sprintf("%sCould not load RPC provider: %s%s", colors.Red, err, colors.Off) 76 logger.InfoTable("Progress:", msg) 77 logger.Fatal("") 78 } else { 79 nTs, _ := tslib.NTimestamps(chain) // when the file has one record, the block is zero, etc. 80 if nTs > 0 { 81 nTs-- 82 } 83 msg := fmt.Sprintf("%d, %d, %d, %d, ts: %d", meta.Latest, meta.Finalized, meta.Staging, meta.Unripe, nTs) 84 logger.InfoTable("Progress: ", msg) 85 } 86 87 go func() { 88 _ = opts.HandleScraper(rCtx) 89 }() 90 go func() { 91 _ = opts.HandleMonitor(rCtx) 92 }() 93 94 // do not remove, this fixes a lint warning that happens in the boilerplate because of the Fatal just below 95 timer.Report(msg) 96 97 // Start listening to the web sockets 98 RunWebsocketPool() 99 // Start listening for requests 100 logger.Fatal(http.ListenAndServe(opts.Url, NewRouter(opts.Silent))) 101 102 // EXISTING_CODE 103 104 timer.Report(msg) 105 106 return err 107 } 108 109 // GetDaemonOptions returns the options for this tool so other tools may use it. 110 func GetDaemonOptions(args []string, g *globals.GlobalOptions) *DaemonOptions { 111 ret := daemonFinishParse(args) 112 if g != nil { 113 ret.Globals = *g 114 } 115 return ret 116 }