github.com/ebakus/go-ebakus@v1.0.5-0.20200520105415-dbccef9ec421/node/defaults.go (about) 1 // Copyright 2019 The ebakus/go-ebakus Authors 2 // This file is part of the ebakus/go-ebakus library. 3 // 4 // The ebakus/go-ebakus library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser 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 // The ebakus/go-ebakus library 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 Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the ebakus/go-ebakus library. If not, see <http://www.gnu.org/licenses/>. 16 17 package node 18 19 import ( 20 "bytes" 21 "encoding/gob" 22 "os" 23 "os/user" 24 "path/filepath" 25 "runtime" 26 27 "github.com/ebakus/go-ebakus/p2p" 28 "github.com/ebakus/go-ebakus/p2p/nat" 29 "github.com/ebakus/go-ebakus/rpc" 30 ) 31 32 const ( 33 DefaultHTTPHost = "localhost" // Default host interface for the HTTP RPC server 34 DefaultHTTPPort = 8545 // Default TCP port for the HTTP RPC server 35 DefaultWSHost = "localhost" // Default host interface for the websocket RPC server 36 DefaultWSPort = 8546 // Default TCP port for the websocket RPC server 37 DefaultGraphQLHost = "localhost" // Default host interface for the GraphQL server 38 DefaultGraphQLPort = 8547 // Default TCP port for the GraphQL server 39 ) 40 41 // DefaultConfig contains reasonable default settings. 42 var DefaultConfig = Config{ 43 DataDir: DefaultDataDir(), 44 HTTPPort: DefaultHTTPPort, 45 HTTPModules: []string{"net", "web3"}, 46 HTTPVirtualHosts: []string{"localhost"}, 47 HTTPTimeouts: rpc.DefaultHTTPTimeouts, 48 WSPort: DefaultWSPort, 49 WSModules: []string{"net", "web3"}, 50 GraphQLPort: DefaultGraphQLPort, 51 GraphQLVirtualHosts: []string{"localhost"}, 52 P2P: p2p.Config{ 53 ListenAddr: ":30403", 54 MaxPeers: 25, 55 NAT: nat.Any(), 56 }, 57 } 58 59 // DefaultDataDir is the default data directory to use for the databases and other 60 // persistence requirements. 61 func DefaultDataDir() string { 62 // Try to place the data folder in the user's home dir 63 home := homeDir() 64 if home != "" { 65 switch runtime.GOOS { 66 case "darwin": 67 return filepath.Join(home, "Library", "Ebakus") 68 case "windows": 69 // We used to put everything in %HOME%\AppData\Roaming, but this caused 70 // problems with non-typical setups. If this fallback location exists and 71 // is non-empty, use it, otherwise DTRT and check %LOCALAPPDATA%. 72 fallback := filepath.Join(home, "AppData", "Roaming", "Ebakus") 73 appdata := windowsAppData() 74 if appdata == "" || isNonEmptyDir(fallback) { 75 return fallback 76 } 77 return filepath.Join(appdata, "Ebakus") 78 default: 79 return filepath.Join(home, ".ebakus") 80 } 81 } 82 // As we cannot guess a stable location, return empty and handle later 83 return "" 84 } 85 86 func windowsAppData() string { 87 v := os.Getenv("LOCALAPPDATA") 88 if v == "" { 89 // Windows XP and below don't have LocalAppData. Crash here because 90 // we don't support Windows XP and undefining the variable will cause 91 // other issues. 92 panic("environment variable LocalAppData is undefined") 93 } 94 return v 95 } 96 97 func isNonEmptyDir(dir string) bool { 98 f, err := os.Open(dir) 99 if err != nil { 100 return false 101 } 102 names, _ := f.Readdir(1) 103 f.Close() 104 return len(names) > 0 105 } 106 107 func homeDir() string { 108 if home := os.Getenv("HOME"); home != "" { 109 return home 110 } 111 if usr, err := user.Current(); err == nil { 112 return usr.HomeDir 113 } 114 return "" 115 } 116 117 func GobMarshal(v interface{}) ([]byte, error) { 118 b := new(bytes.Buffer) 119 err := gob.NewEncoder(b).Encode(v) 120 if err != nil { 121 return nil, err 122 } 123 return b.Bytes(), nil 124 } 125 126 func GobUnmarshal(data []byte, v interface{}) error { 127 b := bytes.NewBuffer(data) 128 return gob.NewDecoder(b).Decode(v) 129 }