feat: initial commit - Rust + React URL shortener
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Hello Web - URL Shortener</title>
|
||||
<style>
|
||||
* { margin: 0; padding: 0; box-sizing: border-box; }
|
||||
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #0f172a; color: #e2e8f0; }
|
||||
.container { max-width: 900px; margin: 0 auto; padding: 2rem; }
|
||||
h1 { text-align: center; font-size: 2.5rem; margin-bottom: 0.5rem; background: linear-gradient(135deg, #667eea, #764ba2); -webkit-background-clip: text; -webkit-text-fill-color: transparent; }
|
||||
.subtitle { text-align: center; color: #94a3b8; margin-bottom: 2rem; }
|
||||
.card { background: #1e293b; border-radius: 12px; padding: 1.5rem; margin-bottom: 1.5rem; box-shadow: 0 4px 6px rgba(0,0,0,0.3); }
|
||||
.input-group { display: flex; gap: 0.5rem; margin-bottom: 1rem; }
|
||||
input { flex: 1; padding: 0.75rem 1rem; border: 2px solid #334155; border-radius: 8px; background: #0f172a; color: #e2e8f0; font-size: 1rem; outline: none; transition: border-color 0.2s; }
|
||||
input:focus { border-color: #667eea; }
|
||||
button { padding: 0.75rem 1.5rem; background: linear-gradient(135deg, #667eea, #764ba2); color: white; border: none; border-radius: 8px; font-size: 1rem; font-weight: 600; cursor: pointer; transition: transform 0.1s, opacity 0.2s; }
|
||||
button:hover { opacity: 0.9; transform: translateY(-1px); }
|
||||
button:active { transform: translateY(0); }
|
||||
.result { padding: 1rem; background: #0f172a; border-radius: 8px; margin-top: 1rem; word-break: break-all; }
|
||||
.result a { color: #667eea; text-decoration: none; }
|
||||
.result a:hover { text-decoration: underline; }
|
||||
.error { color: #f87171; margin-top: 0.5rem; }
|
||||
.stats-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; margin-bottom: 1.5rem; }
|
||||
.stat-card { background: #0f172a; padding: 1rem; border-radius: 8px; text-align: center; }
|
||||
.stat-value { font-size: 2rem; font-weight: 700; color: #667eea; }
|
||||
.stat-label { color: #94a3b8; font-size: 0.875rem; margin-top: 0.25rem; }
|
||||
table { width: 100%; border-collapse: collapse; }
|
||||
th, td { padding: 0.75rem; text-align: left; border-bottom: 1px solid #334155; }
|
||||
th { color: #94a3b8; font-weight: 600; font-size: 0.875rem; text-transform: uppercase; }
|
||||
td a { color: #667eea; text-decoration: none; }
|
||||
td a:hover { text-decoration: underline; }
|
||||
.badge { display: inline-block; padding: 0.25rem 0.5rem; background: #667eea22; color: #667eea; border-radius: 4px; font-size: 0.875rem; }
|
||||
.tabs { display: flex; gap: 0.5rem; margin-bottom: 1.5rem; }
|
||||
.tab { padding: 0.5rem 1rem; background: #0f172a; border: 2px solid #334155; border-radius: 8px; cursor: pointer; transition: all 0.2s; }
|
||||
.tab.active { border-color: #667eea; background: #667eea22; }
|
||||
.custom-alias { margin-bottom: 1rem; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.jsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,10 @@
|
||||
import React from 'react'
|
||||
import ReactDOM from 'react-dom/client'
|
||||
import App from './App'
|
||||
import './styles.css'
|
||||
|
||||
ReactDOM.createRoot(document.getElementById('root')).render(
|
||||
<React.StrictMode>
|
||||
<App />
|
||||
</React.StrictMode>,
|
||||
)
|
||||
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"name": "hello-web-frontend",
|
||||
"private": true,
|
||||
"version": "0.1.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"react": "^18.2.0",
|
||||
"react-dom": "^18.2.0",
|
||||
"axios": "^1.6.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "^18.2.0",
|
||||
"@types/react-dom": "^18.2.0",
|
||||
"@vitejs/plugin-react": "^4.2.0",
|
||||
"vite": "^5.0.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,217 @@
|
||||
import React, { useState, useEffect } from 'react'
|
||||
import axios from 'axios'
|
||||
|
||||
const API_BASE = '/api'
|
||||
|
||||
function App() {
|
||||
const [url, setUrl] = useState('')
|
||||
const [alias, setAlias] = useState('')
|
||||
const [result, setResult] = useState(null)
|
||||
const [error, setError] = useState('')
|
||||
const [urls, setUrls] = useState([])
|
||||
const [stats, setStats] = useState(null)
|
||||
const [tab, setTab] = useState('create')
|
||||
|
||||
useEffect(() => {
|
||||
if (tab === 'list') fetchUrls()
|
||||
if (tab === 'stats') fetchStats()
|
||||
}, [tab])
|
||||
|
||||
const fetchUrls = async () => {
|
||||
try {
|
||||
const res = await axios.get(`${API_BASE}/urls`)
|
||||
setUrls(res.data)
|
||||
} catch (e) {
|
||||
console.error('Failed to fetch URLs:', e)
|
||||
}
|
||||
}
|
||||
|
||||
const fetchStats = async () => {
|
||||
try {
|
||||
const res = await axios.get(`${API_BASE}/stats`)
|
||||
setStats(res.data)
|
||||
} catch (e) {
|
||||
console.error('Failed to fetch stats:', e)
|
||||
}
|
||||
}
|
||||
|
||||
const shorten = async (e) => {
|
||||
e.preventDefault()
|
||||
setError('')
|
||||
setResult(null)
|
||||
|
||||
if (!url) {
|
||||
setError('Please enter a URL')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await axios.post(`${API_BASE}/shorten`, {
|
||||
url,
|
||||
custom_alias: alias || undefined,
|
||||
})
|
||||
setResult(res.data)
|
||||
setUrl('')
|
||||
setAlias('')
|
||||
fetchUrls()
|
||||
fetchStats()
|
||||
} catch (e) {
|
||||
setError(e.response?.data || 'Failed to shorten URL')
|
||||
}
|
||||
}
|
||||
|
||||
const copyToClipboard = (text) => {
|
||||
navigator.clipboard.writeText(text)
|
||||
alert('Copied to clipboard!')
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="container">
|
||||
<h1>🔗 Hello Web</h1>
|
||||
<p className="subtitle">URL Shortener built with Rust + React</p>
|
||||
|
||||
<div className="tabs">
|
||||
<div className={tab === 'create' ? 'tab active' : 'tab'} onClick={() => setTab('create')}>
|
||||
Create
|
||||
</div>
|
||||
<div className={tab === 'list' ? 'tab active' : 'tab'} onClick={() => setTab('list')}>
|
||||
All URLs
|
||||
</div>
|
||||
<div className={tab === 'stats' ? 'tab active' : 'tab'} onClick={() => setTab('stats')}>
|
||||
Stats
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{tab === 'create' && (
|
||||
<div className="card">
|
||||
<form onSubmit={shorten}>
|
||||
<div className="input-group">
|
||||
<input
|
||||
type="url"
|
||||
placeholder="Enter your long URL (e.g., https://example.com/very/long/path)"
|
||||
value={url}
|
||||
onChange={(e) => setUrl(e.target.value)}
|
||||
/>
|
||||
<button type="submit">Shorten</button>
|
||||
</div>
|
||||
<div className="custom-alias">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Optional custom alias (3-20 chars)"
|
||||
value={alias}
|
||||
onChange={(e) => setAlias(e.target.value)}
|
||||
style={{ width: '100%' }}
|
||||
/>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
{error && <div className="error">❌ {error}</div>}
|
||||
|
||||
{result && (
|
||||
<div className="result">
|
||||
<p><strong>Short URL:</strong></p>
|
||||
<a href={result.short_url} target="_blank" rel="noopener noreferrer">
|
||||
{result.short_url}
|
||||
</a>
|
||||
<p><small>Original: {result.original_url}</small></p>
|
||||
<button onClick={() => copyToClipboard(result.short_url)} style={{ marginTop: '0.5rem' }}>
|
||||
📋 Copy
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{tab === 'list' && (
|
||||
<div className="card">
|
||||
{urls.length === 0 ? (
|
||||
<p style={{ textAlign: 'center', color: '#94a3b8' }}>No URLs created yet</p>
|
||||
) : (
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Short URL</th>
|
||||
<th>Original URL</th>
|
||||
<th>Clicks</th>
|
||||
<th>Created</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{urls.map((u) => (
|
||||
<tr key={u.id}>
|
||||
<td>
|
||||
<a href={u.short_url || `/${u.id}`} target="_blank" rel="noopener noreferrer">
|
||||
{u.id}
|
||||
</a>
|
||||
</td>
|
||||
<td>
|
||||
<a href={u.original_url} target="_blank" rel="noopener noreferrer">
|
||||
{u.original_url}
|
||||
</a>
|
||||
</td>
|
||||
<td><span className="badge">{u.click_count}</span></td>
|
||||
<td>{new Date(u.created_at).toLocaleDateString()}</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{tab === 'stats' && stats && (
|
||||
<div className="card">
|
||||
<div className="stats-grid">
|
||||
<div className="stat-card">
|
||||
<div className="stat-value">{stats.total_urls}</div>
|
||||
<div className="stat-label">Total URLs</div>
|
||||
</div>
|
||||
<div className="stat-card">
|
||||
<div className="stat-value">{stats.total_clicks}</div>
|
||||
<div className="stat-label">Total Clicks</div>
|
||||
</div>
|
||||
<div className="stat-card">
|
||||
<div className="stat-value">
|
||||
{stats.total_urls > 0
|
||||
? Math.round(stats.total_clicks / stats.total_urls)
|
||||
: 0}
|
||||
</div>
|
||||
<div className="stat-label">Avg Clicks/URL</div>
|
||||
</div>
|
||||
</div>
|
||||
<h3 style={{ marginBottom: '1rem' }}>🏆 Top URLs</h3>
|
||||
{stats.top_urls.length === 0 ? (
|
||||
<p style={{ color: '#94a3b8' }}>No data yet</p>
|
||||
) : (
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Clicks</th>
|
||||
<th>Original URL</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{stats.top_urls.map((u) => (
|
||||
<tr key={u.id}>
|
||||
<td>{u.id}</td>
|
||||
<td><span className="badge">{u.click_count}</span></td>
|
||||
<td>
|
||||
<a href={u.original_url} target="_blank" rel="noopener noreferrer">
|
||||
{u.original_url.length > 40
|
||||
? u.original_url.slice(0, 40) + '...'
|
||||
: u.original_url}
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
export default App
|
||||
@@ -0,0 +1,9 @@
|
||||
import React from 'react'
|
||||
import ReactDOM from 'react-dom/client'
|
||||
import App from './App'
|
||||
|
||||
ReactDOM.createRoot(document.getElementById('root')).render(
|
||||
<React.StrictMode>
|
||||
<App />
|
||||
</React.StrictMode>,
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
import { defineConfig } from 'vite'
|
||||
import react from '@vitejs/plugin-react'
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
server: {
|
||||
port: 3000,
|
||||
proxy: {
|
||||
'/api': 'http://localhost:8080',
|
||||
},
|
||||
},
|
||||
})
|
||||
Reference in New Issue
Block a user