mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 16:04:01 +00:00
Move the command to cmd/opencode2api and group production code into configuration, gateway, protocol, model, admin, telemetry and helper packages. Keep shared state private behind context, metrics and runtime interfaces. Remove the requested test files and replace test-only CI jobs with vet/build checks. Update Docker and release build paths, preserve version injection and make the WebUI an explicit embedded asset package. Add directory trees and source reading guides to both READMEs. Anchor binary ignore rules so the command package is tracked.
34 lines
751 B
Go
34 lines
751 B
Go
// Package httpx provides shared HTTP response and transport helpers.
|
|
package httpx
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
func WriteJSON(w http.ResponseWriter, status int, value any) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
WriteJSONStatus(w, status, value)
|
|
}
|
|
|
|
func WriteJSONStatus(w http.ResponseWriter, status int, value any) {
|
|
w.WriteHeader(status)
|
|
_ = json.NewEncoder(w).Encode(value)
|
|
}
|
|
|
|
func WriteSSE(w http.ResponseWriter, event string, id uint64, value any) error {
|
|
data, err := json.Marshal(value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if id > 0 {
|
|
_, _ = fmt.Fprintf(w, "id: %d\n", id)
|
|
}
|
|
if event != "" {
|
|
_, _ = fmt.Fprintf(w, "event: %s\n", event)
|
|
}
|
|
_, err = fmt.Fprintf(w, "data: %s\n\n", data)
|
|
return err
|
|
}
|