feat: initial commit
tx-sync build / build (tx-sync-linux-amd64, amd64, linux) (push) Failing after 50s
tx-sync build / build (tx-sync-windows-amd64.exe, amd64, windows) (push) Failing after 6s

This commit is contained in:
2026-03-21 01:02:52 +00:00
parent 69aa23bc89
commit 38dc37149c
10 changed files with 488 additions and 0 deletions
+46
View File
@@ -0,0 +1,46 @@
package sync
import (
"bytes"
"context"
"fmt"
"io"
"net/http"
"os"
"time"
)
// Upload posts the raw playersDB.json bytes to endpoint with X-API-Key and Content-Type: application/json.
func Upload(ctx context.Context, client *http.Client, endpoint, apiKey string, body []byte) error {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(body))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-API-Key", apiKey)
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
snippet, _ := io.ReadAll(io.LimitReader(resp.Body, 2048))
return fmt.Errorf("HTTP %d: %s", resp.StatusCode, bytes.TrimSpace(snippet))
}
_, _ = io.Copy(io.Discard, resp.Body)
return nil
}
// ReadPlayersDB reads the file from disk.
func ReadPlayersDB(path string) ([]byte, error) {
return os.ReadFile(path)
}
// NewHTTPClient returns a client suitable for large JSON uploads.
func NewHTTPClient() *http.Client {
return &http.Client{
Timeout: 90 * time.Second,
}
}
+53
View File
@@ -0,0 +1,53 @@
package sync
import (
"context"
"io"
"net/http"
"net/http/httptest"
"testing"
)
func TestUpload(t *testing.T) {
const wantKey = "secret"
var gotBody []byte
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("method %s", r.Method)
}
if r.Header.Get("X-API-Key") != wantKey {
t.Errorf("X-API-Key")
}
if ct := r.Header.Get("Content-Type"); ct != "application/json" {
t.Errorf("Content-Type %q", ct)
}
var err error
gotBody, err = io.ReadAll(r.Body)
if err != nil {
t.Fatal(err)
}
w.WriteHeader(http.StatusNoContent)
}))
defer srv.Close()
client := srv.Client()
body := []byte(`{"ok":true}`)
if err := Upload(context.Background(), client, srv.URL, wantKey, body); err != nil {
t.Fatal(err)
}
if string(gotBody) != string(body) {
t.Fatalf("body %q", gotBody)
}
}
func TestUploadNon2xx(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "nope", http.StatusTeapot)
}))
defer srv.Close()
err := Upload(context.Background(), srv.Client(), srv.URL, "k", []byte("{}"))
if err == nil {
t.Fatal("expected error")
}
}