-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add dmsghttp logserver route #1579
Changes from 34 commits
0219ce8
30d4055
2136e61
28cf999
7f5f6a7
13d67d6
cc0f8b0
598a398
f4a5b85
664b652
1c6cd55
68adf7b
22a5d5c
8eb076e
9728b5b
bb13cd4
26ee1f8
7662a3d
e19f5de
4361174
9a3e04e
289b167
93a7f7e
f9df75d
e94d07b
3287ca4
15fc1c6
7a7cdab
5174f48
49ce0d9
041a06a
a5e5dc4
9656094
0612c0d
8a18313
7322064
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,14 +8,17 @@ import ( | |
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
coincipher "github.com/skycoin/skycoin/src/cipher" | ||
|
||
"github.com/skycoin/skywire-utilities/pkg/buildinfo" | ||
"github.com/skycoin/skywire-utilities/pkg/cipher" | ||
"github.com/skycoin/skywire-utilities/pkg/httputil" | ||
"github.com/skycoin/skywire-utilities/pkg/logging" | ||
"github.com/skycoin/skywire/pkg/visor/visorconfig" | ||
) | ||
|
||
// API register all the API endpoints. | ||
|
@@ -28,7 +31,7 @@ type API struct { | |
} | ||
|
||
// New creates a new API. | ||
func New(log *logging.Logger, tpLogPath, localPath, customPath string, whitelistedPKs []cipher.PubKey, printLog bool) *API { | ||
func New(log *logging.Logger, tpLogPath, localPath, customPath string, whitelistedPKs []cipher.PubKey, visorPK cipher.PubKey, printLog bool) *API { | ||
api := &API{ | ||
logger: log, | ||
startedAt: time.Now(), | ||
|
@@ -50,16 +53,52 @@ func New(log *logging.Logger, tpLogPath, localPath, customPath string, whitelist | |
if len(whitelistedPKs) > 0 { | ||
authRoute.Use(whitelistAuth(whitelistedPKs)) | ||
} | ||
// note that the survey only exists / is generated if the reward address is set | ||
authRoute.StaticFile("/node-info.json", filepath.Join(localPath, "node-info.json")) | ||
// note that the survey FILE only exists / is generated if the reward address is set | ||
authRoute.StaticFile("/"+visorconfig.NodeInfo, filepath.Join(localPath, visorconfig.NodeInfo)) | ||
// This survey endpoint generates the survey as a response | ||
authRoute.GET("/"+visorconfig.SurveyName, func(c *gin.Context) { | ||
var rewardAddress string | ||
var cAddr coincipher.Address | ||
//check for reward address | ||
rewardAddressBytes, err := os.ReadFile(localPath + "/" + visorconfig.RewardFile) //nolint | ||
if err == nil { | ||
//remove any newline from rewardAddress string | ||
rewardAddress = strings.TrimSuffix(string(rewardAddressBytes), "\n") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changed on next commit |
||
//validate the skycoin address | ||
cAddr, err = coincipher.DecodeBase58Address(rewardAddress) | ||
if err != nil { | ||
log.WithError(err).Error("Invalid skycoin reward address " + rewardAddress) | ||
} | ||
log.Debug("Skycoin reward address: ", cAddr.String()) | ||
} | ||
survey, err := visorconfig.SystemSurvey() | ||
if err != nil { | ||
log.WithError(err).Error("Could not read system info.") | ||
} | ||
survey.PubKey = visorPK | ||
survey.SkycoinAddress = cAddr.String() | ||
// Print results. | ||
s, err := json.MarshalIndent(survey, "", "\t") | ||
if err != nil { | ||
log.WithError(err).Error("Could not marshal system survey to json.") | ||
} | ||
|
||
c.Header("Content-Type", "application/json") | ||
c.Writer.WriteHeader(http.StatusOK) | ||
|
||
_, err = c.Writer.Write([]byte(s)) | ||
if err != nil { | ||
httputil.GetLogger(c.Request).WithError(err).Errorf("failed to write json response") | ||
} | ||
Comment on lines
+85
to
+97
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I mentioned this before, but you can literally just do:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am intentionally using generic c.Writer.Write methods instead of gin's type-specific methods. It makes it more interchangeable with standard library http server in case we ever decide to move away from gin. |
||
}) | ||
|
||
r.GET("/health", func(c *gin.Context) { | ||
api.health(c) | ||
}) | ||
|
||
// serve transport log files ; then any files in the custom path | ||
r.GET("/:file", func(c *gin.Context) { | ||
// files with .csv extension are likely transport log files | ||
// files with .csv extension are **likely** transport log files | ||
if filepath.Ext(c.Param("file")) == ".csv" { | ||
// check transport logs dir for the file, and serve it if it exists | ||
_, err := os.Stat(filepath.Join(tpLogPath, c.Param("file"))) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,84 +54,3 @@ func (d *Duration) UnmarshalJSON(b []byte) error { | |
return errors.New("invalid duration") | ||
} | ||
} | ||
|
||
// VisorConfig is every field of the config | ||
type VisorConfig struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the old visorconfig, right? Any reason we are deleting it? Because its not used anymore and we dont expect users to have to upgrade from that? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that was something that I added there a long time ago that i think was used briefly but then not used |
||
Version string `json:"version"` | ||
Sk string `json:"sk"` | ||
Pk string `json:"pk"` | ||
Dmsg struct { | ||
Discovery string `json:"discovery"` | ||
SessionsCount int `json:"sessions_count"` | ||
Servers []struct { | ||
Version string `json:"version"` | ||
Sequence int `json:"sequence"` | ||
Timestamp int `json:"timestamp"` | ||
Static string `json:"static"` | ||
Server struct { | ||
Address string `json:"address"` | ||
AvailableSessions int `json:"availableSessions"` | ||
} `json:"server"` | ||
} `json:"servers"` | ||
} `json:"dmsg"` | ||
Dmsgpty struct { | ||
DmsgPort int `json:"dmsg_port"` | ||
CliNetwork string `json:"cli_network"` | ||
CliAddress string `json:"cli_address"` | ||
} `json:"dmsgpty"` | ||
SkywireTCP struct { | ||
PkTable interface{} `json:"pk_table"` | ||
ListeningAddress string `json:"listening_address"` | ||
} `json:"skywire-tcp"` | ||
Transport struct { | ||
Discovery string `json:"discovery"` | ||
AddressResolver string `json:"address_resolver"` | ||
PublicAutoconnect bool `json:"public_autoconnect"` | ||
TransportSetupNodes interface{} `json:"transport_setup_nodes"` | ||
} `json:"transport"` | ||
Routing struct { | ||
SetupNodes []string `json:"setup_nodes"` | ||
RouteFinder string `json:"route_finder"` | ||
RouteFinderTimeout string `json:"route_finder_timeout"` | ||
MinHops int `json:"min_hops"` | ||
} `json:"routing"` | ||
UptimeTracker struct { | ||
Addr string `json:"addr"` | ||
} `json:"uptime_tracker"` | ||
Launcher struct { | ||
ServiceDiscovery string `json:"service_discovery"` | ||
Apps []struct { | ||
Name string `json:"name"` | ||
AutoStart bool `json:"auto_start"` | ||
Port int `json:"port"` | ||
Args []string `json:"args,omitempty"` | ||
} `json:"apps"` | ||
ServerAddr string `json:"server_addr"` | ||
BinPath string `json:"bin_path"` | ||
} `json:"launcher"` | ||
Hypervisors []interface{} `json:"hypervisors"` | ||
CliAddr string `json:"cli_addr"` | ||
LogLevel string `json:"log_level"` | ||
LocalPath string `json:"local_path"` | ||
StunServers []string `json:"stun_servers"` | ||
ShutdownTimeout string `json:"shutdown_timeout"` | ||
RestartCheckDelay string `json:"restart_check_delay"` | ||
IsPublic bool `json:"is_public"` | ||
PersistentTransports interface{} `json:"persistent_transports"` | ||
Hypervisor struct { | ||
DbPath string `json:"db_path"` | ||
EnableAuth bool `json:"enable_auth"` | ||
Cookies struct { | ||
HashKey string `json:"hash_key"` | ||
BlockKey string `json:"block_key"` | ||
ExpiresDuration int64 `json:"expires_duration"` | ||
Path string `json:"path"` | ||
Domain string `json:"domain"` | ||
} `json:"cookies"` | ||
DmsgPort int `json:"dmsg_port"` | ||
HTTPAddr string `json:"http_addr"` | ||
EnableTLS bool `json:"enable_tls"` | ||
TLSCertFile string `json:"tls_cert_file"` | ||
TLSKeyFile string `json:"tls_key_file"` | ||
} `json:"hypervisor"` | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need to define the HTTP path here. We dont need to change it frequently and can just hardcode it as its used exactly once.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will change
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've dropped the extra declaration SurveyName, but instead of directly hardcoding I've opted for this implementation: