feat: add phone frequency controls and modularize codebase

This commit is contained in:
hectorzhao
2026-07-31 22:25:23 +08:00
parent 0af671b4ed
commit ca4f591a13
216 changed files with 41579 additions and 23694 deletions
+84
View File
@@ -0,0 +1,84 @@
package inbound
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"strings"
"time"
)
func (s Server) post(ctx context.Context, path string, payload any, result any) error {
client := s.HTTPClient
if client == nil {
client = &http.Client{Timeout: defaultHTTPTimeout}
}
body, err := json.Marshal(payload)
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(apiBaseURL(s.APIBaseURL), "/")+path, bytes.NewReader(body))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
responseBody, err := io.ReadAll(io.LimitReader(resp.Body, 64*1024))
if err != nil {
return fmt.Errorf("read api response: %w", err)
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
detail := strings.TrimSpace(string(responseBody))
if detail == "" {
return fmt.Errorf("api returned %s", resp.Status)
}
return fmt.Errorf("api returned %s: %s", resp.Status, detail)
}
if result != nil {
if len(responseBody) == 0 {
return io.EOF
}
return json.Unmarshal(responseBody, result)
}
return nil
}
func apiBaseURL(value string) string {
if value == "" {
return "http://127.0.0.1:3000/api"
}
return value
}
func remoteIP(addr net.Addr) string {
if tcp, ok := addr.(*net.TCPAddr); ok {
return tcp.IP.String()
}
host, _, err := net.SplitHostPort(addr.String())
if err == nil {
return host
}
return addr.String()
}
func defaultString(value string, fallback string) string {
if value == "" {
return fallback
}
return value
}
func formatRFC3339Nano(value time.Time) string {
if value.IsZero() {
return ""
}
return value.UTC().Format(time.RFC3339Nano)
}