89 lines
2.3 KiB
Go
89 lines
2.3 KiB
Go
package models
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io/ioutil"
|
|
"time"
|
|
|
|
"github.com/beego/beego/logs"
|
|
)
|
|
|
|
var (
|
|
Peers []Peer
|
|
Store Storage
|
|
)
|
|
|
|
type Peer struct {
|
|
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:"api_url,omitempty"`
|
|
PublicKey string `json:"public_key,omitempty"`
|
|
// internal use
|
|
Score int64 `json:"score,omitempty"`
|
|
LastSeenOnline time.Time `json:"last_seen_online,omitempty"`
|
|
ApiVersion string `json:"api_version,omitempty"`
|
|
}
|
|
|
|
func init() {
|
|
c := GetConfig()
|
|
Store = Storage{c.ZincUrl, c.ZincLogin, c.ZincPassword, c.RedisUrl, c.RedisPassword}
|
|
Store = Storage{"http://localhost:4080", "admin", "admin", "localhost:6379", ""}
|
|
//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)
|
|
}
|
|
Store.ImportData(LoadPeersJson("./peers.json"))
|
|
}
|
|
|
|
func AddPeers(peers []Peer) (status string) {
|
|
err := Store.ImportData(peers)
|
|
if err != nil {
|
|
logs.Error("Error during Unmarshal(): ", err)
|
|
return "error"
|
|
}
|
|
return "ok"
|
|
}
|
|
|
|
func FindPeers(query string) (peers []Peer, err error) {
|
|
result, err := Store.FindPeers(query)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func GetPeer(uid string) (*Peer, error) {
|
|
return Store.GetPeer(uid)
|
|
}
|
|
|
|
func Delete(PeerId string) error {
|
|
err := Store.DeletePeer(PeerId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|