Refactor: Le route statiche ora sono aggiunte tutte insieme a blocco

main-old
Antonio De Lucreziis 3 years ago
parent 695720e44c
commit 99371a1688

@ -34,29 +34,24 @@ func main() {
newsArticlesRegistry := NewArticleRenderer("./news")
// Routes
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
err := renderer.Render(w, "home.html", util.H{})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
r.Get("/link", func(w http.ResponseWriter, r *http.Request) {
err := renderer.Render(w, "link.html", util.H{})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
r.Get("/utenti", func(w http.ResponseWriter, r *http.Request) {
err := renderer.Render(w, "utenti.html", util.H{})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
actuallyStaticRoutes := map[string]string{
"/": "home.html",
"/link": "link.html",
"/utenti": "utenti.html",
"/login": "login.html",
}
for route, view := range actuallyStaticRoutes {
localView := view
r.Get(route, func(w http.ResponseWriter, r *http.Request) {
err := renderer.Render(w, localView, util.H{})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
}
r.Get("/news", func(w http.ResponseWriter, r *http.Request) {
articles, err := newsArticlesRegistry.LoadAll()
@ -97,14 +92,6 @@ func main() {
}
})
r.Get("/login", func(w http.ResponseWriter, r *http.Request) {
err := renderer.Render(w, "login.html", util.H{})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
log.Printf(`Starting server on %v...`, config.Host)
http.ListenAndServe(config.Host, r)
}

Loading…
Cancel
Save