mirror of
https://git.klmp200.net/ALFRED/ALFRED.git
synced 2025-05-24 12:51:16 +02:00
Add Users infos and gender registration
This commit is contained in:
parent
85b52e23e5
commit
7030d6d595
5 changed files with 126 additions and 8 deletions
82
shared/users.go
Normal file
82
shared/users.go
Normal file
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* @Author: Bartuccio Antoine
|
||||
* @Date: 2018-07-24 14:41:03
|
||||
* @Last Modified by: klmp200
|
||||
* @Last Modified time: 2018-07-24 15:23:06
|
||||
*/
|
||||
|
||||
package shared
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type users struct {
|
||||
mutex sync.Mutex
|
||||
data map[int]map[string]string
|
||||
}
|
||||
|
||||
type usersFile struct {
|
||||
mutex sync.Mutex
|
||||
path string
|
||||
}
|
||||
|
||||
var Users users
|
||||
var uf usersFile
|
||||
|
||||
func InitUsers(users_file_path string) {
|
||||
uf = usersFile{}
|
||||
uf.path = users_file_path
|
||||
Users = users{}
|
||||
Users.mutex.Lock()
|
||||
defer Users.mutex.Unlock()
|
||||
Users.data = make(map[int]map[string]string)
|
||||
uf.read()
|
||||
}
|
||||
|
||||
// Get an info about a given user
|
||||
func (u users) Get(id int, key string) (string, bool) {
|
||||
u.mutex.Lock()
|
||||
defer u.mutex.Unlock()
|
||||
user, exists := u.data[id]
|
||||
if !exists {
|
||||
return "", false
|
||||
}
|
||||
if _, exists = user[key]; !exists {
|
||||
return "", false
|
||||
}
|
||||
return user[key], true
|
||||
}
|
||||
|
||||
// Add an info about a given user
|
||||
func (u users) Set(id int, key, data string) {
|
||||
u.mutex.Lock()
|
||||
defer u.mutex.Unlock()
|
||||
if _, exists := u.data[id]; !exists {
|
||||
u.data[id] = make(map[string]string)
|
||||
}
|
||||
u.data[id][key] = data
|
||||
go uf.write()
|
||||
}
|
||||
|
||||
func (u usersFile) read() {
|
||||
u.mutex.Lock()
|
||||
defer u.mutex.Unlock()
|
||||
data, err := ioutil.ReadFile(u.path)
|
||||
if err != nil {
|
||||
// File doesn't exist, skip import
|
||||
return
|
||||
}
|
||||
json.Unmarshal(data, &Users.data)
|
||||
}
|
||||
|
||||
func (u usersFile) write() {
|
||||
u.mutex.Lock()
|
||||
defer u.mutex.Unlock()
|
||||
Users.mutex.Lock()
|
||||
defer Users.mutex.Unlock()
|
||||
data, _ := json.Marshal(Users.data)
|
||||
ioutil.WriteFile(u.path, data, 0770)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue