Add fail-fast on init for missing provider credentials

This commit is contained in:
2026-03-06 21:31:51 +00:00
parent 610b6c3367
commit 89c7e3ac85
14 changed files with 398 additions and 20 deletions

View File

@@ -155,6 +155,11 @@ func main() {
// Register admin endpoints if enabled
if cfg.Admin.Enabled {
// Check if frontend dist exists
if _, err := os.Stat("internal/admin/dist"); os.IsNotExist(err) {
log.Fatalf("admin UI enabled but frontend dist not found")
}
buildInfo := admin.BuildInfo{
Version: "dev",
BuildTime: time.Now().Format(time.RFC3339),
@@ -348,23 +353,39 @@ func initConversationStore(cfg config.ConversationConfig, logger *slog.Logger) (
return conversation.NewMemoryStore(ttl), "memory", nil
}
}
type responseWriter struct {
http.ResponseWriter
statusCode int
bytesWritten int
wroteHeader bool
}
func (rw *responseWriter) WriteHeader(code int) {
if rw.wroteHeader {
return
}
rw.wroteHeader = true
rw.statusCode = code
rw.ResponseWriter.WriteHeader(code)
}
func (rw *responseWriter) Write(b []byte) (int, error) {
if !rw.wroteHeader {
rw.wroteHeader = true
rw.statusCode = http.StatusOK
}
n, err := rw.ResponseWriter.Write(b)
rw.bytesWritten += n
return n, err
}
func (rw *responseWriter) Flush() {
if flusher, ok := rw.ResponseWriter.(http.Flusher); ok {
flusher.Flush()
}
}
func loggingMiddleware(next http.Handler, logger *slog.Logger) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()

57
cmd/gateway/main_test.go Normal file
View File

@@ -0,0 +1,57 @@
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
var _ http.Flusher = (*responseWriter)(nil)
type countingFlusherRecorder struct {
*httptest.ResponseRecorder
flushCount int
}
func newCountingFlusherRecorder() *countingFlusherRecorder {
return &countingFlusherRecorder{ResponseRecorder: httptest.NewRecorder()}
}
func (r *countingFlusherRecorder) Flush() {
r.flushCount++
}
func TestResponseWriterWriteHeaderOnlyOnce(t *testing.T) {
rec := httptest.NewRecorder()
rw := &responseWriter{ResponseWriter: rec, statusCode: http.StatusOK}
rw.WriteHeader(http.StatusCreated)
rw.WriteHeader(http.StatusInternalServerError)
assert.Equal(t, http.StatusCreated, rec.Code)
assert.Equal(t, http.StatusCreated, rw.statusCode)
}
func TestResponseWriterWriteSetsImplicitStatus(t *testing.T) {
rec := httptest.NewRecorder()
rw := &responseWriter{ResponseWriter: rec, statusCode: http.StatusOK}
n, err := rw.Write([]byte("ok"))
assert.NoError(t, err)
assert.Equal(t, 2, n)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, http.StatusOK, rw.statusCode)
assert.Equal(t, 2, rw.bytesWritten)
}
func TestResponseWriterFlushDelegates(t *testing.T) {
rec := newCountingFlusherRecorder()
rw := &responseWriter{ResponseWriter: rec, statusCode: http.StatusOK}
rw.Flush()
assert.Equal(t, 1, rec.flushCount)
}