forked from D13GOOOO/BlogEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
163 lines (137 loc) · 4.03 KB
/
main.go
File metadata and controls
163 lines (137 loc) · 4.03 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
package main
import (
"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
}
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(&Post{})
if err != nil {
log.Fatal("Errore nella migrazione:", 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,
})
}
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)
}
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
}
func main() {
// Inizializza il database e il validatore
initDB()
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)
log.Println("Server avviato su http://localhost:8080")
open("http://localhost:8080")
http.ListenAndServe(":8080", r)
}