-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
237 lines (200 loc) · 5.93 KB
/
main.go
File metadata and controls
237 lines (200 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
import (
"blog/models"
"fmt"
"html/template"
"log"
"net/http"
"os/exec"
"runtime"
"strconv"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-playground/validator/v10"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
_ "modernc.org/sqlite" // questo è importante per usare sqlite senza CGO
)
var ( // variabili globali
// db è il puntatore al database
db *gorm.DB
validate *validator.Validate
funcMap = template.FuncMap{
"add": func(a, b int) int { return a + b },
"sub": func(a, b int) int { return a - b },
}
)
type Post struct { // struttura del post
ID uint `gorm:"primaryKey"`
Title string `validate:"required"`
Content string `validate:"required"`
Date time.Time
}
var templates *template.Template
func initDB() { // funzione che inizializza il database
var err error
db, err = gorm.Open(sqlite.Open("blog.db"), &gorm.Config{})
if err != nil {
log.Fatal("Errore apertura DB:", err)
}
err = db.AutoMigrate(&models.Post{}, &models.Comment{})
if err != nil {
log.Fatal("Errore nella migrazione:", err)
}
}
func initTemplates() {
// Carica tutti i template HTML nella cartella "templates"
var err error
templates, err = template.New("base").Funcs(funcMap).ParseFiles(
"templates/home.html",
"templates/new.html",
"templates/post.html", // Aggiungi anche il template post.html
)
if err != nil {
log.Fatal("Errore nel caricamento dei template:", err)
}
}
func homeHandler(w http.ResponseWriter, r *http.Request) { // funzione che gestisce la pagina principale
// Controlla se il metodo è GET
tmpl := template.Must(template.New("home.html").Funcs(funcMap).ParseFiles("templates/home.html"))
pageParam := r.URL.Query().Get("page")
if pageParam == "" {
pageParam = "1"
}
page, err := strconv.Atoi(pageParam)
if err != nil || page < 1 {
page = 1
}
postsPerPage := 6
var posts []Post
// Recupera post ordinati per data decrescente
result := db.Order("date DESC").Offset((page - 1) * postsPerPage).Limit(postsPerPage).Find(&posts)
if result.Error != nil {
http.Error(w, "Errore nel recupero dei post", http.StatusInternalServerError)
return
}
// Conta il numero totale di post
var total int64
db.Model(&Post{}).Count(&total)
totalPages := int((total + int64(postsPerPage) - 1) / int64(postsPerPage))
if totalPages == 0 {
totalPages = 1
}
tmpl.Execute(w, struct {
Posts []Post
CurrentPage int
TotalPages int
}{
Posts: posts,
CurrentPage: page,
TotalPages: totalPages,
})
}
// POST HANDLER
func newPostFormHandler(w http.ResponseWriter, r *http.Request) {
tmpl := template.Must(template.New("new.html").Funcs(funcMap).ParseFiles("templates/new.html"))
tmpl.Execute(w, nil)
}
func createPostHandler(w http.ResponseWriter, r *http.Request) { // funzione che gestisce la creazione di un nuovo post
// Controlla se il metodo è POST
err := r.ParseForm()
if err != nil {
http.Error(w, "Errore nel parsing del form", http.StatusBadRequest)
return
}
newPost := Post{
Title: r.FormValue("title"),
Content: r.FormValue("content"),
Date: time.Now(),
}
err = validate.Struct(newPost)
if err != nil {
http.Error(w, "Titolo e contenuto sono obbligatori", http.StatusBadRequest)
return
}
result := db.Create(&newPost)
if result.Error != nil {
http.Error(w, "Errore nel salvataggio del post", http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}
// HANDLER COMMENTI
func showPostHandler(w http.ResponseWriter, r *http.Request) {
idStr := chi.URLParam(r, "id")
id, err := strconv.Atoi(idStr)
if err != nil {
http.Error(w, "ID del post non valido", http.StatusBadRequest)
return
}
var post models.Post
if err := db.Preload("Comments").First(&post, id).Error; err != nil {
http.NotFound(w, r)
return
}
err = templates.ExecuteTemplate(w, "post.html", post)
if err != nil {
http.Error(w, "Errore rendering template", http.StatusInternalServerError)
}
}
func addCommentHandler(w http.ResponseWriter, r *http.Request) {
idStr := chi.URLParam(r, "id")
id, err := strconv.Atoi(idStr)
if err != nil {
http.Error(w, "ID del post non valido", http.StatusBadRequest)
return
}
if err := r.ParseForm(); err != nil {
http.Error(w, "Errore nei dati del form", http.StatusBadRequest)
return
}
comment := models.Comment{
PostID: uint(id),
Author: r.FormValue("author"),
Content: r.FormValue("content"),
CreatedAt: time.Now(),
}
if err := db.Create(&comment).Error; err != nil {
http.Error(w, "Errore salvataggio commento", http.StatusInternalServerError)
return
}
http.Redirect(w, r, fmt.Sprintf("/post/%d", id), http.StatusSeeOther)
}
// REINDIRIZZAMENTO
func open(url string) error { //funzione che permette di aprire un link nel browser
var cmd string
var param []string
switch runtime.GOOS { //prende il valore del sistema operativo
case "windows":
cmd = "cmd"
param = []string{"/c", "start"}
case "darwin": //macos
cmd = "open"
default: //linux e similari
cmd = "xdg-open"
}
param = append(param, url)
return exec.Command(cmd, param...).Start() //esegue il comando
}
// MAIN
func main() {
// Inizializza il database e il validatore
initDB()
initTemplates()
validate = validator.New()
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
fs := http.StripPrefix("/static/", http.FileServer(http.Dir("static")))
r.Handle("/static/*", fs)
r.Get("/", homeHandler)
r.Get("/new", newPostFormHandler)
r.Post("/create", createPostHandler)
// Rotte per i singoli post e commenti
r.Get("/post/{id}", showPostHandler)
r.Post("/post/{id}/comment", addCommentHandler)
log.Println("Server avviato su http://localhost:8080")
open("http://localhost:8080")
http.ListenAndServe(":8080", r)
}