2023-03-07 13:29:08 +01:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/beego/beego/logs"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
Peers []Peer
|
2023-03-08 16:48:36 +01:00
|
|
|
Store Storage
|
2023-03-07 13:29:08 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type Peer struct {
|
2023-03-08 16:48:36 +01:00
|
|
|
PeerId string `json:"peer_id,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
EntityName string `json:"entity_name,omitempty"`
|
|
|
|
EntityType string `json:"entity_type,omitempty"`
|
|
|
|
Description string `json:"description,omitempty"`
|
|
|
|
Website string `json:"website,omitempty"`
|
|
|
|
Address string `json:"address,omitempty"`
|
|
|
|
Postcode string `json:"postcode,omitempty"`
|
|
|
|
City string `json:"city,omitempty"`
|
|
|
|
Country string `json:"country,omitempty"`
|
|
|
|
Phone string `json:"phone,omitempty"`
|
|
|
|
Email string `json:"email,omitempty"`
|
|
|
|
Activity string `json:"activity,omitempty"`
|
|
|
|
Keywords []string `json:"keywords,omitempty"`
|
|
|
|
ApiUrl string `json:"url,omitempty"`
|
|
|
|
PublicKey string `json:"public_key,omitempty"`
|
|
|
|
// internal use
|
2023-03-07 13:29:08 +01:00
|
|
|
Score int64 `json:"score,omitempty"`
|
|
|
|
LastSeenOnline time.Time `json:"last_seen_online,omitempty"`
|
|
|
|
ApiVersion string `json:"api_version,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2023-03-08 16:48:36 +01:00
|
|
|
Store = Storage{"http://localhost:4080", "admin", "admin", "localhost:6379", ""}
|
2023-03-07 13:29:08 +01:00
|
|
|
//p := Peer{uuid.New().String(), 0, []string{"car", "highway", "images", "video"}, time.Now(), "1", "asf", ""}
|
|
|
|
// pa := []Peer{p}
|
|
|
|
// byteArray, err := json.Marshal(pa)
|
|
|
|
// if err != nil {
|
|
|
|
// log.Fatal(err)
|
|
|
|
// }
|
|
|
|
// ioutil.WriteFile("./peers.json", byteArray, 0644)
|
|
|
|
content, err := ioutil.ReadFile("./peers.json")
|
|
|
|
if err != nil {
|
|
|
|
logs.Error("Error when opening file: ", err)
|
|
|
|
}
|
|
|
|
err = json.Unmarshal(content, &Peers)
|
|
|
|
if err != nil {
|
|
|
|
logs.Error("Error during Unmarshal(): ", err)
|
|
|
|
}
|
2023-03-08 16:48:36 +01:00
|
|
|
Store.ImportData(LoadPeersJson("./peers.json"))
|
2023-03-07 13:29:08 +01:00
|
|
|
}
|
|
|
|
|
2023-03-08 16:48:36 +01:00
|
|
|
func AddPeers(peers []Peer) (status string) {
|
|
|
|
err := Store.ImportData(peers)
|
|
|
|
if err != nil {
|
|
|
|
logs.Error("Error during Unmarshal(): ", err)
|
|
|
|
return "error"
|
|
|
|
}
|
|
|
|
return "ok"
|
2023-03-07 13:29:08 +01:00
|
|
|
}
|
|
|
|
|
2023-03-08 16:48:36 +01:00
|
|
|
func FindPeers(query string) (peers []Peer, err error) {
|
|
|
|
result, err := Store.FindPeers(query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
2023-03-07 13:29:08 +01:00
|
|
|
}
|
|
|
|
|
2023-03-08 16:48:36 +01:00
|
|
|
func GetPeer(uid string) (*Peer, error) {
|
|
|
|
return Store.GetPeer(uid)
|
2023-03-07 13:29:08 +01:00
|
|
|
}
|
|
|
|
|
2023-03-08 16:48:36 +01:00
|
|
|
func Delete(PeerId string) error {
|
|
|
|
err := Store.DeletePeer(PeerId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2023-03-07 13:29:08 +01:00
|
|
|
}
|