73 lines
1.5 KiB
Go
73 lines
1.5 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
|
||
|
"github.com/gofiber/fiber/v2"
|
||
|
|
||
|
"github.com/gofiber/template/html"
|
||
|
|
||
|
"codeberg.org/Odyssium/gothub/pages"
|
||
|
|
||
|
"github.com/gofiber/fiber/v2/middleware/cache"
|
||
|
|
||
|
"time"
|
||
|
|
||
|
"github.com/gofiber/fiber/v2/middleware/proxy"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
engine := html.New("./views", ".html")
|
||
|
|
||
|
app := fiber.New(fiber.Config{
|
||
|
Views: engine,
|
||
|
Prefork: true,
|
||
|
ErrorHandler: func(ctx *fiber.Ctx, err error) error {
|
||
|
code := fiber.StatusInternalServerError
|
||
|
|
||
|
if e, ok := err.(*fiber.Error); ok {
|
||
|
code = e.Code
|
||
|
}
|
||
|
|
||
|
err = ctx.Status(code).Render("error", fiber.Map{
|
||
|
"error": err,
|
||
|
})
|
||
|
if err != nil {
|
||
|
return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
},
|
||
|
})
|
||
|
|
||
|
app.Use(cache.New(cache.Config{
|
||
|
Next: func(c *fiber.Ctx) bool {
|
||
|
return c.Query("refresh") == "true"
|
||
|
},
|
||
|
Expiration: 5 * time.Minute,
|
||
|
CacheControl: true,
|
||
|
}))
|
||
|
|
||
|
app.Get("/", func(c *fiber.Ctx) error {
|
||
|
return c.Render("index", fiber.Map{
|
||
|
"host": c.Hostname(),
|
||
|
})
|
||
|
})
|
||
|
|
||
|
app.Static("/css", "./public/css")
|
||
|
app.Static("/fonts", "./public/fonts")
|
||
|
app.Static("/robots.txt", "./public/robots.txt")
|
||
|
app.Get("/explore", pages.HandleExplore)
|
||
|
app.Get("/:user", pages.HandleUser)
|
||
|
app.Get("/avatar/:id", func(c *fiber.Ctx) error {
|
||
|
url := "https://avatars.githubusercontent.com/u/" + c.Params("id") + "?v=4"
|
||
|
if err := proxy.Do(c, url); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
// Remove Server header from response
|
||
|
c.Response().Header.Del(fiber.HeaderServer)
|
||
|
return nil
|
||
|
})
|
||
|
log.Fatal(app.Listen(":3000"))
|
||
|
}
|