github.com/nitinawathare/ethereumassignment3@v0.0.0-20211021213010-f07344c2b868/go-ethereum/cmd/swarm/global-store/explorer.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 "context" 21 "fmt" 22 "net" 23 "net/http" 24 "time" 25 26 "github.com/ethereum/go-ethereum/log" 27 "github.com/ethereum/go-ethereum/swarm/storage/mock" 28 "github.com/ethereum/go-ethereum/swarm/storage/mock/explorer" 29 cli "gopkg.in/urfave/cli.v1" 30 ) 31 32 // serveChunkExplorer starts an http server in background with chunk explorer handler 33 // using the provided global store. Server is started if the returned shutdown function 34 // is not nil. 35 func serveChunkExplorer(ctx *cli.Context, globalStore mock.GlobalStorer) (shutdown func(), err error) { 36 if !ctx.IsSet("explorer-address") { 37 return nil, nil 38 } 39 40 corsOrigins := ctx.StringSlice("explorer-cors-origin") 41 server := &http.Server{ 42 Handler: explorer.NewHandler(globalStore, corsOrigins), 43 IdleTimeout: 30 * time.Minute, 44 ReadTimeout: 2 * time.Minute, 45 WriteTimeout: 2 * time.Minute, 46 } 47 listener, err := net.Listen("tcp", ctx.String("explorer-address")) 48 if err != nil { 49 return nil, fmt.Errorf("explorer: %v", err) 50 } 51 log.Info("chunk explorer http", "address", listener.Addr().String(), "origins", corsOrigins) 52 53 go func() { 54 if err := server.Serve(listener); err != nil { 55 log.Error("chunk explorer", "err", err) 56 } 57 }() 58 59 return func() { 60 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 61 defer cancel() 62 if err := server.Shutdown(ctx); err != nil { 63 log.Error("chunk explorer: shutdown", "err", err) 64 } 65 }, nil 66 }