Move code for key registration
This commit is contained in:
parent
cab114fae3
commit
30cdf2c7e7
2 changed files with 44 additions and 23 deletions
43
client/register.go
Normal file
43
client/register.go
Normal 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
24
main.go
|
@ -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[:]))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue