new start
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
package main
|
||||
|
||||
import "net/http"
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func isMethodAllowed(response *http.ResponseWriter, request *http.Request) bool {
|
||||
if request.Method != http.MethodPost {
|
||||
@@ -10,7 +16,23 @@ func isMethodAllowed(response *http.ResponseWriter, request *http.Request) bool
|
||||
return true
|
||||
}
|
||||
|
||||
func RegisterHandler(response http.ResponseWriter, request *http.Request) {
|
||||
func parseRgb(str string) ([3]uint8, error) {
|
||||
parts := strings.SplitN(str, ",", 4)
|
||||
if len(parts) != 3 {
|
||||
return [3]uint8{}, fmt.Errorf("invalid rgb")
|
||||
}
|
||||
var rgb [3]uint8
|
||||
for i, p := range parts {
|
||||
n, err := strconv.ParseUint(strings.TrimSpace(p), 10, 8)
|
||||
if err != nil {
|
||||
return [3]uint8{}, fmt.Errorf("invalid component %d: %w", i, err)
|
||||
}
|
||||
rgb[i] = uint8(n)
|
||||
}
|
||||
return rgb, nil
|
||||
}
|
||||
|
||||
func HttpHandleNewUser(response http.ResponseWriter, request *http.Request) {
|
||||
if !isMethodAllowed(&response, request) {
|
||||
return
|
||||
}
|
||||
@@ -29,21 +51,32 @@ func RegisterHandler(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
newClient := Client{
|
||||
color, err := parseRgb(request.FormValue("color"))
|
||||
if err != nil {
|
||||
http.Error(response, "bad color", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
hashedPassword, err := PasswordHash(password)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
newClient := &Client{
|
||||
Name: username,
|
||||
PasswordHash: password,
|
||||
Color: [3]uint8{120, 120, 120}, //"xxx"
|
||||
PasswordHash: hashedPassword,
|
||||
Color: color,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
err := CreateClient(ctx, &newClient)
|
||||
err = DbSaveClientWithoutGroups(ctx, newClient)
|
||||
if err != nil {
|
||||
http.Error(response, "taken", http.StatusBadRequest)
|
||||
http.Error(response, "name taken", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
response.Write([]byte("registered"))
|
||||
}
|
||||
|
||||
func LoginHandler(response http.ResponseWriter, request *http.Request) {
|
||||
func HttpHandleLogin(response http.ResponseWriter, request *http.Request) {
|
||||
if !isMethodAllowed(&response, request) {
|
||||
return
|
||||
}
|
||||
@@ -62,25 +95,6 @@ func LoginHandler(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
var client Client
|
||||
_, err := CacheGetClientById()
|
||||
|
||||
id, err := GetIdFromClientName(ctx, username)
|
||||
if err != nil {
|
||||
http.Error(response, "bad login", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = DbGetClient(ctx)
|
||||
if err != nil {
|
||||
http.Error(response, "bad login", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
token, err := GetToken(id)
|
||||
if err != nil {
|
||||
http.Error(response, "Internal error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
response.Write([]byte(token))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user