Files
vessel/backend/internal/api/proxy.go
vikingowl de835b7af7 feat: initial commit - Ollama WebUI with tools, sync, and backend
Complete Ollama Web UI implementation featuring:

Frontend (SvelteKit + Svelte 5 + Tailwind CSS + Skeleton UI):
- Chat interface with streaming responses and markdown rendering
- Message tree with branching support (edit creates branches)
- Vision model support with image upload/paste
- Code syntax highlighting with Shiki
- Built-in tools: get_current_time, calculate, fetch_url
- Function model middleware (functiongemma) for tool routing
- IndexedDB storage with Dexie.js
- Context window tracking with token estimation
- Knowledge base with embeddings (RAG support)
- Keyboard shortcuts and responsive design
- Export conversations as Markdown/JSON

Backend (Go + Gin + SQLite):
- RESTful API for conversations and messages
- SQLite persistence with branching message tree
- Sync endpoints for IndexedDB ↔ SQLite synchronization
- URL proxy endpoint for CORS-bypassed web fetching
- Health check endpoint
- Docker support with host network mode

Infrastructure:
- Docker Compose for development and production
- Vite proxy configuration for Ollama and backend APIs
- Hot reload development setup

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-31 08:11:33 +01:00

93 lines
2.4 KiB
Go

package api
import (
"io"
"net/http"
"net/url"
"time"
"github.com/gin-gonic/gin"
)
// URLFetchRequest represents a request to fetch a URL
type URLFetchRequest struct {
URL string `json:"url" binding:"required"`
MaxLength int `json:"maxLength"`
}
// URLFetchProxyHandler returns a handler that fetches URLs for the frontend
// This bypasses CORS restrictions for the fetch_url tool
func URLFetchProxyHandler() gin.HandlerFunc {
return func(c *gin.Context) {
var req URLFetchRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request: " + err.Error()})
return
}
// Validate URL
parsedURL, err := url.Parse(req.URL)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid URL: " + err.Error()})
return
}
// Only allow HTTP/HTTPS
if parsedURL.Scheme != "http" && parsedURL.Scheme != "https" {
c.JSON(http.StatusBadRequest, gin.H{"error": "only HTTP and HTTPS URLs are supported"})
return
}
// Create HTTP client with timeout
client := &http.Client{
Timeout: 15 * time.Second,
}
// Create request
httpReq, err := http.NewRequestWithContext(c.Request.Context(), "GET", req.URL, nil)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create request: " + err.Error()})
return
}
// Set user agent
httpReq.Header.Set("User-Agent", "OllamaWebUI/1.0 (URL Fetch Proxy)")
httpReq.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
// Execute request
resp, err := client.Do(httpReq)
if err != nil {
c.JSON(http.StatusBadGateway, gin.H{"error": "failed to fetch URL: " + err.Error()})
return
}
defer resp.Body.Close()
// Check status
if resp.StatusCode >= 400 {
c.JSON(http.StatusBadGateway, gin.H{"error": "HTTP " + resp.Status})
return
}
// Set max length (default 500KB)
maxLen := req.MaxLength
if maxLen <= 0 || maxLen > 500000 {
maxLen = 500000
}
// Read response body with limit
body, err := io.ReadAll(io.LimitReader(resp.Body, int64(maxLen)))
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to read response: " + err.Error()})
return
}
// Return the content
c.JSON(http.StatusOK, gin.H{
"content": string(body),
"contentType": resp.Header.Get("Content-Type"),
"url": resp.Request.URL.String(), // Final URL after redirects
"status": resp.StatusCode,
})
}
}