107 lines
2.7 KiB
Go
107 lines
2.7 KiB
Go
|
|
// Package ledger provides a client for aamos-ledger integration.
|
||
|
|
// One proxy method, not six copies. Linus-style.
|
||
|
|
package ledger
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
"fmt"
|
||
|
|
"net/http"
|
||
|
|
"os"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
var baseURL = getEnv("LEDGER_URL", "http://localhost:3250")
|
||
|
|
|
||
|
|
func getEnv(key, fallback string) string {
|
||
|
|
if v := os.Getenv(key); v != "" {
|
||
|
|
return v
|
||
|
|
}
|
||
|
|
return fallback
|
||
|
|
}
|
||
|
|
|
||
|
|
// Client handles communication with aamos-ledger
|
||
|
|
type Client struct {
|
||
|
|
baseURL string
|
||
|
|
client *http.Client
|
||
|
|
}
|
||
|
|
|
||
|
|
// NewClient creates a new ledger client
|
||
|
|
func NewClient() *Client {
|
||
|
|
return &Client{
|
||
|
|
baseURL: baseURL,
|
||
|
|
client: &http.Client{Timeout: 10 * time.Second},
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Get proxies a GET request to the ledger and returns the JSON response.
|
||
|
|
// This replaces 6 identical methods with one.
|
||
|
|
func (c *Client) Get(ctx context.Context, path string) (map[string]interface{}, error) {
|
||
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.baseURL+path, nil)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("create request: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
resp, err := c.client.Do(req)
|
||
|
|
if err != nil {
|
||
|
|
return nil, fmt.Errorf("ledger unavailable: %w", err)
|
||
|
|
}
|
||
|
|
defer resp.Body.Close()
|
||
|
|
|
||
|
|
if resp.StatusCode != http.StatusOK {
|
||
|
|
return nil, fmt.Errorf("ledger error: status %d", resp.StatusCode)
|
||
|
|
}
|
||
|
|
|
||
|
|
var result map[string]interface{}
|
||
|
|
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||
|
|
return nil, fmt.Errorf("decode error: %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
return result, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// Handler wraps the client for HTTP handlers
|
||
|
|
type Handler struct {
|
||
|
|
client *Client
|
||
|
|
}
|
||
|
|
|
||
|
|
// NewHandler creates a new ledger HTTP handler
|
||
|
|
func NewHandler() *Handler {
|
||
|
|
return &Handler{client: NewClient()}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Proxy handles any ledger endpoint with a single method
|
||
|
|
func (h *Handler) Proxy(w http.ResponseWriter, r *http.Request, ledgerPath string) {
|
||
|
|
result, err := h.client.Get(r.Context(), ledgerPath)
|
||
|
|
if err != nil {
|
||
|
|
w.Header().Set("Content-Type", "application/json")
|
||
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
||
|
|
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
w.Header().Set("Content-Type", "application/json")
|
||
|
|
json.NewEncoder(w).Encode(result)
|
||
|
|
}
|
||
|
|
|
||
|
|
// Convenience methods that use Proxy internally
|
||
|
|
func (h *Handler) GetBalanceSheet(w http.ResponseWriter, r *http.Request) {
|
||
|
|
h.Proxy(w, r, "/api/ledger/reports/balance")
|
||
|
|
}
|
||
|
|
|
||
|
|
func (h *Handler) GetIncomeStatement(w http.ResponseWriter, r *http.Request) {
|
||
|
|
h.Proxy(w, r, "/api/ledger/reports/income")
|
||
|
|
}
|
||
|
|
|
||
|
|
func (h *Handler) GetMomsReport(w http.ResponseWriter, r *http.Request) {
|
||
|
|
h.Proxy(w, r, "/api/ledger/tax/moms")
|
||
|
|
}
|
||
|
|
|
||
|
|
func (h *Handler) GetAccounts(w http.ResponseWriter, r *http.Request) {
|
||
|
|
h.Proxy(w, r, "/api/ledger/accounts")
|
||
|
|
}
|
||
|
|
|
||
|
|
func (h *Handler) GetInvoices(w http.ResponseWriter, r *http.Request) {
|
||
|
|
h.Proxy(w, r, "/api/ledger/invoices")
|
||
|
|
}
|