Move code for key registration

This commit is contained in:
cheddar 2025-02-20 20:33:59 -05:00
parent cab114fae3
commit 30cdf2c7e7
No known key found for this signature in database
2 changed files with 44 additions and 23 deletions

43
client/register.go Normal file
View file

@ -0,0 +1,43 @@
package client
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"crispbyte.dev/sig-auth/server"
)
func RegisterKey(key string, userId string) error {
request := server.RegisterRequest{
UserId: userId,
Key: key,
}
json_data, _ := json.Marshal(request)
resp, err := http.DefaultClient.Post(
"http://localhost:8080/register",
"application/json",
bytes.NewBuffer(json_data))
if err != nil {
return err
}
defer resp.Body.Close()
out, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
fmt.Println(resp.StatusCode)
fmt.Println(resp.Header)
fmt.Println(string(out[:]))
return nil
}

24
main.go
View file

@ -130,31 +130,9 @@ func registerKey(keyFile string, userId string) {
keyText := string(keyBytes)
request := server.RegisterRequest{
UserId: userId,
Key: keyText,
}
json_data, _ := json.Marshal(request)
resp, err := http.DefaultClient.Post(
"http://localhost:8080/register",
"application/json",
bytes.NewBuffer(json_data))
err = client.RegisterKey(keyText, userId)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
out, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Println(resp.StatusCode)
fmt.Println(resp.Header)
fmt.Println(string(out[:]))
}