82 lines
1.6 KiB
Go
82 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
type NodeInfoResponseSection struct {
|
|
Rel string `json:"rel"`
|
|
Href string `json:"href"`
|
|
}
|
|
|
|
type NodeInfoResponse struct {
|
|
Links []NodeInfoResponseSection `json:"links"`
|
|
}
|
|
|
|
type NodeInfo struct {
|
|
Software NodeInfoSoftware `json:"software"`
|
|
}
|
|
|
|
type NodeInfoSoftware struct {
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
func getSoftware(instance string) string {
|
|
// check if instance exists in db already
|
|
result := DbRunQuery(fmt.Sprintf("MATCH (n:Instance {name: '%s'}) RETURN n.software;", instance))
|
|
if len(result.Records) > 0 {
|
|
return result.Records[0].Values[0].(string)
|
|
}
|
|
|
|
client := &http.Client{}
|
|
req, err := http.NewRequest("GET", "https://"+instance+"/.well-known/nodeinfo", nil)
|
|
if err != nil {
|
|
fmt.Printf("error: %s\n", err)
|
|
return "err"
|
|
}
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
fmt.Printf("error: %s\n", err)
|
|
return "err"
|
|
}
|
|
|
|
jsonDecoder := json.NewDecoder(resp.Body)
|
|
var nodeinfo NodeInfoResponse
|
|
err = jsonDecoder.Decode(&nodeinfo)
|
|
if err != nil {
|
|
fmt.Printf("error: %s\n", err)
|
|
return "err"
|
|
}
|
|
|
|
if len(nodeinfo.Links) == 0 {
|
|
return "err"
|
|
}
|
|
|
|
req, err = http.NewRequest("GET", nodeinfo.Links[0].Href, nil)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return "err"
|
|
}
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
resp, err = client.Do(req)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return "err"
|
|
}
|
|
|
|
jsonDecoder = json.NewDecoder(resp.Body)
|
|
var node NodeInfo
|
|
err = jsonDecoder.Decode(&node)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return "err"
|
|
}
|
|
|
|
return node.Software.Name
|
|
}
|