2022-11-06 01:04:34 +05:30
|
|
|
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"
|
2022-11-29 22:29:04 +05:30
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/compress"
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/recover"
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/limiter"
|
|
|
|
|
|
|
|
"html/template"
|
2022-11-06 01:04:34 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
engine := html.New("./views", ".html")
|
|
|
|
|
2022-11-29 22:29:04 +05:30
|
|
|
engine.AddFunc(
|
|
|
|
// add unescape function
|
|
|
|
"unescape", func(s string) template.HTML {
|
|
|
|
return template.HTML(s)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2022-11-06 01:04:34 +05:30
|
|
|
app := fiber.New(fiber.Config{
|
|
|
|
Views: engine,
|
|
|
|
Prefork: true,
|
2022-11-29 22:29:04 +05:30
|
|
|
AppName: "GotHub",
|
2022-11-06 01:04:34 +05:30
|
|
|
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{
|
2022-11-29 22:29:04 +05:30
|
|
|
Expiration: 5 * time.Minute,
|
2022-11-06 01:04:34 +05:30
|
|
|
}))
|
|
|
|
|
|
|
|
app.Get("/", func(c *fiber.Ctx) error {
|
|
|
|
return c.Render("index", fiber.Map{
|
|
|
|
"host": c.Hostname(),
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2022-11-29 22:29:04 +05:30
|
|
|
app.Use(compress.New(compress.Config{
|
|
|
|
Level: compress.LevelBestSpeed, // 1
|
|
|
|
}))
|
|
|
|
|
|
|
|
app.Use(recover.New())
|
|
|
|
|
|
|
|
ratelimiter := limiter.New(limiter.Config{
|
|
|
|
Max: 5,
|
|
|
|
Expiration: 5 * time.Minute,
|
|
|
|
LimitReached: func(c *fiber.Ctx) error {
|
|
|
|
return c.Status(429).Render("ratelimit_gt", fiber.Map{
|
|
|
|
"Title": "Rate limit exceeded",
|
|
|
|
})
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2022-11-06 01:04:34 +05:30
|
|
|
app.Static("/css", "./public/css")
|
|
|
|
app.Static("/fonts", "./public/fonts")
|
|
|
|
app.Static("/robots.txt", "./public/robots.txt")
|
2022-11-29 22:29:04 +05:30
|
|
|
app.Static("/favicon.ico", "./public/assets/favicon.ico")
|
|
|
|
app.Static("/logo.svg", "./public/assets/logo.svg")
|
|
|
|
app.Get("/explore", ratelimiter, pages.HandleExplore)
|
|
|
|
app.Get("/:user", ratelimiter, pages.HandleUser)
|
2022-11-06 01:04:34 +05:30
|
|
|
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"))
|
|
|
|
}
|