oc-discovery/models/identity.go

45 lines
816 B
Go
Raw Permalink Normal View History

2023-03-07 13:29:08 +01:00
package models
import (
2023-03-08 16:48:36 +01:00
"encoding/json"
"os"
"github.com/beego/beego/logs"
2023-03-07 13:29:08 +01:00
)
var (
Me Identity
)
func init() {
2023-03-08 16:48:36 +01:00
content, err := os.ReadFile("./identity.json")
if err != nil {
logs.Error("Error when opening file: ", err)
}
err = json.Unmarshal(content, &Me)
if err != nil {
logs.Error("Error during Unmarshal(): ", err)
}
2023-03-07 13:29:08 +01:00
}
type Identity struct {
2023-03-08 16:48:36 +01:00
Id string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
PrivateKey string `json:"private_key,omitempty"`
PublicAttributes Peer `json:"public_attributes,omitempty"`
2023-03-07 13:29:08 +01:00
}
func GetIdentity() (u *Identity) {
return &Me
}
2023-03-08 16:48:36 +01:00
func UpdateIdentity(uu *Identity) error {
2023-03-07 13:29:08 +01:00
Me = *uu
2023-03-08 16:48:36 +01:00
jsonBytes, err := json.Marshal(uu)
if err != nil {
return err
}
os.WriteFile("./identity.json", jsonBytes, 0600)
return nil
2023-03-07 13:29:08 +01:00
}