mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-17 22:55:10 +08:00
feat: centralize utility functions, abstract SSE stream collection, and add concurrency to admin account testing.
This commit is contained in:
37
internal/util/helpers.go
Normal file
37
internal/util/helpers.go
Normal file
@@ -0,0 +1,37 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// WriteJSON writes a JSON response with the given status code.
|
||||
// This is a shared helper to avoid duplicate writeJSON functions
|
||||
// in openai, claude, and admin packages.
|
||||
func WriteJSON(w http.ResponseWriter, status int, payload any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(payload)
|
||||
}
|
||||
|
||||
// ToBool loosely converts an interface value to bool.
|
||||
func ToBool(v any) bool {
|
||||
if b, ok := v.(bool); ok {
|
||||
return b
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IntFrom converts a JSON-decoded numeric value (float64, int, int64) to int.
|
||||
func IntFrom(v any) int {
|
||||
switch n := v.(type) {
|
||||
case float64:
|
||||
return int(n)
|
||||
case int:
|
||||
return n
|
||||
case int64:
|
||||
return int(n)
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user