Skip to content

Commit

Permalink
Add healthcheck for ollama
Browse files Browse the repository at this point in the history
  • Loading branch information
tanyav2 committed Jan 22, 2025
1 parent ae8160d commit 49d4183
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,33 @@ func corsMiddleware(next http.HandlerFunc) http.HandlerFunc {
}
}

func handleOllamaHealth(w http.ResponseWriter, r *http.Request) {
req, err := http.NewRequestWithContext(r.Context(), http.MethodGet, ollamaURL+"/api/version", nil)
if err != nil {
http.Error(w, fmt.Sprintf("creating version request: %v", err), http.StatusServiceUnavailable)
return
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
http.Error(w, fmt.Sprintf("connecting to Ollama: %v", err), http.StatusServiceUnavailable)
return
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
http.Error(w, fmt.Sprintf("unexpected version status code %d: %s", resp.StatusCode, body), http.StatusBadGateway)
return
}

w.Write([]byte("ollama: "))
// Forward the version response to the client
if _, err := io.Copy(w, resp.Body); err != nil {
log.Printf("Error copying response: %v", err)
}
}

func main() {
mux := http.NewServeMux()

Expand All @@ -179,6 +206,8 @@ func main() {
w.Write([]byte("Content moderation service is running"))
})

mux.HandleFunc("/api/health", corsMiddleware(handleOllamaHealth))

// Analysis endpoint
mux.HandleFunc("/api/analyze", corsMiddleware(handleAnalyze))

Expand Down

0 comments on commit 49d4183

Please sign in to comment.