feat: boxy integration (todo: test)

This commit is contained in:
hexlocation 2025-08-01 18:24:13 +02:00
parent 81aa06d716
commit 32e186d502
2 changed files with 53 additions and 2 deletions

View file

@ -32,3 +32,14 @@ POSTGRES_DSN=
# Random secret. Recommended length is 64 characters at minimum.
JWT_SECRET=
# URL to Boxy's API.
# e.g. http://localhost:8001
# without ending forward slash (/)
BOXY_URL=
# Address by which Boxy can reach us. If it is not set, it will be the address with which we'll reach Boxy's API.
BOXY_CLIENT_ADDRESS=
BOXY_CLIENT_NAME=
BOXY_CLIENT_SECRET=

44
main.go
View file

@ -18,11 +18,15 @@
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
"os"
"strconv"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
@ -56,6 +60,9 @@ func main() {
databaseType := getEnv("DATABASE_TYPE", "sqlite")
sqliteFile := getEnv("SQLITE_FILE", "stereo.db")
boxyApiUrl := getEnv("BOXY_URL", "")
port := getEnv("PORT", "8081")
imagePath := getEnv("IMAGE_PATH", os.TempDir())
if _, err := os.Stat(imagePath); err != nil {
@ -82,7 +89,7 @@ func main() {
Router: gin.Default(),
MinioClient: minioClient,
Bucket: requireEnv("S3_BUCKET"),
Context: context.Background(),
Context: context.Background(),
ImagePath: imagePath,
Client: client.New(
requireEnv("REDIRECT_URI"),
@ -122,7 +129,40 @@ func main() {
c.Database.AutoMigrate(&auth.User{}, &types.File{})
if boxyApiUrl != "" {
boxyClientName := requireEnv("BOXY_CLIENT_NAME")
boxyClientSecret := requireEnv("BOXY_CLIENT_SECRET")
boxyClientAddress := getEnv("BOXY_CLIENT_ADDRESS", "")
rawBody := map[string]any{
"port": port,
"hostname": c.Domain,
}
if boxyClientAddress != "" {
rawBody["address"] = boxyClientAddress
}
jBody, err := json.Marshal(rawBody)
if err != nil {
log.Fatal(err)
}
req, err := http.NewRequest("POST", boxyApiUrl+"/register", bytes.NewBuffer(jBody))
req.SetBasicAuth(boxyClientName, boxyClientSecret)
http.DefaultClient.Do(req)
if req.Response.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)
fmt.Printf("Running on port %s\n", getEnv("PORT", "8081"))
fmt.Printf("Running on port %s\n", port)
c.Router.Run()
}