i wanted to add full cahce but i dont have time :(
This commit is contained in:
@@ -22,6 +22,26 @@ func CacheGetClientById(id uint32) (*Client, error) {
|
|||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CacheGetIdByName(name string) (uint32, error) {
|
||||||
|
client, err := CacheGetClientByName(name)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return client.Id, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CacheGetClientByName(name string) (*Client, error) {
|
||||||
|
mu.RLock()
|
||||||
|
defer mu.RUnlock()
|
||||||
|
|
||||||
|
for _, client := range CacheClients {
|
||||||
|
if client.Name == name {
|
||||||
|
return client, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("client %s not found", name)
|
||||||
|
}
|
||||||
|
|
||||||
func CacheSetClient(client *Client) {
|
func CacheSetClient(client *Client) {
|
||||||
mu.Lock()
|
mu.Lock()
|
||||||
defer mu.Unlock()
|
defer mu.Unlock()
|
||||||
|
|||||||
@@ -73,6 +73,14 @@ func DbSaveClientWithoutGroups(ctx context.Context, client *Client) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DbGetIdByClientName(ctx context.Context, name string) (uint32, error) {
|
||||||
|
var id uint32
|
||||||
|
err := dbConn.QueryRow(ctx, `
|
||||||
|
SELECT id FROM clients WHERE name = $1
|
||||||
|
`, name).Scan(&id)
|
||||||
|
return id, err
|
||||||
|
}
|
||||||
|
|
||||||
func DbSetClientByName(ctx context.Context, client *Client) error {
|
func DbSetClientByName(ctx context.Context, client *Client) error {
|
||||||
err := dbConn.QueryRow(ctx, `
|
err := dbConn.QueryRow(ctx, `
|
||||||
SELECT name, pass_hash, color_red, color_green, color_blue, created_at FROM clients WHERE name = $1
|
SELECT name, pass_hash, color_red, color_green, color_blue, created_at FROM clients WHERE name = $1
|
||||||
|
|||||||
@@ -88,33 +88,24 @@ func HttpHandleLogin(response http.ResponseWriter, request *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var client = Client{Name: username}
|
if len(username) < 8 {
|
||||||
if len(client.Name) < 8 {
|
|
||||||
http.Error(response, "no or short password", http.StatusBadRequest)
|
http.Error(response, "no or short password", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx := request.Context()
|
var (
|
||||||
|
clientId uint32
|
||||||
|
err error
|
||||||
|
ctx = request.Context()
|
||||||
|
)
|
||||||
|
|
||||||
err := DbSetClientByName(ctx, &client)
|
clientId, err = CacheGetIdByName(username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(response, "bad login", http.StatusUnauthorized)
|
clientId, err = DbGetIdByClientName(ctx, username)
|
||||||
return
|
if err != nil {
|
||||||
}
|
|
||||||
|
|
||||||
_, err = CacheGetClientById(client.Id)
|
|
||||||
if err == nil {
|
|
||||||
otherLoggedIn, err := CacheGetClientById(client.Id)
|
|
||||||
if err == nil {
|
|
||||||
otherLoggedIn.WsConn.CloseNow()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
token, err := TokenCreate(client.Id)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
response.Write([]byte(token))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func HttpHandleGroupCreate(response http.ResponseWriter, request *http.Request) {
|
func HttpHandleGroupCreate(response http.ResponseWriter, request *http.Request) {
|
||||||
|
|||||||
Reference in New Issue
Block a user