github.com/nitinawathare/ethereumassignment3@v0.0.0-20211021213010-f07344c2b868/go-ethereum/cmd/swarm/global-store/global_store.go (about) 1 // Copyright 2019 The go-ethereum Authors 2 // This file is part of go-ethereum. 3 // 4 // go-ethereum is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // go-ethereum is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. 16 17 package main 18 19 import ( 20 "io" 21 "net" 22 "net/http" 23 "os" 24 25 "github.com/ethereum/go-ethereum/log" 26 "github.com/ethereum/go-ethereum/rpc" 27 "github.com/ethereum/go-ethereum/swarm/storage/mock" 28 "github.com/ethereum/go-ethereum/swarm/storage/mock/db" 29 "github.com/ethereum/go-ethereum/swarm/storage/mock/mem" 30 cli "gopkg.in/urfave/cli.v1" 31 ) 32 33 // startHTTP starts a global store with HTTP RPC server. 34 // It is used for "http" cli command. 35 func startHTTP(ctx *cli.Context) (err error) { 36 server, cleanup, err := newServer(ctx) 37 if err != nil { 38 return err 39 } 40 defer cleanup() 41 42 listener, err := net.Listen("tcp", ctx.String("addr")) 43 if err != nil { 44 return err 45 } 46 log.Info("http", "address", listener.Addr().String()) 47 48 return http.Serve(listener, server) 49 } 50 51 // startWS starts a global store with WebSocket RPC server. 52 // It is used for "websocket" cli command. 53 func startWS(ctx *cli.Context) (err error) { 54 server, cleanup, err := newServer(ctx) 55 if err != nil { 56 return err 57 } 58 defer cleanup() 59 60 listener, err := net.Listen("tcp", ctx.String("addr")) 61 if err != nil { 62 return err 63 } 64 origins := ctx.StringSlice("origins") 65 log.Info("websocket", "address", listener.Addr().String(), "origins", origins) 66 67 return http.Serve(listener, server.WebsocketHandler(origins)) 68 } 69 70 // newServer creates a global store and starts a chunk explorer server if configured. 71 // Returned cleanup function should be called only if err is nil. 72 func newServer(ctx *cli.Context) (server *rpc.Server, cleanup func(), err error) { 73 log.PrintOrigins(true) 74 log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(ctx.Int("verbosity")), log.StreamHandler(os.Stdout, log.TerminalFormat(false)))) 75 76 cleanup = func() {} 77 var globalStore mock.GlobalStorer 78 dir := ctx.String("dir") 79 if dir != "" { 80 dbStore, err := db.NewGlobalStore(dir) 81 if err != nil { 82 return nil, nil, err 83 } 84 cleanup = func() { 85 if err := dbStore.Close(); err != nil { 86 log.Error("global store: close", "err", err) 87 } 88 } 89 globalStore = dbStore 90 log.Info("database global store", "dir", dir) 91 } else { 92 globalStore = mem.NewGlobalStore() 93 log.Info("in-memory global store") 94 } 95 96 server = rpc.NewServer() 97 if err := server.RegisterName("mockStore", globalStore); err != nil { 98 cleanup() 99 return nil, nil, err 100 } 101 102 shutdown, err := serveChunkExplorer(ctx, globalStore) 103 if err != nil { 104 cleanup() 105 return nil, nil, err 106 } 107 if shutdown != nil { 108 cleanup = func() { 109 shutdown() 110 111 if c, ok := globalStore.(io.Closer); ok { 112 if err := c.Close(); err != nil { 113 log.Error("global store: close", "err", err) 114 } 115 } 116 } 117 } 118 119 return server, cleanup, nil 120 }