complete logic for login and responses of http functions
This commit is contained in:
@@ -75,6 +75,13 @@ func HttpHandleNewUser(response http.ResponseWriter, request *http.Request) {
|
|||||||
http.Error(response, "name taken", http.StatusUnauthorized)
|
http.Error(response, "name taken", http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
response.WriteHeader(http.StatusAccepted)
|
||||||
|
_, err = response.Write([]byte("created"))
|
||||||
|
if err != nil {
|
||||||
|
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func HttpHandleLogin(response http.ResponseWriter, request *http.Request) {
|
func HttpHandleLogin(response http.ResponseWriter, request *http.Request) {
|
||||||
@@ -94,17 +101,33 @@ func HttpHandleLogin(response http.ResponseWriter, request *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
clientId uint32
|
client *Client
|
||||||
err error
|
err error
|
||||||
ctx = request.Context()
|
ctx = request.Context()
|
||||||
)
|
)
|
||||||
|
|
||||||
clientId, err = CacheGetIdByName(username)
|
client, err = CacheGetClientByName(username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
clientId, err = DbGetIdByClientName(ctx, username)
|
err := DbSetClientByName(ctx, client)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
http.Error(response, "bad login", http.StatusBadRequest)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
CacheSetClient(client)
|
||||||
|
}
|
||||||
|
|
||||||
|
token, err := TokenCreate(client.Id)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
response.WriteHeader(http.StatusAccepted)
|
||||||
|
_, err = response.Write([]byte(token))
|
||||||
|
if err != nil {
|
||||||
|
CacheDeleteClient(client.Id)
|
||||||
|
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,7 +192,9 @@ func HttpHandleGroupCreate(response http.ResponseWriter, request *http.Request)
|
|||||||
}
|
}
|
||||||
groupIdBytes := make([]byte, 4)
|
groupIdBytes := make([]byte, 4)
|
||||||
binary.BigEndian.PutUint32(groupIdBytes, group.Id)
|
binary.BigEndian.PutUint32(groupIdBytes, group.Id)
|
||||||
response.Write(groupIdBytes)
|
|
||||||
|
response.WriteHeader(http.StatusCreated)
|
||||||
|
response.Write([]byte(groupIdBytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
func HttpHandleGroupAddClient(response http.ResponseWriter, request *http.Request) {
|
func HttpHandleGroupAddClient(response http.ResponseWriter, request *http.Request) {
|
||||||
@@ -241,4 +266,11 @@ func HttpHandleGroupAddClient(response http.ResponseWriter, request *http.Reques
|
|||||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
response.WriteHeader(http.StatusAccepted)
|
||||||
|
_, err = response.Write([]byte("ok"))
|
||||||
|
if err != nil {
|
||||||
|
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user