This commit is contained in:
293
src/commands/auth.rs
Normal file
293
src/commands/auth.rs
Normal file
@@ -0,0 +1,293 @@
|
||||
use anyhow::{Result, Context};
|
||||
use colored::Colorize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct AuthConfig {
|
||||
pub admin: AdminConfig,
|
||||
pub jetstream: JetstreamConfig,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct AdminConfig {
|
||||
pub did: String,
|
||||
pub handle: String,
|
||||
pub access_jwt: String,
|
||||
pub refresh_jwt: String,
|
||||
pub pds: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct JetstreamConfig {
|
||||
pub url: String,
|
||||
pub collections: Vec<String>,
|
||||
}
|
||||
|
||||
impl Default for AuthConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
admin: AdminConfig {
|
||||
did: String::new(),
|
||||
handle: String::new(),
|
||||
access_jwt: String::new(),
|
||||
refresh_jwt: String::new(),
|
||||
pds: "https://bsky.social".to_string(),
|
||||
},
|
||||
jetstream: JetstreamConfig {
|
||||
url: "wss://jetstream2.us-east.bsky.network/subscribe".to_string(),
|
||||
collections: vec!["ai.syui.log".to_string()],
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_config_path() -> Result<PathBuf> {
|
||||
let home = std::env::var("HOME").context("HOME environment variable not set")?;
|
||||
let config_dir = PathBuf::from(home).join(".config").join("syui").join("ai").join("log");
|
||||
|
||||
// Create directory if it doesn't exist
|
||||
fs::create_dir_all(&config_dir)?;
|
||||
|
||||
Ok(config_dir.join("config.json"))
|
||||
}
|
||||
|
||||
pub async fn init() -> Result<()> {
|
||||
println!("{}", "🔐 Initializing ATProto authentication...".cyan());
|
||||
|
||||
let config_path = get_config_path()?;
|
||||
|
||||
if config_path.exists() {
|
||||
println!("{}", "⚠️ Configuration already exists. Use 'ailog auth logout' to reset.".yellow());
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
println!("{}", "📋 Please provide your ATProto credentials:".cyan());
|
||||
|
||||
// Get user input
|
||||
print!("Handle (e.g., your.handle.bsky.social): ");
|
||||
std::io::Write::flush(&mut std::io::stdout())?;
|
||||
let mut handle = String::new();
|
||||
std::io::stdin().read_line(&mut handle)?;
|
||||
let handle = handle.trim().to_string();
|
||||
|
||||
print!("Access JWT: ");
|
||||
std::io::Write::flush(&mut std::io::stdout())?;
|
||||
let mut access_jwt = String::new();
|
||||
std::io::stdin().read_line(&mut access_jwt)?;
|
||||
let access_jwt = access_jwt.trim().to_string();
|
||||
|
||||
print!("Refresh JWT: ");
|
||||
std::io::Write::flush(&mut std::io::stdout())?;
|
||||
let mut refresh_jwt = String::new();
|
||||
std::io::stdin().read_line(&mut refresh_jwt)?;
|
||||
let refresh_jwt = refresh_jwt.trim().to_string();
|
||||
|
||||
// Resolve DID from handle
|
||||
println!("{}", "🔍 Resolving DID from handle...".cyan());
|
||||
let did = resolve_did(&handle).await?;
|
||||
|
||||
// Create config
|
||||
let config = AuthConfig {
|
||||
admin: AdminConfig {
|
||||
did: did.clone(),
|
||||
handle: handle.clone(),
|
||||
access_jwt,
|
||||
refresh_jwt,
|
||||
pds: if handle.ends_with(".syu.is") {
|
||||
"https://syu.is".to_string()
|
||||
} else {
|
||||
"https://bsky.social".to_string()
|
||||
},
|
||||
},
|
||||
jetstream: JetstreamConfig {
|
||||
url: "wss://jetstream2.us-east.bsky.network/subscribe".to_string(),
|
||||
collections: vec!["ai.syui.log".to_string()],
|
||||
},
|
||||
};
|
||||
|
||||
// Save config
|
||||
let config_json = serde_json::to_string_pretty(&config)?;
|
||||
fs::write(&config_path, config_json)?;
|
||||
|
||||
println!("{}", "✅ Authentication configured successfully!".green());
|
||||
println!("📁 Config saved to: {}", config_path.display());
|
||||
println!("👤 Authenticated as: {} ({})", handle, did);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn resolve_did(handle: &str) -> Result<String> {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("https://public.api.bsky.app/xrpc/app.bsky.actor.getProfile?actor={}",
|
||||
urlencoding::encode(handle));
|
||||
|
||||
let response = client.get(&url).send().await?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(anyhow::anyhow!("Failed to resolve handle: {}", response.status()));
|
||||
}
|
||||
|
||||
let profile: serde_json::Value = response.json().await?;
|
||||
let did = profile["did"].as_str()
|
||||
.ok_or_else(|| anyhow::anyhow!("DID not found in profile response"))?;
|
||||
|
||||
Ok(did.to_string())
|
||||
}
|
||||
|
||||
pub async fn status() -> Result<()> {
|
||||
let config_path = get_config_path()?;
|
||||
|
||||
if !config_path.exists() {
|
||||
println!("{}", "❌ Not authenticated. Run 'ailog auth init' first.".red());
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let config_json = fs::read_to_string(&config_path)?;
|
||||
let config: AuthConfig = serde_json::from_str(&config_json)?;
|
||||
|
||||
println!("{}", "🔐 Authentication Status".cyan().bold());
|
||||
println!("─────────────────────────");
|
||||
println!("📁 Config: {}", config_path.display());
|
||||
println!("👤 Handle: {}", config.admin.handle.green());
|
||||
println!("🆔 DID: {}", config.admin.did);
|
||||
println!("🌐 PDS: {}", config.admin.pds);
|
||||
println!("📡 Jetstream: {}", config.jetstream.url);
|
||||
println!("📂 Collections: {}", config.jetstream.collections.join(", "));
|
||||
|
||||
// Test API access
|
||||
println!("\n{}", "🧪 Testing API access...".cyan());
|
||||
match test_api_access(&config).await {
|
||||
Ok(_) => println!("{}", "✅ API access successful".green()),
|
||||
Err(e) => println!("{}", format!("❌ API access failed: {}", e).red()),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn test_api_access(config: &AuthConfig) -> Result<()> {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("https://public.api.bsky.app/xrpc/app.bsky.actor.getProfile?actor={}",
|
||||
urlencoding::encode(&config.admin.handle));
|
||||
|
||||
let response = client.get(&url).send().await?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(anyhow::anyhow!("API request failed: {}", response.status()));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn logout() -> Result<()> {
|
||||
let config_path = get_config_path()?;
|
||||
|
||||
if !config_path.exists() {
|
||||
println!("{}", "ℹ️ Already logged out.".blue());
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
println!("{}", "🔓 Logging out...".cyan());
|
||||
|
||||
// Remove config file
|
||||
fs::remove_file(&config_path)?;
|
||||
|
||||
println!("{}", "✅ Logged out successfully!".green());
|
||||
println!("🗑️ Configuration removed from: {}", config_path.display());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Load config helper function for other modules
|
||||
pub fn load_config() -> Result<AuthConfig> {
|
||||
let config_path = get_config_path()?;
|
||||
|
||||
if !config_path.exists() {
|
||||
return Err(anyhow::anyhow!("Not authenticated. Run 'ailog auth init' first."));
|
||||
}
|
||||
|
||||
let config_json = fs::read_to_string(&config_path)?;
|
||||
let config: AuthConfig = serde_json::from_str(&config_json)?;
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
// Load config with automatic token refresh
|
||||
pub async fn load_config_with_refresh() -> Result<AuthConfig> {
|
||||
let mut config = load_config()?;
|
||||
|
||||
// Test if current access token is still valid
|
||||
if let Err(_) = test_api_access_with_auth(&config).await {
|
||||
println!("{}", "🔄 Access token expired, refreshing...".yellow());
|
||||
|
||||
// Try to refresh the token
|
||||
match refresh_access_token(&mut config).await {
|
||||
Ok(_) => {
|
||||
save_config(&config)?;
|
||||
println!("{}", "✅ Token refreshed successfully".green());
|
||||
}
|
||||
Err(e) => {
|
||||
return Err(anyhow::anyhow!("Failed to refresh token: {}. Please run 'ailog auth init' again.", e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
async fn test_api_access_with_auth(config: &AuthConfig) -> Result<()> {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("{}/xrpc/com.atproto.repo.listRecords?repo={}&collection=ai.syui.log&limit=1",
|
||||
config.admin.pds,
|
||||
urlencoding::encode(&config.admin.did));
|
||||
|
||||
let response = client
|
||||
.get(&url)
|
||||
.header("Authorization", format!("Bearer {}", config.admin.access_jwt))
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(anyhow::anyhow!("API request failed: {}", response.status()));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn refresh_access_token(config: &mut AuthConfig) -> Result<()> {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("{}/xrpc/com.atproto.server.refreshSession", config.admin.pds);
|
||||
|
||||
let response = client
|
||||
.post(&url)
|
||||
.header("Authorization", format!("Bearer {}", config.admin.refresh_jwt))
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
let status = response.status();
|
||||
let error_text = response.text().await?;
|
||||
return Err(anyhow::anyhow!("Token refresh failed: {} - {}", status, error_text));
|
||||
}
|
||||
|
||||
let refresh_response: serde_json::Value = response.json().await?;
|
||||
|
||||
// Update tokens
|
||||
if let Some(access_jwt) = refresh_response["accessJwt"].as_str() {
|
||||
config.admin.access_jwt = access_jwt.to_string();
|
||||
}
|
||||
|
||||
if let Some(refresh_jwt) = refresh_response["refreshJwt"].as_str() {
|
||||
config.admin.refresh_jwt = refresh_jwt.to_string();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn save_config(config: &AuthConfig) -> Result<()> {
|
||||
let config_path = get_config_path()?;
|
||||
let config_json = serde_json::to_string_pretty(config)?;
|
||||
fs::write(&config_path, config_json)?;
|
||||
Ok(())
|
||||
}
|
@@ -44,7 +44,7 @@ comment_moderation = false
|
||||
fs::write(path.join("config.toml"), config_content)?;
|
||||
println!(" {} config.toml", "Created".cyan());
|
||||
|
||||
// Create default template
|
||||
// Create modern template
|
||||
let base_template = r#"<!DOCTYPE html>
|
||||
<html lang="{{ config.language }}">
|
||||
<head>
|
||||
@@ -54,18 +54,83 @@ comment_moderation = false
|
||||
<link rel="stylesheet" href="/css/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1><a href="/">{{ config.title }}</a></h1>
|
||||
<p>{{ config.description }}</p>
|
||||
</header>
|
||||
<div class="container">
|
||||
<header class="main-header">
|
||||
<div class="header-content">
|
||||
<h1><a href="/" class="site-title">{{ config.title }}</a></h1>
|
||||
<div class="header-actions">
|
||||
<button class="ask-ai-btn" onclick="toggleAskAI()">
|
||||
<span class="ai-icon">🤖</span>
|
||||
Ask AI
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div class="ask-ai-panel" id="askAiPanel" style="display: none;">
|
||||
<div class="ask-ai-content">
|
||||
<h3>Hi! 👋</h3>
|
||||
<p>I'm an AI assistant trained on this blog's content.</p>
|
||||
<p>Ask me anything about the articles here.</p>
|
||||
<div class="ask-ai-form">
|
||||
<input type="text" id="aiQuestion" placeholder="What would you like to know?" />
|
||||
<button onclick="askQuestion()">Ask</button>
|
||||
</div>
|
||||
<div id="aiResponse" class="ai-response"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<main class="main-content">
|
||||
{% block content %}{% endblock %}
|
||||
</main>
|
||||
|
||||
{% block sidebar %}{% endblock %}
|
||||
</div>
|
||||
|
||||
<main>
|
||||
{% block content %}{% endblock %}
|
||||
</main>
|
||||
|
||||
<footer>
|
||||
<footer class="main-footer">
|
||||
<p>© 2025 {{ config.title }}</p>
|
||||
</footer>
|
||||
|
||||
<script>
|
||||
function toggleAskAI() {
|
||||
const panel = document.getElementById('askAiPanel');
|
||||
const isVisible = panel.style.display !== 'none';
|
||||
panel.style.display = isVisible ? 'none' : 'block';
|
||||
if (!isVisible) {
|
||||
document.getElementById('aiQuestion').focus();
|
||||
}
|
||||
}
|
||||
|
||||
async function askQuestion() {
|
||||
const question = document.getElementById('aiQuestion').value;
|
||||
const responseDiv = document.getElementById('aiResponse');
|
||||
|
||||
if (!question.trim()) return;
|
||||
|
||||
responseDiv.innerHTML = '<div class="loading">Thinking...</div>';
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/ask', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ question: question })
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
responseDiv.innerHTML = `<div class="ai-answer">${data.answer}</div>`;
|
||||
} catch (error) {
|
||||
responseDiv.innerHTML = '<div class="error">Sorry, I encountered an error. Please try again.</div>';
|
||||
}
|
||||
}
|
||||
|
||||
document.addEventListener('keydown', function(e) {
|
||||
if (e.key === 'Escape') {
|
||||
document.getElementById('askAiPanel').style.display = 'none';
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>"#;
|
||||
|
||||
@@ -75,15 +140,52 @@ comment_moderation = false
|
||||
let index_template = r#"{% extends "base.html" %}
|
||||
|
||||
{% block content %}
|
||||
<h2>Recent Posts</h2>
|
||||
<ul class="post-list">
|
||||
{% for post in posts %}
|
||||
<li>
|
||||
<a href="{{ post.url }}">{{ post.title }}</a>
|
||||
<time>{{ post.date }}</time>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
<div class="timeline-container">
|
||||
<div class="timeline-header">
|
||||
<h2>Timeline</h2>
|
||||
</div>
|
||||
|
||||
<div class="timeline-feed">
|
||||
{% for post in posts %}
|
||||
<article class="timeline-post">
|
||||
<div class="post-header">
|
||||
<div class="post-meta">
|
||||
<time class="post-date">{{ post.date }}</time>
|
||||
{% if post.language %}
|
||||
<span class="post-lang">{{ post.language }}</span>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="post-content">
|
||||
<h3 class="post-title">
|
||||
<a href="{{ post.url }}">{{ post.title }}</a>
|
||||
</h3>
|
||||
|
||||
{% if post.excerpt %}
|
||||
<p class="post-excerpt">{{ post.excerpt }}</p>
|
||||
{% endif %}
|
||||
|
||||
<div class="post-actions">
|
||||
<a href="{{ post.url }}" class="read-more">Read more</a>
|
||||
{% if post.markdown_url %}
|
||||
<a href="{{ post.markdown_url }}" class="view-markdown" title="View Markdown">📝</a>
|
||||
{% endif %}
|
||||
{% if post.translation_url %}
|
||||
<a href="{{ post.translation_url }}" class="view-translation" title="View Translation">🌐</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
</article>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
{% if posts|length == 0 %}
|
||||
<div class="empty-state">
|
||||
<p>No posts yet. Start writing!</p>
|
||||
</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endblock %}"#;
|
||||
|
||||
fs::write(path.join("templates/index.html"), index_template)?;
|
||||
@@ -94,76 +196,624 @@ comment_moderation = false
|
||||
{% block title %}{{ post.title }} - {{ config.title }}{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<article>
|
||||
<h1>{{ post.title }}</h1>
|
||||
<time>{{ post.date }}</time>
|
||||
<div class="content">
|
||||
{{ post.content | safe }}
|
||||
</div>
|
||||
</article>
|
||||
<div class="article-container">
|
||||
<article class="article-content">
|
||||
<header class="article-header">
|
||||
<h1 class="article-title">{{ post.title }}</h1>
|
||||
<div class="article-meta">
|
||||
<time class="article-date">{{ post.date }}</time>
|
||||
{% if post.language %}
|
||||
<span class="article-lang">{{ post.language }}</span>
|
||||
{% endif %}
|
||||
</div>
|
||||
<div class="article-actions">
|
||||
{% if post.markdown_url %}
|
||||
<a href="{{ post.markdown_url }}" class="action-btn markdown-btn" title="View Markdown">
|
||||
📝 Markdown
|
||||
</a>
|
||||
{% endif %}
|
||||
{% if post.translation_url %}
|
||||
<a href="{{ post.translation_url }}" class="action-btn translation-btn" title="View Translation">
|
||||
🌐 {% if post.language == 'ja' %}English{% else %}日本語{% endif %}
|
||||
</a>
|
||||
{% endif %}
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div class="article-body">
|
||||
{{ post.content | safe }}
|
||||
</div>
|
||||
</article>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
||||
{% block sidebar %}
|
||||
<aside class="article-sidebar">
|
||||
<nav class="toc">
|
||||
<h3>Contents</h3>
|
||||
<div id="toc-content">
|
||||
<!-- TOC will be generated by JavaScript -->
|
||||
</div>
|
||||
</nav>
|
||||
</aside>
|
||||
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
generateTableOfContents();
|
||||
});
|
||||
|
||||
function generateTableOfContents() {
|
||||
const tocContainer = document.getElementById('toc-content');
|
||||
const headings = document.querySelectorAll('.article-body h1, .article-body h2, .article-body h3, .article-body h4, .article-body h5, .article-body h6');
|
||||
|
||||
if (headings.length === 0) {
|
||||
tocContainer.innerHTML = '<p class="no-toc">No headings found</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
const tocList = document.createElement('ul');
|
||||
tocList.className = 'toc-list';
|
||||
|
||||
headings.forEach((heading, index) => {
|
||||
const id = `heading-${index}`;
|
||||
heading.id = id;
|
||||
|
||||
const listItem = document.createElement('li');
|
||||
listItem.className = `toc-item toc-${heading.tagName.toLowerCase()}`;
|
||||
|
||||
const link = document.createElement('a');
|
||||
link.href = `#${id}`;
|
||||
link.textContent = heading.textContent;
|
||||
link.className = 'toc-link';
|
||||
|
||||
// Smooth scroll behavior
|
||||
link.addEventListener('click', function(e) {
|
||||
e.preventDefault();
|
||||
heading.scrollIntoView({ behavior: 'smooth' });
|
||||
});
|
||||
|
||||
listItem.appendChild(link);
|
||||
tocList.appendChild(listItem);
|
||||
});
|
||||
|
||||
tocContainer.appendChild(tocList);
|
||||
}
|
||||
</script>
|
||||
{% endblock %}"#;
|
||||
|
||||
fs::write(path.join("templates/post.html"), post_template)?;
|
||||
println!(" {} templates/post.html", "Created".cyan());
|
||||
|
||||
// Create default CSS
|
||||
let css_content = r#"body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
||||
line-height: 1.6;
|
||||
color: #333;
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
header {
|
||||
margin-bottom: 40px;
|
||||
border-bottom: 1px solid #eee;
|
||||
padding-bottom: 20px;
|
||||
}
|
||||
|
||||
header h1 {
|
||||
// Create modern CSS
|
||||
let css_content = r#"/* Base styles */
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
header h1 a {
|
||||
color: #333;
|
||||
body {
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
|
||||
line-height: 1.6;
|
||||
color: #1f2328;
|
||||
background-color: #ffffff;
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
.container {
|
||||
min-height: 100vh;
|
||||
display: grid;
|
||||
grid-template-rows: auto auto 1fr auto;
|
||||
grid-template-areas:
|
||||
"header"
|
||||
"ask-ai"
|
||||
"main"
|
||||
"footer";
|
||||
}
|
||||
|
||||
/* Header styles */
|
||||
.main-header {
|
||||
grid-area: header;
|
||||
background: #ffffff;
|
||||
border-bottom: 1px solid #d1d9e0;
|
||||
padding: 16px 24px;
|
||||
position: sticky;
|
||||
top: 0;
|
||||
z-index: 100;
|
||||
}
|
||||
|
||||
.header-content {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.site-title {
|
||||
color: #1f2328;
|
||||
text-decoration: none;
|
||||
font-size: 20px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.post-list {
|
||||
list-style: none;
|
||||
.site-title:hover {
|
||||
color: #0969da;
|
||||
}
|
||||
|
||||
/* Ask AI styles */
|
||||
.ask-ai-btn {
|
||||
background: #0969da;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 8px 16px;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
transition: background-color 0.2s;
|
||||
}
|
||||
|
||||
.ask-ai-btn:hover {
|
||||
background: #0860ca;
|
||||
}
|
||||
|
||||
.ai-icon {
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
.ask-ai-panel {
|
||||
grid-area: ask-ai;
|
||||
background: #f6f8fa;
|
||||
border-bottom: 1px solid #d1d9e0;
|
||||
padding: 24px;
|
||||
}
|
||||
|
||||
.ask-ai-content {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.ask-ai-content h3 {
|
||||
color: #1f2328;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.ask-ai-content p {
|
||||
color: #656d76;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.ask-ai-form {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.ask-ai-form input {
|
||||
flex: 1;
|
||||
padding: 8px 12px;
|
||||
border: 1px solid #d1d9e0;
|
||||
border-radius: 6px;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.ask-ai-form button {
|
||||
background: #0969da;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 8px 16px;
|
||||
border-radius: 6px;
|
||||
cursor: pointer;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.ai-response {
|
||||
background: white;
|
||||
border: 1px solid #d1d9e0;
|
||||
border-radius: 6px;
|
||||
padding: 16px;
|
||||
margin-top: 16px;
|
||||
min-height: 60px;
|
||||
}
|
||||
|
||||
.loading {
|
||||
color: #656d76;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.ai-answer {
|
||||
color: #1f2328;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: #d1242f;
|
||||
}
|
||||
|
||||
/* Main content styles */
|
||||
.main-content {
|
||||
grid-area: main;
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
padding: 24px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
/* Timeline styles */
|
||||
.timeline-container {
|
||||
max-width: 600px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.timeline-header {
|
||||
margin-bottom: 24px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.timeline-header h2 {
|
||||
color: #1f2328;
|
||||
font-size: 24px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.timeline-feed {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 24px;
|
||||
}
|
||||
|
||||
.timeline-post {
|
||||
background: #ffffff;
|
||||
border: 1px solid #d1d9e0;
|
||||
border-radius: 8px;
|
||||
padding: 20px;
|
||||
transition: box-shadow 0.2s;
|
||||
}
|
||||
|
||||
.timeline-post:hover {
|
||||
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.post-header {
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.post-meta {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.post-date {
|
||||
color: #656d76;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.post-lang {
|
||||
background: #f6f8fa;
|
||||
color: #656d76;
|
||||
padding: 2px 8px;
|
||||
border-radius: 4px;
|
||||
font-size: 12px;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.post-title {
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.post-title a {
|
||||
color: #1f2328;
|
||||
text-decoration: none;
|
||||
font-size: 18px;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.post-title a:hover {
|
||||
color: #0969da;
|
||||
}
|
||||
|
||||
.post-excerpt {
|
||||
color: #656d76;
|
||||
margin-bottom: 16px;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.post-actions {
|
||||
display: flex;
|
||||
gap: 16px;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.read-more {
|
||||
color: #0969da;
|
||||
text-decoration: none;
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.read-more:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
|
||||
.view-markdown, .view-translation {
|
||||
color: #656d76;
|
||||
text-decoration: none;
|
||||
font-size: 14px;
|
||||
padding: 4px 8px;
|
||||
border-radius: 4px;
|
||||
transition: background-color 0.2s;
|
||||
}
|
||||
|
||||
.view-markdown:hover, .view-translation:hover {
|
||||
background: #f6f8fa;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
text-align: center;
|
||||
padding: 40px 20px;
|
||||
color: #656d76;
|
||||
}
|
||||
|
||||
/* Article page styles */
|
||||
.article-container {
|
||||
display: grid;
|
||||
grid-template-columns: 1fr 240px;
|
||||
gap: 40px;
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.article-content {
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.article-header {
|
||||
margin-bottom: 32px;
|
||||
padding-bottom: 24px;
|
||||
border-bottom: 1px solid #d1d9e0;
|
||||
}
|
||||
|
||||
.article-title {
|
||||
color: #1f2328;
|
||||
font-size: 32px;
|
||||
font-weight: 600;
|
||||
margin-bottom: 16px;
|
||||
line-height: 1.25;
|
||||
}
|
||||
|
||||
.article-meta {
|
||||
display: flex;
|
||||
gap: 16px;
|
||||
align-items: center;
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.article-date {
|
||||
color: #656d76;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.article-lang {
|
||||
background: #f6f8fa;
|
||||
color: #656d76;
|
||||
padding: 4px 8px;
|
||||
border-radius: 4px;
|
||||
font-size: 12px;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.article-actions {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.action-btn {
|
||||
color: #0969da;
|
||||
text-decoration: none;
|
||||
font-size: 14px;
|
||||
padding: 6px 12px;
|
||||
border: 1px solid #d1d9e0;
|
||||
border-radius: 6px;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.action-btn:hover {
|
||||
background: #f6f8fa;
|
||||
border-color: #0969da;
|
||||
}
|
||||
|
||||
/* Article content */
|
||||
.article-body {
|
||||
color: #1f2328;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.article-body h1,
|
||||
.article-body h2,
|
||||
.article-body h3,
|
||||
.article-body h4,
|
||||
.article-body h5,
|
||||
.article-body h6 {
|
||||
color: #1f2328;
|
||||
margin-top: 24px;
|
||||
margin-bottom: 16px;
|
||||
font-weight: 600;
|
||||
line-height: 1.25;
|
||||
}
|
||||
|
||||
.article-body h1 { font-size: 32px; }
|
||||
.article-body h2 { font-size: 24px; }
|
||||
.article-body h3 { font-size: 20px; }
|
||||
.article-body h4 { font-size: 16px; }
|
||||
|
||||
.article-body p {
|
||||
margin-bottom: 16px;
|
||||
}
|
||||
|
||||
.article-body ul,
|
||||
.article-body ol {
|
||||
margin-bottom: 16px;
|
||||
padding-left: 24px;
|
||||
}
|
||||
|
||||
.article-body li {
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
.article-body blockquote {
|
||||
border-left: 4px solid #d1d9e0;
|
||||
padding-left: 16px;
|
||||
margin: 16px 0;
|
||||
color: #656d76;
|
||||
}
|
||||
|
||||
.article-body pre {
|
||||
background: #f6f8fa;
|
||||
border: 1px solid #d1d9e0;
|
||||
border-radius: 6px;
|
||||
padding: 16px;
|
||||
overflow-x: auto;
|
||||
margin: 16px 0;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.article-body code {
|
||||
background: #f6f8fa;
|
||||
padding: 2px 4px;
|
||||
border-radius: 4px;
|
||||
font-family: 'SF Mono', 'Monaco', 'Cascadia Code', 'Roboto Mono', monospace;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.article-body pre code {
|
||||
background: none;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.post-list li {
|
||||
margin-bottom: 15px;
|
||||
/* Sidebar styles */
|
||||
.article-sidebar {
|
||||
position: sticky;
|
||||
top: 100px;
|
||||
height: fit-content;
|
||||
}
|
||||
|
||||
.post-list time {
|
||||
color: #666;
|
||||
font-size: 0.9em;
|
||||
margin-left: 10px;
|
||||
.toc {
|
||||
background: #f6f8fa;
|
||||
border: 1px solid #d1d9e0;
|
||||
border-radius: 8px;
|
||||
padding: 16px;
|
||||
}
|
||||
|
||||
article time {
|
||||
color: #666;
|
||||
.toc h3 {
|
||||
color: #1f2328;
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.toc-list {
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
.toc-item {
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.toc-link {
|
||||
color: #656d76;
|
||||
text-decoration: none;
|
||||
font-size: 14px;
|
||||
line-height: 1.4;
|
||||
display: block;
|
||||
margin-bottom: 20px;
|
||||
padding: 4px 0;
|
||||
transition: color 0.2s;
|
||||
}
|
||||
|
||||
pre {
|
||||
background-color: #f4f4f4;
|
||||
padding: 15px;
|
||||
border-radius: 5px;
|
||||
overflow-x: auto;
|
||||
.toc-link:hover {
|
||||
color: #0969da;
|
||||
}
|
||||
|
||||
code {
|
||||
background-color: #f4f4f4;
|
||||
padding: 2px 5px;
|
||||
border-radius: 3px;
|
||||
font-family: 'Consolas', 'Monaco', monospace;
|
||||
.toc-h1 { padding-left: 0; }
|
||||
.toc-h2 { padding-left: 12px; }
|
||||
.toc-h3 { padding-left: 24px; }
|
||||
.toc-h4 { padding-left: 36px; }
|
||||
.toc-h5 { padding-left: 48px; }
|
||||
.toc-h6 { padding-left: 60px; }
|
||||
|
||||
.no-toc {
|
||||
color: #656d76;
|
||||
font-size: 14px;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
/* Footer styles */
|
||||
.main-footer {
|
||||
grid-area: footer;
|
||||
background: #f6f8fa;
|
||||
border-top: 1px solid #d1d9e0;
|
||||
padding: 24px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.main-footer p {
|
||||
color: #656d76;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
/* Responsive design */
|
||||
@media (max-width: 1024px) {
|
||||
.article-container {
|
||||
grid-template-columns: 1fr;
|
||||
gap: 24px;
|
||||
}
|
||||
|
||||
.article-sidebar {
|
||||
position: static;
|
||||
order: -1;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.main-header {
|
||||
padding: 12px 16px;
|
||||
}
|
||||
|
||||
.header-content {
|
||||
gap: 16px;
|
||||
}
|
||||
|
||||
.ask-ai-panel {
|
||||
padding: 16px;
|
||||
}
|
||||
|
||||
.ask-ai-form {
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.timeline-container {
|
||||
max-width: 100%;
|
||||
}
|
||||
|
||||
.timeline-post {
|
||||
padding: 16px;
|
||||
}
|
||||
|
||||
.article-title {
|
||||
font-size: 24px;
|
||||
}
|
||||
|
||||
.article-actions {
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.main-content {
|
||||
padding: 16px;
|
||||
}
|
||||
}"#;
|
||||
|
||||
fs::write(path.join("static/css/style.css"), css_content)?;
|
||||
@@ -208,9 +858,14 @@ Happy blogging!"#;
|
||||
|
||||
println!("\n{}", "Blog initialized successfully!".green().bold());
|
||||
println!("\nNext steps:");
|
||||
println!(" 1. cd {}", path.display());
|
||||
println!(" 2. ailog build");
|
||||
println!(" 3. ailog serve");
|
||||
println!(" 1. {} {}", "cd".yellow(), path.display());
|
||||
println!(" 2. {} build", "ailog".yellow());
|
||||
println!(" 3. {} serve", "ailog".yellow());
|
||||
println!("\nOr use path as argument:");
|
||||
println!(" {} -- build {}", "cargo run".yellow(), path.display());
|
||||
println!(" {} -- serve {}", "cargo run".yellow(), path.display());
|
||||
println!("\nTo create a new post:");
|
||||
println!(" {} -- new \"Post Title\" {}", "cargo run".yellow(), path.display());
|
||||
|
||||
Ok(())
|
||||
}
|
@@ -3,4 +3,6 @@ pub mod build;
|
||||
pub mod new;
|
||||
pub mod serve;
|
||||
pub mod clean;
|
||||
pub mod doc;
|
||||
pub mod doc;
|
||||
pub mod auth;
|
||||
pub mod stream;
|
@@ -5,6 +5,12 @@ use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
|
||||
pub async fn execute(port: u16) -> Result<()> {
|
||||
// Check if public directory exists
|
||||
if !std::path::Path::new("public").exists() {
|
||||
println!("{}", "No public directory found. Running build first...".yellow());
|
||||
crate::commands::build::execute(std::path::PathBuf::from(".")).await?;
|
||||
}
|
||||
|
||||
let addr = format!("127.0.0.1:{}", port);
|
||||
let listener = TcpListener::bind(&addr).await?;
|
||||
|
||||
@@ -19,59 +25,327 @@ pub async fn execute(port: u16) -> Result<()> {
|
||||
}
|
||||
|
||||
async fn handle_connection(mut stream: TcpStream) -> Result<()> {
|
||||
let mut buffer = [0; 1024];
|
||||
stream.read(&mut buffer).await?;
|
||||
|
||||
let request = String::from_utf8_lossy(&buffer[..]);
|
||||
let path = parse_request_path(&request);
|
||||
|
||||
let (status, content_type, content) = match serve_file(&path).await {
|
||||
Ok((ct, data)) => ("200 OK", ct, data),
|
||||
Err(_) => ("404 NOT FOUND", "text/html", b"<h1>404 - Not Found</h1>".to_vec()),
|
||||
// Read request with timeout and proper buffering
|
||||
let mut buffer = [0; 4096];
|
||||
let bytes_read = match tokio::time::timeout(
|
||||
tokio::time::Duration::from_secs(5),
|
||||
stream.read(&mut buffer)
|
||||
).await {
|
||||
Ok(Ok(n)) => n,
|
||||
Ok(Err(_)) => return Ok(()),
|
||||
Err(_) => {
|
||||
eprintln!("Request timeout");
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
let response = format!(
|
||||
"HTTP/1.1 {}\r\nContent-Type: {}\r\nContent-Length: {}\r\n\r\n",
|
||||
status,
|
||||
content_type,
|
||||
content.len()
|
||||
if bytes_read == 0 {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let request = String::from_utf8_lossy(&buffer[..bytes_read]);
|
||||
let (method, path) = parse_request(&request);
|
||||
|
||||
// Skip empty requests
|
||||
if method.is_empty() || path.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Log request for debugging
|
||||
println!("{} {} {} ({})",
|
||||
"REQUEST".green(),
|
||||
method.cyan(),
|
||||
path.yellow(),
|
||||
std::env::current_dir().unwrap().display()
|
||||
);
|
||||
|
||||
stream.write_all(response.as_bytes()).await?;
|
||||
stream.write_all(&content).await?;
|
||||
stream.flush().await?;
|
||||
let (status, content_type, content, cache_control) = if method == "POST" && path == "/api/ask" {
|
||||
// Handle Ask AI API request
|
||||
let (s, ct, c) = handle_ask_api(&request).await;
|
||||
(s, ct, c, "no-cache")
|
||||
} else if method == "OPTIONS" {
|
||||
// Handle CORS preflight
|
||||
("200 OK", "text/plain", Vec::new(), "no-cache")
|
||||
} else if path.starts_with("/oauth/callback") {
|
||||
// Handle OAuth callback - serve the callback HTML page
|
||||
match serve_oauth_callback().await {
|
||||
Ok((ct, data, cc)) => ("200 OK", ct, data, cc),
|
||||
Err(e) => {
|
||||
eprintln!("Error serving OAuth callback: {}", e);
|
||||
("500 INTERNAL SERVER ERROR", "text/html",
|
||||
"<h1>500 - Server Error</h1><p>OAuth callback error</p>".as_bytes().to_vec(),
|
||||
"no-cache")
|
||||
}
|
||||
}
|
||||
} else if path.starts_with("/.well-known/") || path.contains("devtools") {
|
||||
// Ignore browser dev tools and well-known requests
|
||||
("404 NOT FOUND", "text/plain", "Not Found".as_bytes().to_vec(), "no-cache")
|
||||
} else {
|
||||
// Handle static file serving
|
||||
match serve_file(&path).await {
|
||||
Ok((ct, data, cc)) => ("200 OK", ct, data, cc),
|
||||
Err(e) => {
|
||||
// Only log actual file serving errors, not dev tool requests
|
||||
if !path.contains("devtools") && !path.starts_with("/.well-known/") {
|
||||
eprintln!("Error serving {}: {}", path, e);
|
||||
}
|
||||
("404 NOT FOUND", "text/html",
|
||||
format!("<h1>404 - Not Found</h1><p>Path: {}</p>", path).into_bytes(),
|
||||
"no-cache")
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Build HTTP response with proper headers
|
||||
let response_header = format!(
|
||||
"HTTP/1.1 {}\r\nContent-Type: {}\r\nContent-Length: {}\r\nCache-Control: {}\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: GET, POST, OPTIONS\r\nAccess-Control-Allow-Headers: Content-Type\r\nConnection: close\r\n\r\n",
|
||||
status, content_type, content.len(), cache_control
|
||||
);
|
||||
|
||||
// Send response
|
||||
if let Err(e) = stream.write_all(response_header.as_bytes()).await {
|
||||
eprintln!("Error writing headers: {}", e);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Err(e) = stream.write_all(&content).await {
|
||||
eprintln!("Error writing content: {}", e);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Err(e) = stream.flush().await {
|
||||
eprintln!("Error flushing stream: {}", e);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parse_request_path(request: &str) -> String {
|
||||
request
|
||||
.lines()
|
||||
.next()
|
||||
.and_then(|line| line.split_whitespace().nth(1))
|
||||
.unwrap_or("/")
|
||||
.to_string()
|
||||
fn parse_request(request: &str) -> (String, String) {
|
||||
let first_line = request.lines().next().unwrap_or("").trim();
|
||||
if first_line.is_empty() {
|
||||
return (String::new(), String::new());
|
||||
}
|
||||
|
||||
let parts: Vec<&str> = first_line.split_whitespace().collect();
|
||||
if parts.len() < 2 {
|
||||
return (String::new(), String::new());
|
||||
}
|
||||
|
||||
let method = parts[0].to_string();
|
||||
let path = parts[1].to_string();
|
||||
|
||||
(method, path)
|
||||
}
|
||||
|
||||
async fn serve_file(path: &str) -> Result<(&'static str, Vec<u8>)> {
|
||||
let file_path = if path == "/" {
|
||||
async fn handle_ask_api(request: &str) -> (&'static str, &'static str, Vec<u8>) {
|
||||
// Extract JSON body from request
|
||||
let body_start = request.find("\r\n\r\n").map(|i| i + 4).unwrap_or(0);
|
||||
let body = &request[body_start..];
|
||||
|
||||
// Parse question from JSON
|
||||
let question = extract_question_from_json(body).unwrap_or_else(|| "Hello".to_string());
|
||||
|
||||
// Call Ollama API
|
||||
match call_ollama_api(&question).await {
|
||||
Ok(answer) => {
|
||||
let response_json = format!(r#"{{"answer": "{}"}}"#, answer.replace('"', r#"\""#));
|
||||
("200 OK", "application/json", response_json.into_bytes())
|
||||
}
|
||||
Err(_) => {
|
||||
let error_json = r#"{"error": "Failed to get AI response"}"#;
|
||||
("500 INTERNAL SERVER ERROR", "application/json", error_json.as_bytes().to_vec())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_question_from_json(json_str: &str) -> Option<String> {
|
||||
// Simple JSON parsing for {"question": "..."}
|
||||
if let Some(start) = json_str.find(r#""question""#) {
|
||||
if let Some(colon_pos) = json_str[start..].find(':') {
|
||||
let after_colon = &json_str[start + colon_pos + 1..];
|
||||
if let Some(quote_start) = after_colon.find('"') {
|
||||
let after_quote = &after_colon[quote_start + 1..];
|
||||
if let Some(quote_end) = after_quote.find('"') {
|
||||
return Some(after_quote[..quote_end].to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
async fn call_ollama_api(question: &str) -> Result<String> {
|
||||
// Call Ollama API (assuming it's running on localhost:11434)
|
||||
use tokio::process::Command;
|
||||
|
||||
let output = Command::new("curl")
|
||||
.args(&[
|
||||
"-X", "POST",
|
||||
"http://localhost:11434/api/generate",
|
||||
"-H", "Content-Type: application/json",
|
||||
"-d", &format!(r#"{{"model": "llama2", "prompt": "{}", "stream": false}}"#, question.replace('"', r#"\""#))
|
||||
])
|
||||
.output()
|
||||
.await?;
|
||||
|
||||
if output.status.success() {
|
||||
let response = String::from_utf8_lossy(&output.stdout);
|
||||
// Parse Ollama response JSON
|
||||
if let Some(answer) = extract_response_from_ollama(&response) {
|
||||
Ok(answer)
|
||||
} else {
|
||||
Ok("I'm sorry, I couldn't process your question right now.".to_string())
|
||||
}
|
||||
} else {
|
||||
Err(anyhow::anyhow!("Ollama API call failed"))
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_response_from_ollama(json_str: &str) -> Option<String> {
|
||||
// Simple JSON parsing for {"response": "..."}
|
||||
if let Some(start) = json_str.find(r#""response""#) {
|
||||
if let Some(colon_pos) = json_str[start..].find(':') {
|
||||
let after_colon = &json_str[start + colon_pos + 1..];
|
||||
if let Some(quote_start) = after_colon.find('"') {
|
||||
let after_quote = &after_colon[quote_start + 1..];
|
||||
if let Some(quote_end) = after_quote.find('"') {
|
||||
return Some(after_quote[..quote_end].to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
async fn serve_oauth_callback() -> Result<(&'static str, Vec<u8>, &'static str)> {
|
||||
// Serve OAuth callback HTML from static directory
|
||||
let file_path = PathBuf::from("static/oauth/callback.html");
|
||||
|
||||
println!("Serving OAuth callback: {}", file_path.display());
|
||||
|
||||
// If static file doesn't exist, create a default callback
|
||||
if !file_path.exists() {
|
||||
let default_callback = r#"<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>OAuth Callback - ai.log</title>
|
||||
<script>
|
||||
console.log('OAuth callback page loaded');
|
||||
|
||||
// Get all URL parameters and hash
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const hashParams = new URLSearchParams(window.location.hash.substring(1));
|
||||
|
||||
console.log('URL params:', Object.fromEntries(urlParams));
|
||||
console.log('Hash params:', Object.fromEntries(hashParams));
|
||||
|
||||
// Combine parameters
|
||||
const allParams = new URLSearchParams();
|
||||
urlParams.forEach((value, key) => allParams.set(key, value));
|
||||
hashParams.forEach((value, key) => allParams.set(key, value));
|
||||
|
||||
// Check for OAuth response
|
||||
const code = allParams.get('code');
|
||||
const state = allParams.get('state');
|
||||
const iss = allParams.get('iss');
|
||||
const error = allParams.get('error');
|
||||
|
||||
if (error) {
|
||||
console.error('OAuth error:', error);
|
||||
alert('OAuth authentication failed: ' + error);
|
||||
window.close();
|
||||
} else if (code && state) {
|
||||
console.log('OAuth success, redirecting with parameters');
|
||||
|
||||
// Store OAuth data temporarily
|
||||
const oauthData = {
|
||||
code: code,
|
||||
state: state,
|
||||
iss: iss,
|
||||
timestamp: Date.now()
|
||||
};
|
||||
|
||||
localStorage.setItem('oauth_callback_data', JSON.stringify(oauthData));
|
||||
|
||||
// Redirect to parent window or main page with callback indication
|
||||
if (window.opener) {
|
||||
// Popup window - notify parent and close
|
||||
try {
|
||||
window.opener.postMessage({
|
||||
type: 'oauth_callback',
|
||||
data: oauthData
|
||||
}, '*');
|
||||
console.log('Notified parent window');
|
||||
} catch (e) {
|
||||
console.error('Failed to notify parent:', e);
|
||||
}
|
||||
window.close();
|
||||
} else {
|
||||
// Direct navigation - redirect to main page
|
||||
console.log('Redirecting to main page');
|
||||
window.location.href = '/?oauth_callback=true';
|
||||
}
|
||||
} else {
|
||||
console.error('Invalid OAuth callback - missing code or state');
|
||||
alert('Invalid OAuth callback parameters');
|
||||
window.close();
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<div style="font-family: system-ui; text-align: center; padding: 50px;">
|
||||
<h2>🔄 Processing OAuth Authentication...</h2>
|
||||
<p>Please wait while we complete your authentication.</p>
|
||||
<p><small>This window will close automatically.</small></p>
|
||||
</div>
|
||||
</body>
|
||||
</html>"#;
|
||||
return Ok(("text/html; charset=utf-8", default_callback.as_bytes().to_vec(), "no-cache"));
|
||||
}
|
||||
|
||||
let content = tokio::fs::read(&file_path).await
|
||||
.map_err(|e| anyhow::anyhow!("Failed to read OAuth callback file: {}", e))?;
|
||||
|
||||
Ok(("text/html; charset=utf-8", content, "no-cache"))
|
||||
}
|
||||
|
||||
async fn serve_file(path: &str) -> Result<(&'static str, Vec<u8>, &'static str)> {
|
||||
// Remove query parameters from path
|
||||
let clean_path = path.split('?').next().unwrap_or(path);
|
||||
|
||||
let file_path = if clean_path == "/" {
|
||||
PathBuf::from("public/index.html")
|
||||
} else {
|
||||
PathBuf::from("public").join(path.trim_start_matches('/'))
|
||||
PathBuf::from("public").join(clean_path.trim_start_matches('/'))
|
||||
};
|
||||
|
||||
let content_type = match file_path.extension().and_then(|ext| ext.to_str()) {
|
||||
Some("html") => "text/html",
|
||||
Some("css") => "text/css",
|
||||
Some("js") => "application/javascript",
|
||||
Some("json") => "application/json",
|
||||
Some("png") => "image/png",
|
||||
Some("jpg") | Some("jpeg") => "image/jpeg",
|
||||
Some("gif") => "image/gif",
|
||||
Some("svg") => "image/svg+xml",
|
||||
_ => "text/plain",
|
||||
println!("Serving file: {}", file_path.display());
|
||||
|
||||
// Check if file exists and get metadata
|
||||
let metadata = tokio::fs::metadata(&file_path).await?;
|
||||
if !metadata.is_file() {
|
||||
return Err(anyhow::anyhow!("Not a file: {}", file_path.display()));
|
||||
}
|
||||
|
||||
let (content_type, cache_control) = match file_path.extension().and_then(|ext| ext.to_str()) {
|
||||
Some("html") => ("text/html; charset=utf-8", "no-cache"),
|
||||
Some("css") => ("text/css; charset=utf-8", "public, max-age=3600"),
|
||||
Some("js") => ("application/javascript; charset=utf-8", "public, max-age=3600"),
|
||||
Some("json") => ("application/json; charset=utf-8", "no-cache"),
|
||||
Some("md") => ("text/markdown; charset=utf-8", "no-cache"),
|
||||
Some("png") => ("image/png", "public, max-age=86400"),
|
||||
Some("jpg") | Some("jpeg") => ("image/jpeg", "public, max-age=86400"),
|
||||
Some("gif") => ("image/gif", "public, max-age=86400"),
|
||||
Some("svg") => ("image/svg+xml", "public, max-age=3600"),
|
||||
Some("ico") => ("image/x-icon", "public, max-age=86400"),
|
||||
Some("woff") | Some("woff2") => ("font/woff2", "public, max-age=86400"),
|
||||
Some("ttf") => ("font/ttf", "public, max-age=86400"),
|
||||
_ => ("text/plain; charset=utf-8", "no-cache"),
|
||||
};
|
||||
|
||||
let content = tokio::fs::read(file_path).await?;
|
||||
Ok((content_type, content))
|
||||
let content = tokio::fs::read(&file_path).await
|
||||
.map_err(|e| anyhow::anyhow!("Failed to read file {}: {}", file_path.display(), e))?;
|
||||
|
||||
Ok((content_type, content, cache_control))
|
||||
}
|
65
src/commands/serve_oauth.rs
Normal file
65
src/commands/serve_oauth.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
use anyhow::Result;
|
||||
use axum::{
|
||||
extract::{Query, State},
|
||||
http::{HeaderValue, Method, StatusCode},
|
||||
response::{Html, Json},
|
||||
routing::{get, post},
|
||||
Router,
|
||||
};
|
||||
use colored::Colorize;
|
||||
use std::collections::HashMap;
|
||||
use std::path::PathBuf;
|
||||
use tower::ServiceBuilder;
|
||||
use tower_http::{
|
||||
cors::{CorsLayer, Any},
|
||||
services::ServeDir,
|
||||
};
|
||||
use tower_sessions::{MemoryStore, SessionManagerLayer};
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
use crate::oauth::{oauth_callback_handler, oauth_session_handler, oauth_logout_handler};
|
||||
|
||||
pub async fn execute_with_oauth(port: u16) -> Result<()> {
|
||||
// Check if public directory exists
|
||||
if !std::path::Path::new("public").exists() {
|
||||
println!("{}", "No public directory found. Running build first...".yellow());
|
||||
crate::commands::build::execute(std::path::PathBuf::from(".")).await?;
|
||||
}
|
||||
|
||||
// Create session store
|
||||
let session_store = MemoryStore::default();
|
||||
let session_layer = SessionManagerLayer::new(session_store)
|
||||
.with_secure(false); // Set to true in production with HTTPS
|
||||
|
||||
// CORS layer
|
||||
let cors = CorsLayer::new()
|
||||
.allow_origin(Any)
|
||||
.allow_methods([Method::GET, Method::POST, Method::OPTIONS])
|
||||
.allow_headers(Any);
|
||||
|
||||
// Build the router
|
||||
let app = Router::new()
|
||||
// OAuth routes
|
||||
.route("/oauth/callback", get(oauth_callback_handler))
|
||||
.route("/api/oauth/session", get(oauth_session_handler))
|
||||
.route("/api/oauth/logout", post(oauth_logout_handler))
|
||||
// Static file serving
|
||||
.fallback_service(ServeDir::new("public"))
|
||||
.layer(
|
||||
ServiceBuilder::new()
|
||||
.layer(cors)
|
||||
.layer(session_layer)
|
||||
);
|
||||
|
||||
let addr = format!("127.0.0.1:{}", port);
|
||||
let listener = TcpListener::bind(&addr).await?;
|
||||
|
||||
println!("{}", "Starting development server with OAuth support...".green());
|
||||
println!("Serving at: {}", format!("http://{}", addr).blue().underline());
|
||||
println!("OAuth callback: {}", format!("http://{}/oauth/callback", addr).blue().underline());
|
||||
println!("Press Ctrl+C to stop\n");
|
||||
|
||||
axum::serve(listener, app).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
792
src/commands/stream.rs
Normal file
792
src/commands/stream.rs
Normal file
@@ -0,0 +1,792 @@
|
||||
use anyhow::{Result, Context};
|
||||
use colored::Colorize;
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{json, Value};
|
||||
use std::collections::HashSet;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::process::{Command, Stdio};
|
||||
use tokio::time::{sleep, Duration, interval};
|
||||
use tokio_tungstenite::{connect_async, tungstenite::Message};
|
||||
|
||||
use super::auth::{load_config, load_config_with_refresh, AuthConfig};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct JetstreamMessage {
|
||||
collection: Option<String>,
|
||||
commit: Option<JetstreamCommit>,
|
||||
did: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct JetstreamCommit {
|
||||
operation: Option<String>,
|
||||
uri: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
struct UserRecord {
|
||||
did: String,
|
||||
handle: String,
|
||||
pds: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct UserListRecord {
|
||||
#[serde(rename = "$type")]
|
||||
record_type: String,
|
||||
users: Vec<UserRecord>,
|
||||
#[serde(rename = "createdAt")]
|
||||
created_at: String,
|
||||
#[serde(rename = "updatedBy")]
|
||||
updated_by: UserInfo,
|
||||
metadata: Option<Value>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct UserInfo {
|
||||
did: String,
|
||||
handle: String,
|
||||
}
|
||||
|
||||
fn get_pid_file() -> Result<PathBuf> {
|
||||
let home = std::env::var("HOME").context("HOME environment variable not set")?;
|
||||
let pid_dir = PathBuf::from(home).join(".config").join("syui").join("ai").join("log");
|
||||
fs::create_dir_all(&pid_dir)?;
|
||||
Ok(pid_dir.join("stream.pid"))
|
||||
}
|
||||
|
||||
pub async fn start(daemon: bool) -> Result<()> {
|
||||
let config = load_config_with_refresh().await?;
|
||||
let pid_file = get_pid_file()?;
|
||||
|
||||
// Check if already running
|
||||
if pid_file.exists() {
|
||||
let pid = fs::read_to_string(&pid_file)?;
|
||||
println!("{}", format!("⚠️ Stream monitor already running (PID: {})", pid.trim()).yellow());
|
||||
println!("Use 'ailog stream stop' to stop it first.");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if daemon {
|
||||
println!("{}", "🚀 Starting stream monitor as daemon...".cyan());
|
||||
|
||||
// Fork process for daemon mode
|
||||
let current_exe = std::env::current_exe()?;
|
||||
let child = Command::new(current_exe)
|
||||
.args(&["stream", "start"])
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::null())
|
||||
.stderr(Stdio::null())
|
||||
.spawn()?;
|
||||
|
||||
// Save PID
|
||||
fs::write(&pid_file, child.id().to_string())?;
|
||||
|
||||
println!("{}", format!("✅ Stream monitor started as daemon (PID: {})", child.id()).green());
|
||||
println!("Use 'ailog stream status' to check status");
|
||||
println!("Use 'ailog stream stop' to stop monitoring");
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Save current process PID for non-daemon mode
|
||||
let pid = std::process::id();
|
||||
fs::write(&pid_file, pid.to_string())?;
|
||||
|
||||
println!("{}", "🎯 Starting ATProto stream monitor...".cyan());
|
||||
println!("👤 Authenticated as: {}", config.admin.handle.green());
|
||||
println!("📡 Connecting to: {}", config.jetstream.url);
|
||||
println!("📂 Monitoring collections: {}", config.jetstream.collections.join(", "));
|
||||
println!();
|
||||
|
||||
// Setup graceful shutdown
|
||||
let pid_file_clone = pid_file.clone();
|
||||
tokio::spawn(async move {
|
||||
tokio::signal::ctrl_c().await.ok();
|
||||
println!("\n{}", "🛑 Shutting down stream monitor...".yellow());
|
||||
let _ = fs::remove_file(&pid_file_clone);
|
||||
std::process::exit(0);
|
||||
});
|
||||
|
||||
// Start monitoring
|
||||
let mut reconnect_attempts = 0;
|
||||
let max_reconnect_attempts = 10;
|
||||
let mut config = config; // Make config mutable for token refresh
|
||||
|
||||
loop {
|
||||
match run_monitor(&mut config).await {
|
||||
Ok(_) => {
|
||||
println!("{}", "Monitor loop ended normally".blue());
|
||||
break;
|
||||
}
|
||||
Err(e) => {
|
||||
reconnect_attempts += 1;
|
||||
if reconnect_attempts <= max_reconnect_attempts {
|
||||
let delay = std::cmp::min(5 * reconnect_attempts, 30);
|
||||
println!("{}", format!("❌ Monitor error: {}", e).red());
|
||||
|
||||
// Show debug information
|
||||
if reconnect_attempts == 1 {
|
||||
println!("{}", "🔍 Debug information:".yellow());
|
||||
println!(" - Jetstream URL: {}", config.jetstream.url);
|
||||
println!(" - Collections: {:?}", config.jetstream.collections);
|
||||
|
||||
// Test basic connectivity
|
||||
println!("{}", "🧪 Testing basic connectivity...".cyan());
|
||||
test_connectivity().await;
|
||||
}
|
||||
|
||||
println!("{}", format!("🔄 Reconnecting in {}s... ({}/{})",
|
||||
delay, reconnect_attempts, max_reconnect_attempts).yellow());
|
||||
sleep(Duration::from_secs(delay)).await;
|
||||
} else {
|
||||
println!("{}", "❌ Max reconnection attempts reached".red());
|
||||
let _ = fs::remove_file(&pid_file);
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let _ = fs::remove_file(&pid_file);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn run_monitor(config: &mut AuthConfig) -> Result<()> {
|
||||
// Connect to Jetstream
|
||||
println!("{}", format!("🔗 Attempting to connect to: {}", config.jetstream.url).blue());
|
||||
|
||||
// Create request with HTTP/1.1 headers to ensure WebSocket compatibility
|
||||
let request = tungstenite::http::Request::builder()
|
||||
.method("GET")
|
||||
.uri(&config.jetstream.url)
|
||||
.header("Host", config.jetstream.url.replace("wss://", "").replace("/subscribe", ""))
|
||||
.header("Upgrade", "websocket")
|
||||
.header("Connection", "Upgrade")
|
||||
.header("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ==")
|
||||
.header("Sec-WebSocket-Version", "13")
|
||||
.body(())?;
|
||||
|
||||
let (ws_stream, response) = connect_async(request).await
|
||||
.with_context(|| format!("Failed to connect to Jetstream at {}", config.jetstream.url))?;
|
||||
|
||||
println!("{}", format!("📡 WebSocket handshake status: {}", response.status()).blue());
|
||||
|
||||
println!("{}", "✅ Connected to Jetstream".green());
|
||||
|
||||
// Since Jetstream may not include custom collections, we'll use a hybrid approach:
|
||||
// 1. Keep WebSocket connection for any potential custom collection events
|
||||
// 2. Add periodic polling for ai.syui.log collection
|
||||
|
||||
let (mut write, mut read) = ws_stream.split();
|
||||
|
||||
// Subscribe to collections
|
||||
let subscribe_msg = json!({
|
||||
"wantedCollections": config.jetstream.collections
|
||||
});
|
||||
|
||||
write.send(Message::Text(subscribe_msg.to_string())).await?;
|
||||
println!("{}", "📨 Subscribed to collections".blue());
|
||||
|
||||
// Start periodic polling task
|
||||
let config_clone = config.clone();
|
||||
let polling_task = tokio::spawn(async move {
|
||||
poll_comments_periodically(config_clone).await
|
||||
});
|
||||
|
||||
// Process WebSocket messages
|
||||
let ws_task = async {
|
||||
while let Some(msg) = read.next().await {
|
||||
match msg? {
|
||||
Message::Text(text) => {
|
||||
// Filter out standard Bluesky collections for cleaner output
|
||||
let should_debug = std::env::var("AILOG_DEBUG").is_ok();
|
||||
let is_standard_collection = text.contains("app.bsky.feed.") ||
|
||||
text.contains("app.bsky.actor.") ||
|
||||
text.contains("app.bsky.graph.");
|
||||
|
||||
// Only show debug for custom collections or when explicitly requested
|
||||
if should_debug && (!is_standard_collection || std::env::var("AILOG_DEBUG_ALL").is_ok()) {
|
||||
println!("{}", format!("🔍 Received: {}", text).blue());
|
||||
}
|
||||
|
||||
if let Err(e) = handle_message(&text, config).await {
|
||||
println!("{}", format!("⚠️ Failed to handle message: {}", e).yellow());
|
||||
}
|
||||
}
|
||||
Message::Close(_) => {
|
||||
println!("{}", "🔌 WebSocket closed by server".yellow());
|
||||
break;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok::<(), anyhow::Error>(())
|
||||
};
|
||||
|
||||
// Run both tasks concurrently
|
||||
tokio::select! {
|
||||
result = polling_task => {
|
||||
match result {
|
||||
Ok(Ok(_)) => println!("{}", "📊 Polling task completed".blue()),
|
||||
Ok(Err(e)) => println!("{}", format!("❌ Polling task error: {}", e).red()),
|
||||
Err(e) => println!("{}", format!("❌ Polling task panic: {}", e).red()),
|
||||
}
|
||||
}
|
||||
result = ws_task => {
|
||||
match result {
|
||||
Ok(_) => println!("{}", "📡 WebSocket task completed".blue()),
|
||||
Err(e) => println!("{}", format!("❌ WebSocket task error: {}", e).red()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_message(text: &str, config: &mut AuthConfig) -> Result<()> {
|
||||
let message: JetstreamMessage = serde_json::from_str(text)?;
|
||||
|
||||
// Debug: Check all received collections (but filter standard ones)
|
||||
if let Some(collection) = &message.collection {
|
||||
let is_standard_collection = collection.starts_with("app.bsky.");
|
||||
|
||||
if std::env::var("AILOG_DEBUG").is_ok() && (!is_standard_collection || std::env::var("AILOG_DEBUG_ALL").is_ok()) {
|
||||
println!("{}", format!("📂 Collection: {}", collection).cyan());
|
||||
}
|
||||
|
||||
// Skip processing standard Bluesky collections
|
||||
if is_standard_collection {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
|
||||
// Check if this is a comment creation
|
||||
if let (Some(collection), Some(commit), Some(did)) =
|
||||
(&message.collection, &message.commit, &message.did) {
|
||||
|
||||
if collection == "ai.syui.log" && commit.operation.as_deref() == Some("create") {
|
||||
let unknown_uri = "unknown".to_string();
|
||||
let uri = commit.uri.as_ref().unwrap_or(&unknown_uri);
|
||||
|
||||
println!("{}", "🆕 New comment detected!".green().bold());
|
||||
println!(" 📝 URI: {}", uri);
|
||||
println!(" 👤 Author DID: {}", did);
|
||||
|
||||
// Resolve handle
|
||||
match resolve_handle(did).await {
|
||||
Ok(handle) => {
|
||||
println!(" 🏷️ Handle: {}", handle.cyan());
|
||||
|
||||
// Update user list
|
||||
if let Err(e) = update_user_list(config, did, &handle).await {
|
||||
println!("{}", format!(" ⚠️ Failed to update user list: {}", e).yellow());
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("{}", format!(" ⚠️ Failed to resolve handle: {}", e).yellow());
|
||||
}
|
||||
}
|
||||
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn resolve_handle(did: &str) -> Result<String> {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("https://public.api.bsky.app/xrpc/app.bsky.actor.getProfile?actor={}",
|
||||
urlencoding::encode(did));
|
||||
|
||||
let response = client.get(&url).send().await?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
return Err(anyhow::anyhow!("Failed to resolve handle: {}", response.status()));
|
||||
}
|
||||
|
||||
let profile: Value = response.json().await?;
|
||||
let handle = profile["handle"].as_str()
|
||||
.ok_or_else(|| anyhow::anyhow!("Handle not found in profile response"))?;
|
||||
|
||||
Ok(handle.to_string())
|
||||
}
|
||||
|
||||
async fn update_user_list(config: &mut AuthConfig, did: &str, handle: &str) -> Result<()> {
|
||||
// Get current user list
|
||||
let current_users = get_current_user_list(config).await?;
|
||||
|
||||
// Check if user already exists
|
||||
if current_users.iter().any(|u| u.did == did) {
|
||||
println!(" ℹ️ User already in list: {}", handle.blue());
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
println!(" ➕ Adding new user to list: {}", handle.green());
|
||||
|
||||
// Detect PDS
|
||||
let pds = if handle.ends_with(".syu.is") {
|
||||
"https://syu.is"
|
||||
} else {
|
||||
"https://bsky.social"
|
||||
};
|
||||
|
||||
// Add new user
|
||||
let new_user = UserRecord {
|
||||
did: did.to_string(),
|
||||
handle: handle.to_string(),
|
||||
pds: pds.to_string(),
|
||||
};
|
||||
|
||||
let mut updated_users = current_users;
|
||||
updated_users.push(new_user);
|
||||
|
||||
// Post updated user list
|
||||
post_user_list(config, &updated_users, json!({
|
||||
"reason": "auto_add_commenter",
|
||||
"trigger_did": did,
|
||||
"trigger_handle": handle
|
||||
})).await?;
|
||||
|
||||
println!("{}", " ✅ User list updated successfully".green());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_current_user_list(config: &mut AuthConfig) -> Result<Vec<UserRecord>> {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("{}/xrpc/com.atproto.repo.listRecords?repo={}&collection=ai.syui.log.user&limit=10",
|
||||
config.admin.pds,
|
||||
urlencoding::encode(&config.admin.did));
|
||||
|
||||
let response = client
|
||||
.get(&url)
|
||||
.header("Authorization", format!("Bearer {}", config.admin.access_jwt))
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
if response.status().as_u16() == 401 {
|
||||
// Token expired, try to refresh
|
||||
if let Ok(_) = super::auth::load_config_with_refresh().await {
|
||||
// Retry with refreshed token
|
||||
let refreshed_config = super::auth::load_config()?;
|
||||
*config = refreshed_config;
|
||||
return Box::pin(get_current_user_list(config)).await;
|
||||
}
|
||||
}
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
|
||||
let data: Value = response.json().await?;
|
||||
let empty_vec = vec![];
|
||||
let records = data["records"].as_array().unwrap_or(&empty_vec);
|
||||
|
||||
if records.is_empty() {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
|
||||
// Get the latest record
|
||||
let latest_record = &records[0];
|
||||
let empty_users = vec![];
|
||||
let users = latest_record["value"]["users"].as_array().unwrap_or(&empty_users);
|
||||
|
||||
let mut user_list = Vec::new();
|
||||
for user in users {
|
||||
if let (Some(did), Some(handle), Some(pds)) = (
|
||||
user["did"].as_str(),
|
||||
user["handle"].as_str(),
|
||||
user["pds"].as_str(),
|
||||
) {
|
||||
user_list.push(UserRecord {
|
||||
did: did.to_string(),
|
||||
handle: handle.to_string(),
|
||||
pds: pds.to_string(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Ok(user_list)
|
||||
}
|
||||
|
||||
async fn post_user_list(config: &mut AuthConfig, users: &[UserRecord], metadata: Value) -> Result<()> {
|
||||
let client = reqwest::Client::new();
|
||||
|
||||
let now = chrono::Utc::now();
|
||||
let rkey = now.format("%Y-%m-%dT%H-%M-%S-%3fZ").to_string().replace(".", "-");
|
||||
|
||||
let record = UserListRecord {
|
||||
record_type: "ai.syui.log.user".to_string(),
|
||||
users: users.to_vec(),
|
||||
created_at: now.to_rfc3339(),
|
||||
updated_by: UserInfo {
|
||||
did: config.admin.did.clone(),
|
||||
handle: config.admin.handle.clone(),
|
||||
},
|
||||
metadata: Some(metadata.clone()),
|
||||
};
|
||||
|
||||
let url = format!("{}/xrpc/com.atproto.repo.putRecord", config.admin.pds);
|
||||
|
||||
let request_body = json!({
|
||||
"repo": config.admin.did,
|
||||
"collection": "ai.syui.log.user",
|
||||
"rkey": rkey,
|
||||
"record": record
|
||||
});
|
||||
|
||||
let response = client
|
||||
.post(&url)
|
||||
.header("Authorization", format!("Bearer {}", config.admin.access_jwt))
|
||||
.header("Content-Type", "application/json")
|
||||
.json(&request_body)
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
let status = response.status();
|
||||
if status.as_u16() == 401 {
|
||||
// Token expired, try to refresh and retry
|
||||
if let Ok(_) = super::auth::load_config_with_refresh().await {
|
||||
let refreshed_config = super::auth::load_config()?;
|
||||
*config = refreshed_config;
|
||||
return Box::pin(post_user_list(config, users, metadata)).await;
|
||||
}
|
||||
}
|
||||
let error_text = response.text().await?;
|
||||
return Err(anyhow::anyhow!("Failed to post user list: {} - {}", status, error_text));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn stop() -> Result<()> {
|
||||
let pid_file = get_pid_file()?;
|
||||
|
||||
if !pid_file.exists() {
|
||||
println!("{}", "ℹ️ Stream monitor is not running.".blue());
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let pid_str = fs::read_to_string(&pid_file)?;
|
||||
let pid = pid_str.trim();
|
||||
|
||||
println!("{}", format!("🛑 Stopping stream monitor (PID: {})...", pid).cyan());
|
||||
|
||||
// Try to kill the process
|
||||
let output = Command::new("kill")
|
||||
.arg(pid)
|
||||
.output()?;
|
||||
|
||||
if output.status.success() {
|
||||
// Wait a bit for the process to stop
|
||||
sleep(Duration::from_secs(2)).await;
|
||||
|
||||
// Remove PID file
|
||||
fs::remove_file(&pid_file)?;
|
||||
|
||||
println!("{}", "✅ Stream monitor stopped successfully".green());
|
||||
} else {
|
||||
println!("{}", format!("⚠️ Failed to stop process: {}",
|
||||
String::from_utf8_lossy(&output.stderr)).yellow());
|
||||
|
||||
// Force remove PID file anyway
|
||||
fs::remove_file(&pid_file)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn status() -> Result<()> {
|
||||
let pid_file = get_pid_file()?;
|
||||
|
||||
println!("{}", "📊 Stream Monitor Status".cyan().bold());
|
||||
println!("─────────────────────────");
|
||||
|
||||
if !pid_file.exists() {
|
||||
println!("{}", "📴 Status: Not running".red());
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let pid_str = fs::read_to_string(&pid_file)?;
|
||||
let pid = pid_str.trim();
|
||||
|
||||
// Check if process is actually running
|
||||
let output = Command::new("ps")
|
||||
.args(&["-p", pid])
|
||||
.output()?;
|
||||
|
||||
if output.status.success() {
|
||||
println!("{}", "✅ Status: Running".green());
|
||||
println!("🆔 PID: {}", pid);
|
||||
println!("📁 PID file: {}", pid_file.display());
|
||||
|
||||
// Show config info
|
||||
match load_config() {
|
||||
Ok(config) => {
|
||||
println!("👤 Authenticated as: {}", config.admin.handle);
|
||||
println!("📡 Jetstream URL: {}", config.jetstream.url);
|
||||
println!("📂 Monitoring: {}", config.jetstream.collections.join(", "));
|
||||
}
|
||||
Err(_) => {
|
||||
println!("{}", "⚠️ No authentication config found".yellow());
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("{}", "❌ Status: Process not found (stale PID file)".red());
|
||||
println!("🗑️ Removing stale PID file...");
|
||||
fs::remove_file(&pid_file)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn test_connectivity() {
|
||||
let endpoints = [
|
||||
"wss://jetstream2.us-east.bsky.network/subscribe",
|
||||
"wss://jetstream1.us-east.bsky.network/subscribe",
|
||||
"wss://jetstream2.us-west.bsky.network/subscribe",
|
||||
];
|
||||
|
||||
for endpoint in &endpoints {
|
||||
print!(" Testing {}: ", endpoint);
|
||||
|
||||
// Test basic HTTP connectivity first
|
||||
let http_url = endpoint.replace("wss://", "https://").replace("/subscribe", "");
|
||||
match reqwest::Client::new()
|
||||
.head(&http_url)
|
||||
.timeout(Duration::from_secs(5))
|
||||
.send()
|
||||
.await
|
||||
{
|
||||
Ok(response) => {
|
||||
if response.status().as_u16() == 405 {
|
||||
println!("{}", "✅ HTTP reachable".green());
|
||||
} else {
|
||||
println!("{}", format!("⚠️ HTTP status: {}", response.status()).yellow());
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("{}", format!("❌ HTTP failed: {}", e).red());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Test WebSocket connectivity
|
||||
print!(" WebSocket {}: ", endpoint);
|
||||
match tokio::time::timeout(Duration::from_secs(5), connect_async(*endpoint)).await {
|
||||
Ok(Ok(_)) => println!("{}", "✅ Connected".green()),
|
||||
Ok(Err(e)) => println!("{}", format!("❌ Failed: {}", e).red()),
|
||||
Err(_) => println!("{}", "❌ Timeout".red()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn poll_comments_periodically(mut config: AuthConfig) -> Result<()> {
|
||||
println!("{}", "📊 Starting periodic comment polling...".cyan());
|
||||
|
||||
let mut known_comments = HashSet::new();
|
||||
let mut interval = interval(Duration::from_secs(30)); // Poll every 30 seconds
|
||||
|
||||
// Initial population of known comments
|
||||
if let Ok(comments) = get_recent_comments(&mut config).await {
|
||||
for comment in &comments {
|
||||
if let Some(uri) = comment.get("uri").and_then(|v| v.as_str()) {
|
||||
known_comments.insert(uri.to_string());
|
||||
if std::env::var("AILOG_DEBUG").is_ok() {
|
||||
println!("{}", format!("🔍 Existing comment: {}", uri).blue());
|
||||
}
|
||||
}
|
||||
}
|
||||
println!("{}", format!("📝 Found {} existing comments", known_comments.len()).blue());
|
||||
|
||||
// Debug: Show full response for first comment
|
||||
if std::env::var("AILOG_DEBUG").is_ok() && !comments.is_empty() {
|
||||
println!("{}", format!("🔍 Sample comment data: {}", serde_json::to_string_pretty(&comments[0]).unwrap_or_default()).yellow());
|
||||
}
|
||||
} else {
|
||||
println!("{}", "⚠️ Failed to get initial comments".yellow());
|
||||
}
|
||||
|
||||
loop {
|
||||
interval.tick().await;
|
||||
|
||||
if std::env::var("AILOG_DEBUG").is_ok() {
|
||||
println!("{}", "🔄 Polling for new comments...".cyan());
|
||||
}
|
||||
|
||||
match get_recent_comments(&mut config).await {
|
||||
Ok(comments) => {
|
||||
if std::env::var("AILOG_DEBUG").is_ok() {
|
||||
println!("{}", format!("📊 Retrieved {} comments from API", comments.len()).cyan());
|
||||
}
|
||||
|
||||
for comment in comments {
|
||||
if let (Some(uri), Some(value)) = (
|
||||
comment.get("uri").and_then(|v| v.as_str()),
|
||||
comment.get("value")
|
||||
) {
|
||||
if !known_comments.contains(uri) {
|
||||
// New comment detected
|
||||
known_comments.insert(uri.to_string());
|
||||
|
||||
if let Some(created_at) = value.get("createdAt").and_then(|v| v.as_str()) {
|
||||
// Check if this comment is recent (within last 5 minutes)
|
||||
if is_recent_comment(created_at) {
|
||||
println!("{}", "🆕 New comment detected via polling!".green().bold());
|
||||
println!(" 📝 URI: {}", uri);
|
||||
|
||||
// Extract author DID from URI
|
||||
if let Some(did) = extract_did_from_uri(uri) {
|
||||
println!(" 👤 Author DID: {}", did);
|
||||
|
||||
// Resolve handle and update user list
|
||||
match resolve_handle(&did).await {
|
||||
Ok(handle) => {
|
||||
println!(" 🏷️ Handle: {}", handle.cyan());
|
||||
|
||||
if let Err(e) = update_user_list(&mut config, &did, &handle).await {
|
||||
println!("{}", format!(" ⚠️ Failed to update user list: {}", e).yellow());
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("{}", format!(" ⚠️ Failed to resolve handle: {}", e).yellow());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("{}", format!("⚠️ Failed to poll comments: {}", e).yellow());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_recent_comments(config: &mut AuthConfig) -> Result<Vec<Value>> {
|
||||
let client = reqwest::Client::new();
|
||||
let url = format!("{}/xrpc/com.atproto.repo.listRecords?repo={}&collection=ai.syui.log&limit=20",
|
||||
config.admin.pds,
|
||||
urlencoding::encode(&config.admin.did));
|
||||
|
||||
if std::env::var("AILOG_DEBUG").is_ok() {
|
||||
println!("{}", format!("🌐 API Request URL: {}", url).yellow());
|
||||
}
|
||||
|
||||
let response = client
|
||||
.get(&url)
|
||||
.header("Authorization", format!("Bearer {}", config.admin.access_jwt))
|
||||
.send()
|
||||
.await?;
|
||||
|
||||
if !response.status().is_success() {
|
||||
if std::env::var("AILOG_DEBUG").is_ok() {
|
||||
println!("{}", format!("❌ API Error: {} {}", response.status(), response.status().canonical_reason().unwrap_or("Unknown")).red());
|
||||
}
|
||||
|
||||
if response.status().as_u16() == 401 {
|
||||
// Token expired, try to refresh
|
||||
if let Ok(_) = super::auth::load_config_with_refresh().await {
|
||||
let refreshed_config = super::auth::load_config()?;
|
||||
*config = refreshed_config;
|
||||
return Box::pin(get_recent_comments(config)).await;
|
||||
}
|
||||
}
|
||||
return Err(anyhow::anyhow!("Failed to fetch comments: {}", response.status()));
|
||||
}
|
||||
|
||||
let data: Value = response.json().await?;
|
||||
|
||||
if std::env::var("AILOG_DEBUG").is_ok() {
|
||||
println!("{}", format!("📄 Raw API Response: {}", serde_json::to_string_pretty(&data).unwrap_or_default()).magenta());
|
||||
}
|
||||
|
||||
let empty_vec = vec![];
|
||||
let records = data["records"].as_array().unwrap_or(&empty_vec);
|
||||
|
||||
Ok(records.to_vec())
|
||||
}
|
||||
|
||||
fn is_recent_comment(created_at: &str) -> bool {
|
||||
use chrono::{DateTime, Utc, Duration};
|
||||
|
||||
if let Ok(comment_time) = DateTime::parse_from_rfc3339(created_at) {
|
||||
let now = Utc::now();
|
||||
let comment_utc = comment_time.with_timezone(&Utc);
|
||||
let diff = now.signed_duration_since(comment_utc);
|
||||
|
||||
// Consider comments from the last 5 minutes as "recent"
|
||||
diff <= Duration::minutes(5) && diff >= Duration::zero()
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn extract_did_from_uri(uri: &str) -> Option<String> {
|
||||
// URI format: at://did:plc:xxx/ai.syui.log/yyy
|
||||
if let Some(captures) = uri.strip_prefix("at://") {
|
||||
if let Some(end) = captures.find("/") {
|
||||
return Some(captures[..end].to_string());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub async fn test_api() -> Result<()> {
|
||||
println!("{}", "🧪 Testing API access to comments collection...".cyan().bold());
|
||||
|
||||
let mut config = load_config_with_refresh().await?;
|
||||
|
||||
println!("👤 Testing as: {}", config.admin.handle.green());
|
||||
println!("🌐 PDS: {}", config.admin.pds);
|
||||
println!("🆔 DID: {}", config.admin.did);
|
||||
println!();
|
||||
|
||||
// Test API access
|
||||
match get_recent_comments(&mut config).await {
|
||||
Ok(comments) => {
|
||||
println!("{}", format!("✅ Successfully retrieved {} comments", comments.len()).green());
|
||||
|
||||
if comments.is_empty() {
|
||||
println!("{}", "ℹ️ No comments found in ai.syui.log collection".blue());
|
||||
println!("💡 Try posting a comment first using the web interface");
|
||||
} else {
|
||||
println!("{}", "📝 Comment details:".cyan());
|
||||
for (i, comment) in comments.iter().enumerate() {
|
||||
println!(" {}. URI: {}", i + 1,
|
||||
comment.get("uri").and_then(|v| v.as_str()).unwrap_or("N/A"));
|
||||
|
||||
if let Some(value) = comment.get("value") {
|
||||
if let Some(created_at) = value.get("createdAt").and_then(|v| v.as_str()) {
|
||||
println!(" Created: {}", created_at);
|
||||
}
|
||||
if let Some(text) = value.get("text").and_then(|v| v.as_str()) {
|
||||
let preview = if text.len() > 50 {
|
||||
format!("{}...", &text[..50])
|
||||
} else {
|
||||
text.to_string()
|
||||
};
|
||||
println!(" Text: {}", preview);
|
||||
}
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("{}", format!("❌ API test failed: {}", e).red());
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
181
src/generator.rs
181
src/generator.rs
@@ -94,6 +94,46 @@ impl Generator {
|
||||
fs::copy(path, &dest_path)?;
|
||||
}
|
||||
}
|
||||
|
||||
// Copy files from atproto-auth-widget dist (if available)
|
||||
let widget_dist = self.base_path.join("atproto-auth-widget/dist");
|
||||
if widget_dist.exists() {
|
||||
for entry in WalkDir::new(&widget_dist).min_depth(1) {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
let relative_path = path.strip_prefix(&widget_dist)?;
|
||||
let dest_path = public_dir.join(relative_path);
|
||||
|
||||
if path.is_dir() {
|
||||
fs::create_dir_all(&dest_path)?;
|
||||
} else {
|
||||
if let Some(parent) = dest_path.parent() {
|
||||
fs::create_dir_all(parent)?;
|
||||
}
|
||||
fs::copy(path, &dest_path)?;
|
||||
}
|
||||
}
|
||||
println!("{} widget files from dist", "Copied".yellow());
|
||||
}
|
||||
|
||||
// Handle client-metadata.json based on environment (fallback)
|
||||
let is_production = std::env::var("PRODUCTION").unwrap_or_default() == "true";
|
||||
let metadata_dest = public_dir.join("client-metadata.json");
|
||||
|
||||
// First try to get from widget dist (preferred)
|
||||
let widget_metadata = widget_dist.join("client-metadata.json");
|
||||
if widget_metadata.exists() {
|
||||
fs::copy(&widget_metadata, &metadata_dest)?;
|
||||
println!("{} client-metadata.json from widget", "Using".yellow());
|
||||
} else if is_production {
|
||||
// Fallback to local static files
|
||||
let prod_metadata = static_dir.join("client-metadata-prod.json");
|
||||
if prod_metadata.exists() {
|
||||
fs::copy(&prod_metadata, &metadata_dest)?;
|
||||
println!("{} production client-metadata.json (fallback)", "Using".yellow());
|
||||
}
|
||||
}
|
||||
|
||||
println!("{} static files", "Copied".cyan());
|
||||
}
|
||||
|
||||
@@ -144,11 +184,16 @@ impl Generator {
|
||||
|
||||
let html_content = self.markdown_processor.render(&content)?;
|
||||
|
||||
let slug = path
|
||||
.file_stem()
|
||||
.and_then(|s| s.to_str())
|
||||
.unwrap_or("post")
|
||||
.to_string();
|
||||
// Use slug from frontmatter if available, otherwise derive from filename
|
||||
let slug = frontmatter.get("slug")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|s| s.to_string())
|
||||
.unwrap_or_else(|| {
|
||||
path.file_stem()
|
||||
.and_then(|s| s.to_str())
|
||||
.unwrap_or("post")
|
||||
.to_string()
|
||||
});
|
||||
|
||||
let mut post = Post {
|
||||
title: frontmatter.get("title")
|
||||
@@ -211,7 +256,34 @@ impl Generator {
|
||||
}
|
||||
|
||||
async fn generate_index(&self, posts: &[Post]) -> Result<()> {
|
||||
let context = self.template_engine.create_context(&self.config, posts)?;
|
||||
// Enhance posts with additional metadata for timeline view
|
||||
let enhanced_posts: Vec<serde_json::Value> = posts.iter().map(|post| {
|
||||
let excerpt = self.extract_excerpt(&post.content);
|
||||
let markdown_url = format!("/posts/{}.md", post.slug);
|
||||
let translation_url = if let Some(ref translations) = post.translations {
|
||||
translations.first().map(|t| t.url.clone())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
serde_json::json!({
|
||||
"title": post.title,
|
||||
"date": post.date,
|
||||
"content": post.content,
|
||||
"slug": post.slug,
|
||||
"url": post.url,
|
||||
"tags": post.tags,
|
||||
"excerpt": excerpt,
|
||||
"markdown_url": markdown_url,
|
||||
"translation_url": translation_url,
|
||||
"language": self.config.site.language
|
||||
})
|
||||
}).collect();
|
||||
|
||||
let mut context = tera::Context::new();
|
||||
context.insert("config", &self.config.site);
|
||||
context.insert("posts", &enhanced_posts);
|
||||
|
||||
let html = self.template_engine.render("index.html", &context)?;
|
||||
|
||||
let output_path = self.base_path.join("public/index.html");
|
||||
@@ -223,7 +295,33 @@ impl Generator {
|
||||
async fn generate_post_page(&self, post: &Post) -> Result<()> {
|
||||
let mut context = tera::Context::new();
|
||||
context.insert("config", &self.config.site);
|
||||
context.insert("post", post);
|
||||
|
||||
// Create enhanced post with additional URLs
|
||||
let mut enhanced_post = post.clone();
|
||||
enhanced_post.url = format!("/posts/{}.html", post.slug);
|
||||
|
||||
// Add markdown view URL
|
||||
let markdown_url = format!("/posts/{}.md", post.slug);
|
||||
|
||||
// Add translation URLs if available
|
||||
let translation_urls: Vec<String> = if let Some(ref translations) = post.translations {
|
||||
translations.iter().map(|t| t.url.clone()).collect()
|
||||
} else {
|
||||
Vec::new()
|
||||
};
|
||||
|
||||
context.insert("post", &serde_json::json!({
|
||||
"title": enhanced_post.title,
|
||||
"date": enhanced_post.date,
|
||||
"content": enhanced_post.content,
|
||||
"slug": enhanced_post.slug,
|
||||
"url": enhanced_post.url,
|
||||
"tags": enhanced_post.tags,
|
||||
"ai_comment": enhanced_post.ai_comment,
|
||||
"markdown_url": markdown_url,
|
||||
"translation_url": translation_urls.first(),
|
||||
"language": self.config.site.language
|
||||
}));
|
||||
|
||||
let html = self.template_engine.render_with_context("post.html", &context)?;
|
||||
|
||||
@@ -232,6 +330,9 @@ impl Generator {
|
||||
|
||||
let output_path = output_dir.join(format!("{}.html", post.slug));
|
||||
fs::write(output_path, html)?;
|
||||
|
||||
// Generate markdown view
|
||||
self.generate_markdown_view(post).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -260,6 +361,72 @@ impl Generator {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn extract_excerpt(&self, html_content: &str) -> String {
|
||||
// Simple excerpt extraction - take first 200 characters of text content
|
||||
let text_content = html_content
|
||||
.replace("<p>", "")
|
||||
.replace("</p>", " ")
|
||||
.replace("<br>", " ")
|
||||
.replace("<br/>", " ");
|
||||
|
||||
// Remove HTML tags with a simple regex-like approach
|
||||
let mut text = String::new();
|
||||
let mut in_tag = false;
|
||||
for ch in text_content.chars() {
|
||||
match ch {
|
||||
'<' => in_tag = true,
|
||||
'>' => in_tag = false,
|
||||
_ if !in_tag => text.push(ch),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
let excerpt = text.trim().chars().take(200).collect::<String>();
|
||||
if text.len() > 200 {
|
||||
format!("{}...", excerpt)
|
||||
} else {
|
||||
excerpt
|
||||
}
|
||||
}
|
||||
|
||||
async fn generate_markdown_view(&self, post: &Post) -> Result<()> {
|
||||
// Find original markdown file
|
||||
let posts_dir = self.base_path.join("content/posts");
|
||||
|
||||
// Try to find the markdown file by checking all files in posts directory
|
||||
for entry in fs::read_dir(&posts_dir)? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
|
||||
if let Some(extension) = path.extension() {
|
||||
if extension == "md" {
|
||||
let content = fs::read_to_string(&path)?;
|
||||
let (frontmatter, _) = self.markdown_processor.parse_frontmatter(&content)?;
|
||||
|
||||
// Check if this file has the same slug
|
||||
let file_slug = frontmatter.get("slug")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or_else(|| {
|
||||
path.file_stem()
|
||||
.and_then(|s| s.to_str())
|
||||
.unwrap_or("")
|
||||
});
|
||||
|
||||
if file_slug == post.slug {
|
||||
let output_dir = self.base_path.join("public/posts");
|
||||
fs::create_dir_all(&output_dir)?;
|
||||
|
||||
let output_path = output_dir.join(format!("{}.md", post.slug));
|
||||
fs::write(output_path, content)?;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, serde::Serialize)]
|
||||
|
87
src/main.rs
87
src/main.rs
@@ -8,6 +8,7 @@ mod doc_generator;
|
||||
mod generator;
|
||||
mod markdown;
|
||||
mod template;
|
||||
mod oauth;
|
||||
mod translator;
|
||||
mod config;
|
||||
mod ai;
|
||||
@@ -44,15 +45,25 @@ enum Commands {
|
||||
/// Post format
|
||||
#[arg(short, long, default_value = "md")]
|
||||
format: String,
|
||||
/// Path to the blog directory
|
||||
#[arg(default_value = ".")]
|
||||
path: PathBuf,
|
||||
},
|
||||
/// Serve the blog locally
|
||||
Serve {
|
||||
/// Port to serve on
|
||||
#[arg(short, long, default_value = "8080")]
|
||||
port: u16,
|
||||
/// Path to the blog directory
|
||||
#[arg(default_value = ".")]
|
||||
path: PathBuf,
|
||||
},
|
||||
/// Clean build artifacts
|
||||
Clean,
|
||||
Clean {
|
||||
/// Path to the blog directory
|
||||
#[arg(default_value = ".")]
|
||||
path: PathBuf,
|
||||
},
|
||||
/// Start MCP server for ai.gpt integration
|
||||
Mcp {
|
||||
/// Port to serve MCP on
|
||||
@@ -64,6 +75,42 @@ enum Commands {
|
||||
},
|
||||
/// Generate documentation from code
|
||||
Doc(commands::doc::DocCommand),
|
||||
/// ATProto authentication
|
||||
Auth {
|
||||
#[command(subcommand)]
|
||||
command: AuthCommands,
|
||||
},
|
||||
/// ATProto stream monitoring
|
||||
Stream {
|
||||
#[command(subcommand)]
|
||||
command: StreamCommands,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum AuthCommands {
|
||||
/// Initialize OAuth authentication
|
||||
Init,
|
||||
/// Show current authentication status
|
||||
Status,
|
||||
/// Logout and clear credentials
|
||||
Logout,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
enum StreamCommands {
|
||||
/// Start monitoring ATProto streams
|
||||
Start {
|
||||
/// Run as daemon
|
||||
#[arg(short, long)]
|
||||
daemon: bool,
|
||||
},
|
||||
/// Stop monitoring
|
||||
Stop,
|
||||
/// Show monitoring status
|
||||
Status,
|
||||
/// Test API access to comments collection
|
||||
Test,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
@@ -77,13 +124,16 @@ async fn main() -> Result<()> {
|
||||
Commands::Build { path } => {
|
||||
commands::build::execute(path).await?;
|
||||
}
|
||||
Commands::New { title, format } => {
|
||||
Commands::New { title, format, path } => {
|
||||
std::env::set_current_dir(path)?;
|
||||
commands::new::execute(title, format).await?;
|
||||
}
|
||||
Commands::Serve { port } => {
|
||||
Commands::Serve { port, path } => {
|
||||
std::env::set_current_dir(path)?;
|
||||
commands::serve::execute(port).await?;
|
||||
}
|
||||
Commands::Clean => {
|
||||
Commands::Clean { path } => {
|
||||
std::env::set_current_dir(path)?;
|
||||
commands::clean::execute().await?;
|
||||
}
|
||||
Commands::Mcp { port, path } => {
|
||||
@@ -94,6 +144,35 @@ async fn main() -> Result<()> {
|
||||
Commands::Doc(doc_cmd) => {
|
||||
doc_cmd.execute(std::env::current_dir()?).await?;
|
||||
}
|
||||
Commands::Auth { command } => {
|
||||
match command {
|
||||
AuthCommands::Init => {
|
||||
commands::auth::init().await?;
|
||||
}
|
||||
AuthCommands::Status => {
|
||||
commands::auth::status().await?;
|
||||
}
|
||||
AuthCommands::Logout => {
|
||||
commands::auth::logout().await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Commands::Stream { command } => {
|
||||
match command {
|
||||
StreamCommands::Start { daemon } => {
|
||||
commands::stream::start(daemon).await?;
|
||||
}
|
||||
StreamCommands::Stop => {
|
||||
commands::stream::stop().await?;
|
||||
}
|
||||
StreamCommands::Status => {
|
||||
commands::stream::status().await?;
|
||||
}
|
||||
StreamCommands::Test => {
|
||||
commands::stream::test_api().await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
206
src/oauth.rs
Normal file
206
src/oauth.rs
Normal file
@@ -0,0 +1,206 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tower_sessions::Session;
|
||||
use axum::{
|
||||
extract::{Query, State},
|
||||
response::{Html, Redirect},
|
||||
Json,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
use jsonwebtoken::{encode, decode, Header, Algorithm, Validation, EncodingKey, DecodingKey};
|
||||
use anyhow::Result;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct OAuthData {
|
||||
pub did: String,
|
||||
pub handle: String,
|
||||
pub display_name: Option<String>,
|
||||
pub avatar: Option<String>,
|
||||
pub access_jwt: Option<String>,
|
||||
pub refresh_jwt: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct OAuthCallback {
|
||||
pub code: Option<String>,
|
||||
pub state: Option<String>,
|
||||
pub error: Option<String>,
|
||||
pub error_description: Option<String>,
|
||||
pub iss: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Claims {
|
||||
pub sub: String, // DID
|
||||
pub handle: String,
|
||||
pub display_name: Option<String>,
|
||||
pub avatar: Option<String>,
|
||||
pub exp: usize,
|
||||
pub iat: usize,
|
||||
}
|
||||
|
||||
const JWT_SECRET: &[u8] = b"ailog-oauth-secret-key-2025";
|
||||
|
||||
pub fn create_jwt(oauth_data: &OAuthData) -> Result<String> {
|
||||
let now = chrono::Utc::now().timestamp() as usize;
|
||||
let claims = Claims {
|
||||
sub: oauth_data.did.clone(),
|
||||
handle: oauth_data.handle.clone(),
|
||||
display_name: oauth_data.display_name.clone(),
|
||||
avatar: oauth_data.avatar.clone(),
|
||||
exp: now + 24 * 60 * 60, // 24 hours
|
||||
iat: now,
|
||||
};
|
||||
|
||||
let token = encode(
|
||||
&Header::default(),
|
||||
&claims,
|
||||
&EncodingKey::from_secret(JWT_SECRET),
|
||||
)?;
|
||||
|
||||
Ok(token)
|
||||
}
|
||||
|
||||
pub fn verify_jwt(token: &str) -> Result<Claims> {
|
||||
let token_data = decode::<Claims>(
|
||||
token,
|
||||
&DecodingKey::from_secret(JWT_SECRET),
|
||||
&Validation::new(Algorithm::HS256),
|
||||
)?;
|
||||
|
||||
Ok(token_data.claims)
|
||||
}
|
||||
|
||||
pub async fn oauth_callback_handler(
|
||||
Query(params): Query<OAuthCallback>,
|
||||
session: Session,
|
||||
) -> Result<Html<String>, String> {
|
||||
println!("🔧 OAuth callback received: {:?}", params);
|
||||
|
||||
if let Some(error) = params.error {
|
||||
let error_html = format!(
|
||||
r#"
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>OAuth Error</title>
|
||||
<style>
|
||||
body {{ font-family: -apple-system, BlinkMacSystemFont, sans-serif; text-align: center; padding: 50px; }}
|
||||
.error {{ background: #f8d7da; color: #721c24; padding: 20px; border-radius: 8px; }}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="error">
|
||||
<h2>❌ Authentication Failed</h2>
|
||||
<p><strong>Error:</strong> {}</p>
|
||||
{}
|
||||
<button onclick="window.close()">Close Window</button>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
"#,
|
||||
error,
|
||||
params.error_description.map(|d| format!("<p><strong>Description:</strong> {}</p>", d)).unwrap_or_default()
|
||||
);
|
||||
return Ok(Html(error_html));
|
||||
}
|
||||
|
||||
if let Some(code) = params.code {
|
||||
// In a real implementation, you would exchange the code for tokens here
|
||||
// For now, we'll create a mock session
|
||||
let oauth_data = OAuthData {
|
||||
did: format!("did:plc:example_{}", &code[..8]),
|
||||
handle: "user.bsky.social".to_string(),
|
||||
display_name: Some("OAuth User".to_string()),
|
||||
avatar: Some("https://via.placeholder.com/40x40/1185fe/ffffff?text=U".to_string()),
|
||||
access_jwt: None,
|
||||
refresh_jwt: None,
|
||||
};
|
||||
|
||||
// Create JWT
|
||||
let jwt_token = create_jwt(&oauth_data).map_err(|e| e.to_string())?;
|
||||
|
||||
// Store in session
|
||||
session.insert("oauth_data", &oauth_data).await.map_err(|e| e.to_string())?;
|
||||
session.insert("jwt_token", &jwt_token).await.map_err(|e| e.to_string())?;
|
||||
|
||||
println!("✅ OAuth session created for: {}", oauth_data.handle);
|
||||
|
||||
let success_html = format!(
|
||||
r#"
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>OAuth Success</title>
|
||||
<style>
|
||||
body {{ font-family: -apple-system, BlinkMacSystemFont, sans-serif; text-align: center; padding: 50px; }}
|
||||
.success {{ background: #d1edff; color: #0c5460; padding: 20px; border-radius: 8px; }}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="success">
|
||||
<h2>✅ Authentication Successful</h2>
|
||||
<p><strong>Handle:</strong> @{}</p>
|
||||
<p><strong>DID:</strong> {}</p>
|
||||
<p>You can now close this window.</p>
|
||||
</div>
|
||||
<script>
|
||||
// Send success message to parent window
|
||||
if (window.opener && !window.opener.closed) {{
|
||||
window.opener.postMessage({{
|
||||
type: 'oauth_success',
|
||||
session: {{
|
||||
authenticated: true,
|
||||
did: '{}',
|
||||
handle: '{}',
|
||||
displayName: '{}',
|
||||
avatar: '{}',
|
||||
jwt: '{}'
|
||||
}}
|
||||
}}, window.location.origin);
|
||||
|
||||
setTimeout(() => window.close(), 2000);
|
||||
}}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
"#,
|
||||
oauth_data.handle,
|
||||
oauth_data.did,
|
||||
oauth_data.did,
|
||||
oauth_data.handle,
|
||||
oauth_data.display_name.as_deref().unwrap_or("User"),
|
||||
oauth_data.avatar.as_deref().unwrap_or(""),
|
||||
jwt_token
|
||||
);
|
||||
|
||||
return Ok(Html(success_html));
|
||||
}
|
||||
|
||||
Err("No authorization code received".to_string())
|
||||
}
|
||||
|
||||
pub async fn oauth_session_handler(session: Session) -> Json<serde_json::Value> {
|
||||
if let Ok(Some(oauth_data)) = session.get::<OAuthData>("oauth_data").await {
|
||||
if let Ok(Some(jwt_token)) = session.get::<String>("jwt_token").await {
|
||||
return Json(serde_json::json!({
|
||||
"authenticated": true,
|
||||
"user": oauth_data,
|
||||
"jwt": jwt_token
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
Json(serde_json::json!({
|
||||
"authenticated": false
|
||||
}))
|
||||
}
|
||||
|
||||
pub async fn oauth_logout_handler(session: Session) -> Json<serde_json::Value> {
|
||||
let _ = session.remove::<OAuthData>("oauth_data").await;
|
||||
let _ = session.remove::<String>("jwt_token").await;
|
||||
|
||||
Json(serde_json::json!({
|
||||
"success": true,
|
||||
"message": "Logged out successfully"
|
||||
}))
|
||||
}
|
Reference in New Issue
Block a user