|
|
|
@ -3,29 +3,32 @@ 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"
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/logger"
|
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/recover"
|
|
|
|
|
"github.com/gofiber/redirect/v2"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
config.Load()
|
|
|
|
|
|
|
|
|
|
r := chi.NewRouter()
|
|
|
|
|
app := fiber.New()
|
|
|
|
|
|
|
|
|
|
// Middleware
|
|
|
|
|
r.Use(middleware.Logger)
|
|
|
|
|
r.Use(middleware.Recoverer)
|
|
|
|
|
r.Use(middleware.RedirectSlashes)
|
|
|
|
|
app.Use(logger.New())
|
|
|
|
|
app.Use(recover.New())
|
|
|
|
|
app.Use(redirect.New(redirect.Config{
|
|
|
|
|
Rules: map[string]string{
|
|
|
|
|
"/*/": "/$1",
|
|
|
|
|
},
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
// Static content
|
|
|
|
|
r.Handle("/public/*", http.StripPrefix("/public", http.FileServer(http.Dir("./public"))))
|
|
|
|
|
app.Static("/public/", "./public")
|
|
|
|
|
|
|
|
|
|
// Templates & Renderer
|
|
|
|
|
renderer := templates.NewRenderer(
|
|
|
|
@ -47,99 +50,74 @@ func main() {
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
app.Get(route, func(c *fiber.Ctx) error {
|
|
|
|
|
c.Type("html")
|
|
|
|
|
return renderer.Render(c, localView, util.H{})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r.Get("/api/utenti", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
app.Get("/api/utenti", func(c *fiber.Ctx) error {
|
|
|
|
|
utenti, err := GetUtenti()
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := json.NewEncoder(w).Encode(utenti); err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.Type("json")
|
|
|
|
|
return json.NewEncoder(c).Encode(utenti)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
r.Get("/utenti", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
app.Get("/utenti", func(c *fiber.Ctx) error {
|
|
|
|
|
utenti, err := GetUtenti()
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := renderer.Render(w, "utenti.html", util.H{
|
|
|
|
|
c.Type("html")
|
|
|
|
|
return renderer.Render(c, "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 := ""
|
|
|
|
|
app.Get("/appunti", func(c *fiber.Ctx) error {
|
|
|
|
|
searchQuery := c.Query("q", "")
|
|
|
|
|
|
|
|
|
|
keys, present := r.URL.Query()["q"]
|
|
|
|
|
if present {
|
|
|
|
|
searchQuery = keys[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := renderer.Render(w, "appunti.html", util.H{
|
|
|
|
|
c.Type("html")
|
|
|
|
|
return renderer.Render(c, "appunti.html", util.H{
|
|
|
|
|
"Query": searchQuery,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
r.Get("/news", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
app.Get("/news", func(c *fiber.Ctx) error {
|
|
|
|
|
articles, err := newsArticlesRegistry.GetArticles()
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := renderer.Render(w, "news.html", util.H{
|
|
|
|
|
c.Type("html")
|
|
|
|
|
return renderer.Render(c, "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")
|
|
|
|
|
app.Get("/news/:article", func(c *fiber.Ctx) error {
|
|
|
|
|
articleID := c.Params("article")
|
|
|
|
|
|
|
|
|
|
article, err := newsArticlesRegistry.GetArticle(articleID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
html, err := article.Render()
|
|
|
|
|
if err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := renderer.Render(w, "news-base.html", util.H{
|
|
|
|
|
c.Type("html")
|
|
|
|
|
return renderer.Render(c, "news-base.html", util.H{
|
|
|
|
|
"Article": article,
|
|
|
|
|
"ContentHTML": template.HTML(html),
|
|
|
|
|
}); err != nil {
|
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
log.Printf(`Starting server on %v...`, config.Host)
|
|
|
|
|
http.ListenAndServe(config.Host, r)
|
|
|
|
|
app.Listen(config.Host)
|
|
|
|
|
}
|
|
|
|
|