mirror of
https://codeberg.org/gothub/gothub
synced 2024-12-06 19:16:24 +01:00
cleanup serve/serve.go a bit
Signed-off-by: Odyssey <hi@odyssey346.dev>
This commit is contained in:
@@ -14,6 +14,7 @@ require (
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/alecthomas/chroma v0.10.0 // indirect
|
||||
github.com/andybalholm/brotli v1.0.4 // indirect
|
||||
github.com/aymerick/douceur v0.2.0 // indirect
|
||||
github.com/dlclark/regexp2 v1.7.0 // indirect
|
||||
|
||||
@@ -55,6 +55,8 @@ github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKz
|
||||
github.com/Joker/jade v1.1.3/go.mod h1:T+2WLyt7VH6Lp0TRxQrUYEs64nRc83wkMQrfeIQKduM=
|
||||
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
|
||||
github.com/alecthomas/assert/v2 v2.2.0 h1:f6L/b7KE2bfA+9O4FL3CM/xJccDEwPVYd5fALBiuwvw=
|
||||
github.com/alecthomas/chroma v0.10.0 h1:7XDcGkCQopCNKjZHfYrNLraA+M7e0fMiJ/Mfikbfjek=
|
||||
github.com/alecthomas/chroma v0.10.0/go.mod h1:jtJATyUxlIORhUOFNA9NZDWGAQ8wpxQQqNSB4rjA/1s=
|
||||
github.com/alecthomas/chroma/v2 v2.4.0 h1:Loe2ZjT5x3q1bcWwemqyqEi8p11/IV/ncFCeLYDpWC4=
|
||||
github.com/alecthomas/chroma/v2 v2.4.0/go.mod h1:6kHzqF5O6FUSJzBXW7fXELjb+e+7OXW4UpoPqMO7IBQ=
|
||||
github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE=
|
||||
@@ -108,6 +110,7 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
|
||||
github.com/dlclark/regexp2 v1.7.0 h1:7lJfhqlPssTb1WQx4yvTHN0uElPEv52sbaECrAQxjAo=
|
||||
github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
|
||||
github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM=
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package pages
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"html/template"
|
||||
|
||||
"github.com/alecthomas/chroma/lexers"
|
||||
"github.com/alecthomas/chroma/styles"
|
||||
htmlfmt "github.com/alecthomas/chroma/v2/formatters/html"
|
||||
"github.com/carlmjohnson/requests"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
// FileView is the file view page
|
||||
func FileView(c *fiber.Ctx) error {
|
||||
var file string
|
||||
url := "https://raw.githubusercontent.com/" + c.Params("user") + "/" + c.Params("repo") + "/" + c.Params("branch") + "/" + c.Params("file")
|
||||
err := requests.
|
||||
URL(url).
|
||||
ToString(&file).
|
||||
Fetch(context.Background())
|
||||
if err != nil {
|
||||
return c.Status(404).Render("error", fiber.Map{
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
|
||||
lexer := lexers.Match(c.Params("file"))
|
||||
if lexer == nil {
|
||||
lexer = lexers.Fallback
|
||||
}
|
||||
|
||||
formatter := htmlfmt.New(
|
||||
htmlfmt.WithClasses(true),
|
||||
htmlfmt.WithLineNumbers(true),
|
||||
htmlfmt.PreventSurroundingPre(false),
|
||||
)
|
||||
|
||||
buf := bytes.Buffer{}
|
||||
writer := bufio.NewWriter(&buf)
|
||||
style := styles.Get("native")
|
||||
|
||||
tokens, err := lexer.Tokenise(nil, file)
|
||||
if err != nil {
|
||||
return c.Status(500).Render("error", fiber.Map{
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
formatter.Format(writer, style, tokens)
|
||||
|
||||
cssbuf := bytes.Buffer{}
|
||||
cssw := bufio.NewWriter(&cssbuf)
|
||||
formatter.WriteCSS(cssw, style)
|
||||
|
||||
writer.Flush()
|
||||
cssw.Flush()
|
||||
|
||||
return c.Render("file", fiber.Map{
|
||||
"file": template.HTML(buf.String()),
|
||||
"css": template.CSS(cssbuf.String()),
|
||||
"fullname": c.Params("user") + "/" + c.Params("repo"),
|
||||
"name": c.Params("file"),
|
||||
"branch": c.Params("branch"),
|
||||
})
|
||||
}
|
||||
+3
-64
@@ -1,9 +1,6 @@
|
||||
package serve
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"html/template"
|
||||
"log"
|
||||
"os"
|
||||
@@ -12,11 +9,6 @@ import (
|
||||
|
||||
"codeberg.org/gothub/gothub/pages"
|
||||
"codeberg.org/gothub/gothub/utils"
|
||||
"github.com/carlmjohnson/requests"
|
||||
|
||||
htmlfmt "github.com/alecthomas/chroma/v2/formatters/html"
|
||||
"github.com/alecthomas/chroma/v2/lexers"
|
||||
"github.com/alecthomas/chroma/v2/styles"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/cache"
|
||||
@@ -40,7 +32,7 @@ func Serve() {
|
||||
Views: engine,
|
||||
Prefork: false,
|
||||
AppName: "GotHub",
|
||||
// fucked up way to fix rate limits
|
||||
// kind of screwed up way to fix rate limits
|
||||
EnableTrustedProxyCheck: true,
|
||||
TrustedProxies: []string{"0.0.0.0/0"},
|
||||
ProxyHeader: fiber.HeaderXForwardedFor,
|
||||
@@ -66,8 +58,6 @@ func Serve() {
|
||||
Expiration: 5 * time.Minute,
|
||||
}))
|
||||
|
||||
app.Get("/", pages.HandleIndex)
|
||||
|
||||
app.Use(compress.New(compress.Config{
|
||||
Level: compress.LevelBestSpeed, // 1
|
||||
}))
|
||||
@@ -84,6 +74,7 @@ func Serve() {
|
||||
},
|
||||
})
|
||||
|
||||
app.Get("/", pages.HandleIndex)
|
||||
app.Static("/css", "./public/css")
|
||||
app.Static("/fonts", "./public/fonts")
|
||||
app.Static("/robots.txt", "./public/robots.txt")
|
||||
@@ -96,57 +87,7 @@ func Serve() {
|
||||
return nil
|
||||
})
|
||||
app.Get("/:user/:repo", ratelimiter, pages.HandleRepo)
|
||||
app.Get("/file/:user/:repo/:branch/:file", func(c *fiber.Ctx) error {
|
||||
var file string
|
||||
url := "https://raw.githubusercontent.com/" + c.Params("user") + "/" + c.Params("repo") + "/" + c.Params("branch") + "/" + c.Params("file")
|
||||
err := requests.
|
||||
URL(url).
|
||||
ToString(&file).
|
||||
Fetch(context.Background())
|
||||
if err != nil {
|
||||
return c.Status(404).Render("error", fiber.Map{
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
|
||||
lexer := lexers.Match(c.Params("file"))
|
||||
if lexer == nil {
|
||||
lexer = lexers.Fallback
|
||||
}
|
||||
|
||||
formatter := htmlfmt.New(
|
||||
htmlfmt.WithClasses(true),
|
||||
htmlfmt.WithLineNumbers(true),
|
||||
htmlfmt.PreventSurroundingPre(false),
|
||||
)
|
||||
|
||||
buf := bytes.Buffer{}
|
||||
writer := bufio.NewWriter(&buf)
|
||||
style := styles.Get("native")
|
||||
|
||||
tokens, err := lexer.Tokenise(nil, file)
|
||||
if err != nil {
|
||||
return c.Status(500).Render("error", fiber.Map{
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
formatter.Format(writer, style, tokens)
|
||||
|
||||
cssbuf := bytes.Buffer{}
|
||||
cssw := bufio.NewWriter(&cssbuf)
|
||||
formatter.WriteCSS(cssw, style)
|
||||
|
||||
writer.Flush()
|
||||
cssw.Flush()
|
||||
|
||||
return c.Render("file", fiber.Map{
|
||||
"file": template.HTML(buf.String()),
|
||||
"css": template.CSS(cssbuf.String()),
|
||||
"fullname": c.Params("user") + "/" + c.Params("repo"),
|
||||
"name": c.Params("file"),
|
||||
"branch": c.Params("branch"),
|
||||
})
|
||||
})
|
||||
app.Get("/file/:user/:repo/:branch/:file", pages.FileView)
|
||||
app.Get("/download/:user/:repo/:branch", ratelimiter, func(c *fiber.Ctx) error {
|
||||
utils.ProxyRequest(c, "https://github.com/"+c.Params("user")+"/"+c.Params("repo")+"/archive/"+c.Params("branch")+".zip")
|
||||
return nil
|
||||
@@ -169,8 +110,6 @@ func Serve() {
|
||||
val, ok := os.LookupEnv("GOTHUB_PORT")
|
||||
if !ok {
|
||||
val = "3000"
|
||||
} else {
|
||||
log.Println("Using port from env variable")
|
||||
}
|
||||
log.Fatal(app.Listen(":" + val))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user