From 6e90f3eca9f9269ba97e72f510b13ad625526914 Mon Sep 17 00:00:00 2001 From: Odyssey Date: Wed, 30 Nov 2022 17:37:27 +0100 Subject: [PATCH] Initial repo view. Signed-off-by: Odyssey --- main.go | 21 ++++++++++++++++++ pages/repo.go | 51 +++++++++++++++++++++++++++++++++++++++---- public/css/global.css | 21 ++++++++++++++++++ views/file.html | 17 +++++++++++++++ views/repo.html | 20 +++++++++++++---- 5 files changed, 122 insertions(+), 8 deletions(-) create mode 100644 views/file.html diff --git a/main.go b/main.go index 0202155..157a8d3 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "log" "os" @@ -24,6 +25,8 @@ import ( "github.com/gofiber/fiber/v2/middleware/limiter" "html/template" + + "github.com/carlmjohnson/requests" ) func main() { @@ -96,6 +99,24 @@ func main() { app.Get("/explore", ratelimiter, pages.HandleExplore) app.Get("/:user", ratelimiter, pages.HandleUser) 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, + }) + } + return c.Render("file", fiber.Map{ + "file": file, + "fullname": c.Params("user") + "/" + c.Params("repo"), + "name": c.Params("file"), + }) + }) 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 { diff --git a/pages/repo.go b/pages/repo.go index 0f8aac1..504232c 100644 --- a/pages/repo.go +++ b/pages/repo.go @@ -1,8 +1,13 @@ package pages import ( + "context" + "log" + "codeberg.org/Odyssium/gothub/utils" + "github.com/carlmjohnson/requests" "github.com/gofiber/fiber/v2" + "github.com/gomarkdown/markdown" ) type Repo struct { @@ -20,15 +25,51 @@ type Repo struct { } type RepoFiles struct { - Name string - Path string - Type string + Name string + Path string + Type string + Fullname string + DefaultBranch string } func HandleRepo(c *fiber.Ctx) error { var repoArray []Repo + var repoFilesArray []RepoFiles // get repo repo := utils.GetRequest("https://api.github.com/repos/" + c.Params("user") + "/" + c.Params("repo")) + if repo.Get("message").String() == "Not Found" { + return c.Status(404).Render("error", fiber.Map{ + "error": "Repository " + c.Params("user") + "/" + c.Params("repo") + " not found", + }) + } + repoFiles := utils.GetRequest("https://api.github.com/repos/" + c.Params("user") + "/" + c.Params("repo") + "/contents") + bruh := repoFiles.Get("#.@pretty").Array() + for _, item := range bruh { + repoFilesArray = append(repoFilesArray, RepoFiles{ + Name: item.Get("path").String(), + Path: item.Get("path").String(), + Type: item.Get("type").String(), + Fullname: repo.Get("full_name").String(), + DefaultBranch: repo.Get("default_branch").String(), + }) + } + + var readmee string + + err := requests. + URL("https://raw.githubusercontent.com/" + c.Params("user") + "/" + c.Params("repo") + "/" + repo.Get("default_branch").String() + "/README.md"). + ToString(&readmee). + Fetch(context.Background()) + if err != nil { + readmee = "" + log.Println(err) + } + + mightBeUnsafe := markdown.ToHTML([]byte(readmee), nil, nil) + + // Trust Nobody + readmeOutput := UGCPolicy().SanitizeBytes(mightBeUnsafe) + repoArray = append(repoArray, Repo{ Fullname: repo.Get("full_name").String(), Description: repo.Get("description").String(), @@ -44,6 +85,8 @@ func HandleRepo(c *fiber.Ctx) error { }) return c.Render("repo", fiber.Map{ - "repo": repoArray, + "repo": repoArray, + "files": repoFilesArray, + "readme": string(readmeOutput), }) } diff --git a/public/css/global.css b/public/css/global.css index 7aa4805..5e19f90 100644 --- a/public/css/global.css +++ b/public/css/global.css @@ -174,6 +174,27 @@ a:hover { flex-direction: column; } +.filesList { + list-style-type: none; + padding: 0; + margin: 0; +} + +.filesUList { + padding: 0; + margin: 0; + list-style-type: none; +} + +/* URI: /file/:user/:repo/:file */ +.filePre { + background-color: var(--background-darker); + color: var(--text); + padding: 8px; + border-radius: 8px; +} + + @media screen and (prefers-color-scheme: light) { :root { --text: #000; diff --git a/views/file.html b/views/file.html new file mode 100644 index 0000000..d953e8f --- /dev/null +++ b/views/file.html @@ -0,0 +1,17 @@ +{{ template "header" .}} + +
+ {{ if .file }} + + +
+

{{.name}}

+
{{ .file}}
+
+
+ Download +
+ {{end}} +
\ No newline at end of file diff --git a/views/repo.html b/views/repo.html index fbf011f..4bebb8b 100644 --- a/views/repo.html +++ b/views/repo.html @@ -25,18 +25,30 @@ {{ end }} {{end}} - + {{ end }} + {{ if .readme}} +
+

README.md

+
+ {{ unescape .readme}} +
+
+ {{ end }} {{ else }}

Repository not found

That repository doesn't exist.