github.com/alexdevranger/node-1.8.27@v0.0.0-20221128213301-aa5841e41d2d/cmd/swarm/global-store/global_store.go (about) 1 // Copyright 2019 The go-ethereum Authors 2 // This file is part of go-dubxcoin. 3 // 4 // go-dubxcoin 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 "net" 21 "net/http" 22 "os" 23 24 "github.com/alexdevranger/node-1.8.27/log" 25 "github.com/alexdevranger/node-1.8.27/rpc" 26 "github.com/alexdevranger/node-1.8.27/swarm/storage/mock" 27 "github.com/alexdevranger/node-1.8.27/swarm/storage/mock/db" 28 "github.com/alexdevranger/node-1.8.27/swarm/storage/mock/mem" 29 cli "gopkg.in/urfave/cli.v1" 30 ) 31 32 // startHTTP starts a global store with HTTP RPC server. 33 // It is used for "http" cli command. 34 func startHTTP(ctx *cli.Context) (err error) { 35 server, cleanup, err := newServer(ctx) 36 if err != nil { 37 return err 38 } 39 defer cleanup() 40 41 listener, err := net.Listen("tcp", ctx.String("addr")) 42 if err != nil { 43 return err 44 } 45 log.Info("http", "address", listener.Addr().String()) 46 47 return http.Serve(listener, server) 48 } 49 50 // startWS starts a global store with WebSocket RPC server. 51 // It is used for "websocket" cli command. 52 func startWS(ctx *cli.Context) (err error) { 53 server, cleanup, err := newServer(ctx) 54 if err != nil { 55 return err 56 } 57 defer cleanup() 58 59 listener, err := net.Listen("tcp", ctx.String("addr")) 60 if err != nil { 61 return err 62 } 63 origins := ctx.StringSlice("origins") 64 log.Info("websocket", "address", listener.Addr().String(), "origins", origins) 65 66 return http.Serve(listener, server.WebsocketHandler(origins)) 67 } 68 69 // newServer creates a global store and returns its RPC server. 70 // Returned cleanup function should be called only if err is nil. 71 func newServer(ctx *cli.Context) (server *rpc.Server, cleanup func(), err error) { 72 log.PrintOrigins(true) 73 log.Root().SetHandler(log.LvlFilterHandler(log.Lvl(ctx.Int("verbosity")), log.StreamHandler(os.Stdout, log.TerminalFormat(false)))) 74 75 cleanup = func() {} 76 var globalStore mock.GlobalStorer 77 dir := ctx.String("dir") 78 if dir != "" { 79 dbStore, err := db.NewGlobalStore(dir) 80 if err != nil { 81 return nil, nil, err 82 } 83 cleanup = func() { 84 dbStore.Close() 85 } 86 globalStore = dbStore 87 log.Info("database global store", "dir", dir) 88 } else { 89 globalStore = mem.NewGlobalStore() 90 log.Info("in-memory global store") 91 } 92 93 server = rpc.NewServer() 94 if err := server.RegisterName("mockStore", globalStore); err != nil { 95 cleanup() 96 return nil, nil, err 97 } 98 99 return server, cleanup, nil 100 }