From 42fc02f2aaa8e3e358373181bc34e31d51300526 Mon Sep 17 00:00:00 2001 From: TerranStakers Date: Thu, 18 Nov 2021 08:54:57 +0100 Subject: [PATCH 1/3] - Small refactoring - Better webserver implementation --- go.mod | 4 ++ go.sum | 15 ++++ main.go | 148 +++++++++++++++++++++++---------------- web/assets/css/main.css | 3 + web/index.html | 8 --- web/templates/index.html | 12 ++++ 6 files changed, 123 insertions(+), 67 deletions(-) create mode 100644 web/assets/css/main.css delete mode 100644 web/index.html create mode 100644 web/templates/index.html diff --git a/go.mod b/go.mod index 394ee5d..813943a 100644 --- a/go.mod +++ b/go.mod @@ -26,6 +26,7 @@ require ( github.com/go-logfmt/logfmt v0.5.1 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.2 // indirect + github.com/google/uuid v1.1.2 // indirect github.com/gtank/merlin v0.1.1 // indirect github.com/jessevdk/go-flags v1.4.0 // indirect github.com/jrick/logrotate v1.0.0 // indirect @@ -41,8 +42,11 @@ require ( github.com/prometheus/common v0.32.1 // indirect github.com/prometheus/procfs v0.7.3 // indirect github.com/sasha-s/go-deadlock v0.3.1 // indirect + go.opencensus.io v0.23.0 // indirect golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect golang.org/x/net v0.0.0-20211020060615-d418f374d309 // indirect golang.org/x/sys v0.0.0-20211023085530-d6a326fbbf70 // indirect + golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect google.golang.org/protobuf v1.27.1 // indirect + googlemaps.github.io/maps v1.3.2 // indirect ) diff --git a/go.sum b/go.sum index fe1bb62..ca4747c 100644 --- a/go.sum +++ b/go.sum @@ -241,6 +241,7 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= @@ -257,6 +258,8 @@ github.com/google/pprof v0.0.0-20200430221834-fc25d7d30c6d/go.mod h1:ZgVRPoUq/hf github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= @@ -333,6 +336,7 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= @@ -478,6 +482,7 @@ github.com/sasha-s/go-deadlock v0.3.1 h1:sqv7fDNShgjcaxkO0JNcOAlr8B9+cV5Ey/OB71e github.com/sasha-s/go-deadlock v0.3.1/go.mod h1:F73l+cr82YSh10GxyRI6qZiCgK64VaZjwesgfQ1/iLM= github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/seccomp/libseccomp-golang v0.9.1/go.mod h1:GbW5+tmTXfcxTToHLXlScSlAvWlF4P2Ca7zGrPiEpWo= +github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= @@ -513,6 +518,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= @@ -546,6 +552,8 @@ go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= +go.opencensus.io v0.23.0 h1:gqCw0LfLxScz8irSi8exQc7fyQ0fKQU/qnC/X8+V/1M= +go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E= go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= @@ -641,6 +649,7 @@ golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81R golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= +golang.org/x/net v0.0.0-20201110031124-69a78807bb2b/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20210903162142-ad29c8ab022f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= @@ -738,6 +747,9 @@ golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxb golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac h1:7zkz7BUtwNFFqcowJ+RIgu2MaV/MapERkDIy+mwPyjs= +golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= @@ -863,6 +875,7 @@ google.golang.org/grpc v1.28.0/go.mod h1:rpkK4SK4GF4Ach/+MFLZUBavHOvF2JJB5uozKKa google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk= google.golang.org/grpc v1.30.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= google.golang.org/grpc v1.31.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= +google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc= google.golang.org/grpc v1.35.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU= google.golang.org/grpc v1.37.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= @@ -879,6 +892,8 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0 google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +googlemaps.github.io/maps v1.3.2 h1:3YfYdVWFTFi7lVdCdrDYW3dqHvfCSUdC7/x8pbMOuKQ= +googlemaps.github.io/maps v1.3.2/go.mod h1:cCq0JKYAnnCRSdiaBi7Ex9CW15uxIAk7oPi8V/xEh6s= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/main.go b/main.go index 5bbd2ae..aa57454 100644 --- a/main.go +++ b/main.go @@ -1,8 +1,8 @@ package main import ( - "embed" "fmt" + "github.com/mitchellh/go-homedir" "github.com/tendermint/tendermint/config" "github.com/tendermint/tendermint/libs/log" tmos "github.com/tendermint/tendermint/libs/os" @@ -10,26 +10,21 @@ import ( "github.com/tendermint/tendermint/p2p" "github.com/tendermint/tendermint/p2p/pex" "github.com/tendermint/tendermint/version" + "html/template" "net/http" "os" "path/filepath" - "time" - - "github.com/mitchellh/go-homedir" ) var ( -// https://blog.jetbrains.com/go/2021/06/09/how-to-use-go-embed-in-go-1-16/ - //go:embed resources - res embed.FS - pages = map[string]string{ - "/": "web/index.html", - } + configDir = ".tinyseed" + logger = log.NewTMLogger(log.NewSyncWriter(os.Stdout)) ) // Config defines the configuration format type Config struct { ListenAddress string `toml:"laddr" comment:"Address to listen for incoming connections"` + HttpPort string `toml:"http_port" comment:"Port for the http server"` ChainID string `toml:"chain_id" comment:"network identifier (todo move to cli flag argument? keeps the config network agnostic)"` NodeKeyFile string `toml:"node_key_file" comment:"path to node_key (relative to tendermint-seed home directory or an absolute path)"` AddrBookFile string `toml:"addr_book_file" comment:"path to address book (relative to tendermint-seed home directory or an absolute path)"` @@ -43,48 +38,95 @@ type Config struct { func DefaultConfig() *Config { return &Config{ ListenAddress: "tcp://0.0.0.0:6969", + HttpPort: "3000", ChainID: "osmosis-1", - NodeKeyFile: "config/node_key.json", - AddrBookFile: "data/addrbook.json", + NodeKeyFile: "node_key.json", + AddrBookFile: "addrbook.json", AddrBookStrict: true, - MaxNumInboundPeers: 1000, + MaxNumInboundPeers: 3000, MaxNumOutboundPeers: 1000, Seeds: "1b077d96ceeba7ef503fb048f343a538b2dcdf1b@136.243.218.244:26656,2308bed9e096a8b96d2aa343acc1147813c59ed2@3.225.38.25:26656,085f62d67bbf9c501e8ac84d4533440a1eef6c45@95.217.196.54:26656,f515a8599b40f0e84dfad935ba414674ab11a668@osmosis.blockpane.com:26656", } } -func startWebServer() { - http.Handle("/", http.FileServer(http.Dir("/tmp"))) - - - s := &http.Server{ - Addr: ":8080", - Handler: myHandler, - ReadTimeout: 10 * time.Second, - WriteTimeout: 10 * time.Second, - MaxHeaderBytes: 1 << 20, - } -} - func main() { idOverride := os.Getenv("ID") seedOverride := os.Getenv("SEEDS") userHomeDir, err := homedir.Dir() + seedConfig := DefaultConfig() + if err != nil { panic(err) } - homeDir := filepath.Join(userHomeDir, ".terranseed") - configFile := "config/config.toml" - configFilePath := filepath.Join(homeDir, configFile) + + // init config directory & files + homeDir := filepath.Join(userHomeDir, configDir, "config") + configFilePath := filepath.Join(homeDir, "config.toml") + nodeKeyFilePath := filepath.Join(homeDir, seedConfig.NodeKeyFile) + addrBookFilePath := filepath.Join(homeDir, seedConfig.AddrBookFile) + + MkdirAllPanic(filepath.Dir(nodeKeyFilePath), os.ModePerm) + MkdirAllPanic(filepath.Dir(addrBookFilePath), os.ModePerm) MkdirAllPanic(filepath.Dir(configFilePath), os.ModePerm) - SeedConfig := DefaultConfig() + if idOverride != "" { - SeedConfig.ChainID = idOverride + seedConfig.ChainID = idOverride } if seedOverride != "" { - SeedConfig.Seeds = seedOverride + seedConfig.Seeds = seedOverride + } + logger.Info("Starting Web Server...") + StartWebServer(*seedConfig) + logger.Info("Starting Seed Node...") + Start(*seedConfig) +} + +func StartWebServer(seedConfig Config) { + + // serve static assets + fs := http.FileServer(http.Dir("./web/assets")) + http.Handle("/assets/", http.StripPrefix("/assets/", fs)) + + // serve html files + http.HandleFunc("/", serveTemplate) + + // start web server in non-blocking + go func() { + err := http.ListenAndServe(":"+seedConfig.HttpPort, nil) + logger.Info("HTTP Server started", "port", seedConfig.HttpPort) + if err != nil { + panic(err) + } + }() +} + +func serveTemplate(w http.ResponseWriter, r *http.Request) { + index := filepath.Join("./web/templates", "index.html") + templates := filepath.Join("./web/templates", filepath.Clean(r.URL.Path)) + logger.Info("index", "i", index, "t", templates) + + // Return a 404 if the template doesn't exist + fileInfo, err := os.Stat(templates) + + if err != nil || fileInfo.IsDir() { + http.Redirect(w, r,"/index.html", 302) + return + } + + tmpl, err := template.ParseFiles(index, templates) + if err != nil { + // Log the detailed error + logger.Error(err.Error()) + // Return a generic "Internal Server Error" message + http.Error(w, http.StatusText(500), 500) + return + } + + err = tmpl.ExecuteTemplate(w, "index", nil) + if err != nil { + logger.Error(err.Error()) + http.Error(w, http.StatusText(500), 500) } - Start(*SeedConfig) } // MkdirAllPanic invokes os.MkdirAll but panics if there is an error @@ -96,42 +138,29 @@ func MkdirAllPanic(path string, perm os.FileMode) { } // Start starts a Tenderseed -func Start(SeedConfig Config) { - logger := log.NewTMLogger( - log.NewSyncWriter(os.Stdout), - ) - - chainID := SeedConfig.ChainID - nodeKeyFilePath := SeedConfig.NodeKeyFile - addrBookFilePath := SeedConfig.AddrBookFile +func Start(seedConfig Config) { - MkdirAllPanic(filepath.Dir(nodeKeyFilePath), os.ModePerm) - MkdirAllPanic(filepath.Dir(addrBookFilePath), os.ModePerm) + chainID := seedConfig.ChainID cfg := config.DefaultP2PConfig() cfg.AllowDuplicateIP = true - // allow a lot of inbound peers since we disconnect from them quickly in seed mode - cfg.MaxNumInboundPeers = 3000 - - // keep trying to make outbound connections to exchange peering info - cfg.MaxNumOutboundPeers = 400 - + userHomeDir, err := homedir.Dir() + nodeKeyFilePath := filepath.Join(userHomeDir, configDir, "config", seedConfig.NodeKeyFile) nodeKey, err := p2p.LoadOrGenNodeKey(nodeKeyFilePath) if err != nil { panic(err) } - logger.Info("terranseed", + logger.Info("Configuration", "key", nodeKey.ID(), - "listen", SeedConfig.ListenAddress, + "listen", seedConfig.ListenAddress, "chain", chainID, - "strict-routing", SeedConfig.AddrBookStrict, - "max-inbound", SeedConfig.MaxNumInboundPeers, - "max-outbound", SeedConfig.MaxNumOutboundPeers, + "strict-routing", seedConfig.AddrBookStrict, + "max-inbound", seedConfig.MaxNumInboundPeers, + "max-outbound", seedConfig.MaxNumOutboundPeers, ) - // TODO(roman) expose per-module log levels in the config filteredLogger := log.NewFilter(logger, log.AllowInfo()) protocolVersion := @@ -141,11 +170,11 @@ func Start(SeedConfig Config) { 0, ) - // NodeInfo gets info on yhour node + // NodeInfo gets info on your node nodeInfo := p2p.DefaultNodeInfo{ ProtocolVersion: protocolVersion, DefaultNodeID: nodeKey.ID(), - ListenAddr: SeedConfig.ListenAddress, + ListenAddr: seedConfig.ListenAddress, Network: chainID, Version: "0.6.9", Channels: []byte{pex.PexChannel}, @@ -162,12 +191,13 @@ func Start(SeedConfig Config) { panic(err) } - book := pex.NewAddrBook(addrBookFilePath, SeedConfig.AddrBookStrict) + addrBookFilePath := filepath.Join(userHomeDir, configDir, "config", seedConfig.AddrBookFile) + book := pex.NewAddrBook(addrBookFilePath, seedConfig.AddrBookStrict) book.SetLogger(filteredLogger.With("module", "book")) pexReactor := pex.NewReactor(book, &pex.ReactorConfig{ SeedMode: true, - Seeds: tmstrings.SplitAndTrim(SeedConfig.Seeds, ",", " "), + Seeds: tmstrings.SplitAndTrim(seedConfig.Seeds, ",", " "), }) pexReactor.SetLogger(filteredLogger.With("module", "pex")) diff --git a/web/assets/css/main.css b/web/assets/css/main.css new file mode 100644 index 0000000..b7a6468 --- /dev/null +++ b/web/assets/css/main.css @@ -0,0 +1,3 @@ +h2 { + color: grey; +} diff --git a/web/index.html b/web/index.html deleted file mode 100644 index 6a3f70c..0000000 --- a/web/index.html +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/web/templates/index.html b/web/templates/index.html new file mode 100644 index 0000000..0f697d6 --- /dev/null +++ b/web/templates/index.html @@ -0,0 +1,12 @@ +{{define "index"}} + + + + + + + +

It works!

+ + +{{end}} From f9b07510b96cffc6284566afd78c0874562e32c1 Mon Sep 17 00:00:00 2001 From: TerranStakers Date: Mon, 22 Nov 2021 12:25:30 +0100 Subject: [PATCH 2/3] Minor --- main.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index aa57454..fbb6669 100644 --- a/main.go +++ b/main.go @@ -154,7 +154,8 @@ func Start(seedConfig Config) { logger.Info("Configuration", "key", nodeKey.ID(), - "listen", seedConfig.ListenAddress, + "node listen", seedConfig.ListenAddress, + "http server port", seedConfig.HttpPort, "chain", chainID, "strict-routing", seedConfig.AddrBookStrict, "max-inbound", seedConfig.MaxNumInboundPeers, From 94bc29400397bab880c25e139fbc68070d50fa16 Mon Sep 17 00:00:00 2001 From: TerranStakers Date: Mon, 22 Nov 2021 18:16:29 +0100 Subject: [PATCH 3/3] Log peers --- main.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index fbb6669..0a3d8b0 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( "net/http" "os" "path/filepath" + "time" ) var ( @@ -109,7 +110,7 @@ func serveTemplate(w http.ResponseWriter, r *http.Request) { fileInfo, err := os.Stat(templates) if err != nil || fileInfo.IsDir() { - http.Redirect(w, r,"/index.html", 302) + http.Redirect(w, r, "/index.html", 302) return } @@ -197,8 +198,10 @@ func Start(seedConfig Config) { book.SetLogger(filteredLogger.With("module", "book")) pexReactor := pex.NewReactor(book, &pex.ReactorConfig{ - SeedMode: true, - Seeds: tmstrings.SplitAndTrim(seedConfig.Seeds, ",", " "), + SeedMode: true, + Seeds: tmstrings.SplitAndTrim(seedConfig.Seeds, ",", " "), + SeedDisconnectWaitPeriod: 1 * time.Second, // default is 28 hours, we just want to harvest as many addresses as possible + PersistentPeersMaxDialPeriod: 0, // use exponential back-off }) pexReactor.SetLogger(filteredLogger.With("module", "pex")) @@ -225,5 +228,17 @@ func Start(seedConfig Config) { panic(err) } + go func() { + // Fire periodically + ticker := time.NewTicker(5 * time.Second) + + for { + select { + case <-ticker.C: + logger.Info("Peers list", "peers", sw.Peers().List()) + } + } + }() + sw.Wait() }