Hello World
Crea la tua prima applicazione web con Flux
Obiettivo
In questo tutorial creeremo una semplice applicazione web che mostra "Hello World" nel browser. Imparerai i concetti base di Flux: routes, controller e template.
1. Crea un Nuovo Progetto
Apri il terminale e crea un nuovo progetto Flux:
# Crea il progetto
flux new hello-world
# Entra nella directory
cd hello-world
# Avvia il server di sviluppo
flux serve
Dovresti vedere:
π Server Flux avviato su http://localhost:8000
π Watching for changes...
β¨ Ready!
2. La Struttura del Progetto
Flux ha creato automaticamente questa struttura:
hello-world/
βββ app/
β βββ routes.flux # Definizione delle route
β βββ controllers/ # Logica dell'applicazione
β βββ models/ # Modelli database
βββ views/ # Template HTML
βββ public/ # Asset statici (CSS, JS, immagini)
βββ config/ # Configurazioni
βββ flux.json # File di configurazione progetto
3. Crea la Tua Prima Route
Apri il file app/routes.flux e aggiungi:
# app/routes.flux
route "/" {
return "Hello World!"
}
route "/hello/:name" {
return f"Hello {name}!"
}
Salva il file e visita http://localhost:8000 nel browser. Vedrai "Hello World!"
4. Aggiungi un Template HTML
Creiamo un template piΓΉ elaborato. Prima modifica la route:
# app/routes.flux
route "/" {
return render("home", {
"title": "La mia prima app Flux",
"message": "Hello World!"
})
}
Ora crea il file views/home.flux.html:
<!-- views/home.flux.html -->
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.container {
text-align: center;
background: white;
padding: 3rem;
border-radius: 10px;
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}
h1 {
color: #667eea;
margin: 0;
}
p {
color: #666;
margin-top: 1rem;
}
</style>
</head>
<body>
<div class="container">
<h1>{{ message }}</h1>
<p>Benvenuto in {{ title }}</p>
<p>π Flux Γ¨ attivo e funzionante!</p>
</div>
</body>
</html>
5. Aggiungi InterattivitΓ
Rendiamo l'app piΓΉ dinamica con una route che accetta parametri:
# app/routes.flux
route "/greet/:name" {
hour = datetime.now().hour
if hour < 12:
greeting = "Buongiorno"
elif hour < 18:
greeting = "Buon pomeriggio"
else:
greeting = "Buonasera"
return render("greet", {
"name": name,
"greeting": greeting,
"time": datetime.now().strftime("%H:%M")
})
}
Crea views/greet.flux.html:
<!-- views/greet.flux.html -->
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<title>Saluto Personalizzato</title>
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<div class="container">
<h1>{{ greeting }}, {{ name }}!</h1>
<p>Sono le {{ time }}</p>
<a href="/">Torna alla home</a>
</div>
</body>
</html>
Visita http://localhost:8000/greet/Mario per vedere il saluto personalizzato!
6. Gestione degli Errori
Flux gestisce automaticamente gli errori 404. Puoi personalizzarli:
# app/routes.flux
route.error(404) {
return render("errors/404", {
"message": "Pagina non trovata"
})
}
7. Prossimi Passi
Congratulazioni! Hai creato la tua prima app Flux. Ecco cosa puoi esplorare dopo:
Codice Completo
Ecco il codice completo dell'applicazione Hello World:
# app/routes.flux
route "/" {
return render("home", {
"title": "La mia prima app Flux",
"message": "Hello World!"
})
}
route "/greet/:name" {
hour = datetime.now().hour
if hour < 12:
greeting = "Buongiorno"
elif hour < 18:
greeting = "Buon pomeriggio"
else:
greeting = "Buonasera"
return render("greet", {
"name": name,
"greeting": greeting,
"time": datetime.now().strftime("%H:%M")
})
}
route.error(404) {
return render("errors/404", {
"message": "Pagina non trovata"
})
}
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<title>{{ title }}</title>
<link rel="stylesheet" href="/css/style.css">
</head>
<body>
<div class="container">
<h1>{{ message }}</h1>
<p>Benvenuto in {{ title }}</p>
<p>π Flux Γ¨ attivo e funzionante!</p>
<div class="links">
<a href="/greet/Visitatore">Saluto personalizzato</a>
</div>
</div>
</body>
</html>
/* public/css/style.css */
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.container {
text-align: center;
background: white;
padding: 3rem;
border-radius: 10px;
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
max-width: 500px;
}
h1 {
color: #667eea;
margin: 0 0 1rem 0;
}
p {
color: #666;
margin: 0.5rem 0;
}
.links {
margin-top: 2rem;
}
a {
color: #667eea;
text-decoration: none;
padding: 0.5rem 1rem;
border: 2px solid #667eea;
border-radius: 5px;
transition: all 0.3s;
}
a:hover {
background: #667eea;
color: white;
}