You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
website/main.go

145 lines
3.3 KiB
Go

package main
import (
"encoding/json"
"html/template"
"log"
"net/http"
"git.phc.dm.unipi.it/phc/website/articles"
"git.phc.dm.unipi.it/phc/website/config"
"git.phc.dm.unipi.it/phc/website/templates"
"git.phc.dm.unipi.it/phc/website/util"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
)
func main() {
config.Load()
r := chi.NewRouter()
// Middleware
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(middleware.RedirectSlashes)
// Static content
r.Handle("/public/*", http.StripPrefix("/public", http.FileServer(http.Dir("./public"))))
// Templates & Renderer
renderer := templates.NewRenderer(
"./views/",
"./views/base.html",
"./views/partials/*.html",
)
newsArticlesRegistry := articles.NewRegistry("./news")
// Routes
actuallyStaticRoutes := map[string]string{
"/": "home.html",
"/link": "link.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("/api/utenti", func(w http.ResponseWriter, r *http.Request) {
utenti, err := GetUtenti()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := json.NewEncoder(w).Encode(utenti); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
r.Get("/utenti", func(w http.ResponseWriter, r *http.Request) {
utenti, err := GetUtenti()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := renderer.Render(w, "utenti.html", util.H{
"Utenti": utenti,
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
r.Get("/appunti", func(w http.ResponseWriter, r *http.Request) {
searchQuery := ""
keys, present := r.URL.Query()["q"]
if present {
searchQuery = keys[0]
}
err := renderer.Render(w, "appunti.html", util.H{
"Query": searchQuery,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
3 years ago
r.Get("/news", func(w http.ResponseWriter, r *http.Request) {
articles, err := newsArticlesRegistry.GetArticles()
3 years ago
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := renderer.Render(w, "news.html", util.H{
"Articles": articles,
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
r.Get("/news/{article}", func(w http.ResponseWriter, r *http.Request) {
articleID := chi.URLParam(r, "article")
article, err := newsArticlesRegistry.GetArticle(articleID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
html, err := article.Render()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := renderer.Render(w, "news-base.html", util.H{
"Article": article,
"ContentHTML": template.HTML(html),
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
3 years ago
})
log.Printf(`Starting server on %v...`, config.Host)
http.ListenAndServe(config.Host, r)
}