oc-discovery/models/peer.go

70 lines
1.5 KiB
Go
Raw Normal View History

2023-03-07 13:29:08 +01:00
package models
import (
"encoding/json"
"errors"
"io/ioutil"
"strconv"
"time"
"github.com/beego/beego/logs"
)
var (
Peers []Peer
)
type Peer struct {
PeerId string `json:"peer_id,omitempty"`
Name string `json:"name,omitempty"`
Score int64 `json:"score,omitempty"`
Keywords []string `json:"keywords,omitempty"`
LastSeenOnline time.Time `json:"last_seen_online,omitempty"`
ApiVersion string `json:"api_version,omitempty"`
Url string `json:"url,omitempty"`
PublicKey string `json:"public_key,omitempty"`
}
func init() {
//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)
}
}
func AddOne(peer Peer) (PeerId string) {
peer.PeerId = "astaxie" + strconv.FormatInt(time.Now().UnixNano(), 10)
Peers = append(Peers, peer)
return peer.PeerId
}
func GetOne(PeerId string) (peer *Peer, err error) {
return nil, errors.New("PeerId Not Exist")
}
func GetAll() []Peer {
return Peers
}
func Update(PeerId string, Score int64) (err error) {
return errors.New("PeerId Not Exist")
}
func Delete(PeerId string) {
}