45 lines
816 B
Go
45 lines
816 B
Go
package models
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
|
|
"github.com/beego/beego/logs"
|
|
)
|
|
|
|
var (
|
|
Me Identity
|
|
)
|
|
|
|
func init() {
|
|
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)
|
|
}
|
|
}
|
|
|
|
type Identity struct {
|
|
Id string `json:"id,omitempty"`
|
|
Name string `json:"name,omitempty"`
|
|
PrivateKey string `json:"private_key,omitempty"`
|
|
PublicAttributes Peer `json:"public_attributes,omitempty"`
|
|
}
|
|
|
|
func GetIdentity() (u *Identity) {
|
|
return &Me
|
|
}
|
|
|
|
func UpdateIdentity(uu *Identity) error {
|
|
Me = *uu
|
|
jsonBytes, err := json.Marshal(uu)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
os.WriteFile("./identity.json", jsonBytes, 0600)
|
|
return nil
|
|
}
|