sig-auth.git

git clone https://git.crispbyte.dev/sig-auth.git

commit
30cdf2c
parent
cab114f
author
cheddar
date
2025-02-21 02:33:59 +0100 CET
Move code for key registration
2 files changed,  +44, -23
A client/register.go
+43, -0
 1@@ -0,0 +1,43 @@
 2+package client
 3+
 4+import (
 5+	"bytes"
 6+	"encoding/json"
 7+	"fmt"
 8+	"io"
 9+	"net/http"
10+
11+	"crispbyte.dev/sig-auth/server"
12+)
13+
14+func RegisterKey(key string, userId string) error {
15+	request := server.RegisterRequest{
16+		UserId: userId,
17+		Key:    key,
18+	}
19+
20+	json_data, _ := json.Marshal(request)
21+
22+	resp, err := http.DefaultClient.Post(
23+		"http://localhost:8080/register",
24+		"application/json",
25+		bytes.NewBuffer(json_data))
26+
27+	if err != nil {
28+		return err
29+	}
30+
31+	defer resp.Body.Close()
32+
33+	out, err := io.ReadAll(resp.Body)
34+
35+	if err != nil {
36+		return err
37+	}
38+
39+	fmt.Println(resp.StatusCode)
40+	fmt.Println(resp.Header)
41+	fmt.Println(string(out[:]))
42+
43+	return nil
44+}
M main.go
+1, -23
 1@@ -130,31 +130,9 @@ func registerKey(keyFile string, userId string) {
 2 
 3 	keyText := string(keyBytes)
 4 
 5-	request := server.RegisterRequest{
 6-		UserId: userId,
 7-		Key:    keyText,
 8-	}
 9-
10-	json_data, _ := json.Marshal(request)
11-
12-	resp, err := http.DefaultClient.Post(
13-		"http://localhost:8080/register",
14-		"application/json",
15-		bytes.NewBuffer(json_data))
16+	err = client.RegisterKey(keyText, userId)
17 
18 	if err != nil {
19 		log.Fatal(err)
20 	}
21-
22-	defer resp.Body.Close()
23-
24-	out, err := io.ReadAll(resp.Body)
25-
26-	if err != nil {
27-		log.Fatal(err)
28-	}
29-
30-	fmt.Println(resp.StatusCode)
31-	fmt.Println(resp.Header)
32-	fmt.Println(string(out[:]))
33 }