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

124 lines
2.4 KiB
Go

package main
import (
"encoding/json"
"html/template"
"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/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()
app := fiber.New()
app.Use(logger.New())
app.Use(recover.New())
app.Use(redirect.New(redirect.Config{
Rules: map[string]string{
"/*/": "/$1",
},
}))
// Static content
app.Static("/public/", "./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",
"/storia": "storia.html",
}
for route, view := range actuallyStaticRoutes {
localView := view
app.Get(route, func(c *fiber.Ctx) error {
c.Type("html")
return renderer.Render(c, localView, util.H{})
})
}
app.Get("/api/utenti", func(c *fiber.Ctx) error {
utenti, err := GetUtenti()
if err != nil {
return err
}
c.Type("json")
return json.NewEncoder(c).Encode(utenti)
})
app.Get("/utenti", func(c *fiber.Ctx) error {
utenti, err := GetUtenti()
if err != nil {
return err
}
c.Type("html")
return renderer.Render(c, "utenti.html", util.H{
"Utenti": utenti,
})
})
app.Get("/appunti", func(c *fiber.Ctx) error {
searchQuery := c.Query("q", "")
c.Type("html")
return renderer.Render(c, "appunti.html", util.H{
"Query": searchQuery,
})
})
app.Get("/news", func(c *fiber.Ctx) error {
articles, err := newsArticlesRegistry.GetArticles()
3 years ago
if err != nil {
return err
3 years ago
}
c.Type("html")
return renderer.Render(c, "news.html", util.H{
"Articles": articles,
})
})
app.Get("/news/:article", func(c *fiber.Ctx) error {
articleID := c.Params("article")
article, err := newsArticlesRegistry.GetArticle(articleID)
if err != nil {
return err
}
html, err := article.Render()
if err != nil {
return err
}
c.Type("html")
return renderer.Render(c, "news-base.html", util.H{
"Article": article,
"ContentHTML": template.HTML(html),
})
3 years ago
})
app.Listen(config.Host)
}