feat: basic boxy client
This commit is contained in:
parent
cad2855220
commit
8522e60af3
3 changed files with 111 additions and 26 deletions
38
main.go
38
main.go
|
@ -18,13 +18,10 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -39,6 +36,7 @@ import (
|
|||
"stereo.cat/backend/internal/api"
|
||||
"stereo.cat/backend/internal/auth"
|
||||
"stereo.cat/backend/internal/auth/client"
|
||||
"stereo.cat/backend/internal/boxy"
|
||||
"stereo.cat/backend/internal/types"
|
||||
)
|
||||
|
||||
|
@ -137,34 +135,22 @@ func main() {
|
|||
|
||||
port, err := strconv.Atoi(strings.Split(listenAddr, ":")[1])
|
||||
|
||||
fmt.Printf("Using port %d\n", port)
|
||||
|
||||
rawBody := map[string]any{
|
||||
"port": port,
|
||||
"hostname": c.Domain,
|
||||
}
|
||||
if boxyClientAddress != "" {
|
||||
rawBody["address"] = boxyClientAddress
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
jBody, err := json.Marshal(rawBody)
|
||||
c.BoxyClient = boxy.BoxyClient{
|
||||
Name: boxyClientName,
|
||||
Secret: boxyClientSecret,
|
||||
ApiUrl: boxyApiUrl,
|
||||
Hostnames: []string{c.Domain},
|
||||
}
|
||||
|
||||
err = c.BoxyClient.Register(boxyClientAddress, port)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
panic(err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", boxyApiUrl+"/register", bytes.NewBuffer(jBody))
|
||||
|
||||
req.SetBasicAuth(boxyClientName, boxyClientSecret)
|
||||
|
||||
resp, _ := http.DefaultClient.Do(req)
|
||||
|
||||
if resp.StatusCode != 200 {
|
||||
log.Fatal("Could not register with Boxy.\nStatus Code: " + strconv.Itoa(req.Response.StatusCode))
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("Successfully registered with Boxy.")
|
||||
}
|
||||
|
||||
api.Register(&c)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue