fix: Resolve CORS issue for Ollama API access

- Add script to start Ollama with CORS enabled
- Add proxy server option for environments without CORS support
- Document CORS configuration in scripts

Note: Run './scripts/start-ollama-cors.sh' or set OLLAMA_ORIGINS='*' when starting Ollama

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

Co-Authored-By: Claude <[email protected]>
This commit is contained in:
BrunoGao
2025-09-30 20:31:18 +08:00
co-authored by Claude
parent 50cb7e5aa3
commit 8cff90f7a0
2 changed files with 70 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
#!/bin/bash
# 停止现有的Ollama服务
echo "停止现有的Ollama服务..."
pkill ollama || true
sleep 2
# 设置环境变量以允许CORS
export OLLAMA_ORIGINS="*"
export OLLAMA_HOST="0.0.0.0:11434"
echo "启动Ollama服务(允许CORS)..."
ollama serve &
# 等待服务启动
sleep 3
# 检查服务状态
if curl -s http://localhost:11434/api/tags > /dev/null; then
echo "✅ Ollama服务已启动,CORS已启用"
echo " 可以从浏览器访问API"
else
echo "❌ Ollama服务启动失败"
exit 1
fi
echo ""
echo "提示:"
echo "- 服务正在后台运行"
echo "- 要停止服务,运行: pkill ollama"
echo "- 要查看日志,运行: ollama serve(前台模式)"
+39
View File
@@ -0,0 +1,39 @@
import express from 'express';
import cors from 'cors';
import fetch from 'node-fetch';
const app = express();
const PORT = 3001;
// Enable CORS for all origins
app.use(cors());
app.use(express.json());
// Proxy endpoint for Ollama API
app.post('/api/chat', async (req, res) => {
try {
const response = await fetch('http://localhost:11434/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(req.body),
});
const data = await response.json();
res.json(data);
} catch (error) {
console.error('Proxy error:', error);
res.status(500).json({ error: 'Failed to connect to Ollama service' });
}
});
// Health check endpoint
app.get('/health', (req, res) => {
res.json({ status: 'ok', message: 'Proxy server is running' });
});
app.listen(PORT, () => {
console.log(`🚀 Proxy server running on http://localhost:${PORT}`);
console.log(` Proxying Ollama requests from :${PORT} to :11434`);
});