From 6e90f3eca9f9269ba97e72f510b13ad625526914 Mon Sep 17 00:00:00 2001 From: Odyssey Date: Wed, 30 Nov 2022 17:37:27 +0100 Subject: [PATCH 1/9] 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.

From 6f3c54a8fc2d68bd71e693a4ed0e25e1fc3beca6 Mon Sep 17 00:00:00 2001 From: Midou36O Date: Wed, 30 Nov 2022 17:58:11 +0100 Subject: [PATCH 2/9] rename to gothub --- .woodpecker.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.woodpecker.yml b/.woodpecker.yml index 61a0282..800a578 100644 --- a/.woodpecker.yml +++ b/.woodpecker.yml @@ -13,7 +13,7 @@ pipeline: image: plugins/docker settings: registry: codeberg.org - repo: codeberg.org/odyssium/gothub + repo: codeberg.org/gothub/gothub username: from_secret: user password: @@ -27,7 +27,7 @@ pipeline: image: plugins/docker settings: registry: codeberg.org - repo: codeberg.org/odyssium/gothub + repo: codeberg.org/gothub/gothub username: from_secret: user password: From 25c7b1d64392f9cdb0f026f75e981457a6f43b52 Mon Sep 17 00:00:00 2001 From: Midou36O Date: Wed, 30 Nov 2022 18:01:33 +0100 Subject: [PATCH 3/9] Trigger CI From b50293077cb05649c4b2a9475afb97facba5298d Mon Sep 17 00:00:00 2001 From: Midou36O Date: Wed, 30 Nov 2022 18:07:44 +0100 Subject: [PATCH 4/9] Trigger CI From 0c6b23cde0e749556514cc08f8028e44793d105c Mon Sep 17 00:00:00 2001 From: Odyssey Date: Wed, 30 Nov 2022 18:32:15 +0100 Subject: [PATCH 5/9] Update header, and fix raw from fileview. Signed-off-by: Odyssey --- main.go | 10 ++++++++++ public/css/global.css | 1 + views/file.html | 2 +- 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 157a8d3..bcb598f 100644 --- a/main.go +++ b/main.go @@ -115,6 +115,7 @@ func main() { "file": file, "fullname": c.Params("user") + "/" + c.Params("repo"), "name": c.Params("file"), + "branch": c.Params("branch"), }) }) app.Get("/avatar/:id", func(c *fiber.Ctx) error { @@ -135,6 +136,15 @@ func main() { c.Response().Header.Del(fiber.HeaderServer) return nil }) + app.Get("/raw/:user/:repo/:branch/:file", ratelimiter, func(c *fiber.Ctx) error { + url := "https://raw.githubusercontent.com/" + c.Params("user") + "/" + c.Params("repo") + "/" + c.Params("branch") + "/" + c.Params("file") + if err := proxy.Do(c, url); err != nil { + return err + } + // Remove Server header from response + c.Response().Header.Del(fiber.HeaderServer) + return nil + }) val, ok := os.LookupEnv("GOTHUB_PORT") if !ok { val = "3000" diff --git a/public/css/global.css b/public/css/global.css index 5e19f90..d19e324 100644 --- a/public/css/global.css +++ b/public/css/global.css @@ -192,6 +192,7 @@ a:hover { color: var(--text); padding: 8px; border-radius: 8px; + white-space: pre-wrap; } diff --git a/views/file.html b/views/file.html index d953e8f..5f1ff7b 100644 --- a/views/file.html +++ b/views/file.html @@ -11,7 +11,7 @@
{{ .file}}
{{end}} \ No newline at end of file From ea7f1966f48ce3e6cc5600babe005a9bfad99b42 Mon Sep 17 00:00:00 2001 From: Odyssey Date: Wed, 30 Nov 2022 18:36:45 +0100 Subject: [PATCH 6/9] Update source code URL. Signed-off-by: Odyssey --- views/header.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/header.html b/views/header.html index 0537e07..f8aafc0 100644 --- a/views/header.html +++ b/views/header.html @@ -12,7 +12,7 @@ GotHubGotHub (alpha) From 84bc2f0a7109d07b27ed32ceb1abbf6bc72d8cb5 Mon Sep 17 00:00:00 2001 From: Odyssey Date: Wed, 30 Nov 2022 18:47:35 +0100 Subject: [PATCH 7/9] Temporarily disable directory hrefs. Signed-off-by: Odyssey --- views/repo.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/repo.html b/views/repo.html index 4bebb8b..f611966 100644 --- a/views/repo.html +++ b/views/repo.html @@ -32,7 +32,7 @@
    {{ range $key, $value := .files}} {{ if eq .Type "dir" }} -
  • {{.Path}} (directory)
  • +
  • {{.Path}} (directory)
  • {{ else }}
  • {{.Path}}
  • {{ end }} From 5cd6ac27f0026d6971b97c408700f5be03870eac Mon Sep 17 00:00:00 2001 From: Midou36O Date: Wed, 30 Nov 2022 18:47:59 +0100 Subject: [PATCH 8/9] Change the repo namings --- pages/explore.go | 2 +- pages/repo.go | 2 +- pages/user.go | 2 +- views/error.html | 4 ++-- views/header.html | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pages/explore.go b/pages/explore.go index 88b9886..9d9b364 100644 --- a/pages/explore.go +++ b/pages/explore.go @@ -3,7 +3,7 @@ package pages import ( "github.com/gofiber/fiber/v2" - "codeberg.org/Odyssium/gothub/utils" + "codeberg.org/gothub/gothub/utils" ) type Items struct { diff --git a/pages/repo.go b/pages/repo.go index 504232c..9645200 100644 --- a/pages/repo.go +++ b/pages/repo.go @@ -4,7 +4,7 @@ import ( "context" "log" - "codeberg.org/Odyssium/gothub/utils" + "codeberg.org/gothub/gothub/utils" "github.com/carlmjohnson/requests" "github.com/gofiber/fiber/v2" "github.com/gomarkdown/markdown" diff --git a/pages/user.go b/pages/user.go index d636769..108c5fd 100644 --- a/pages/user.go +++ b/pages/user.go @@ -9,7 +9,7 @@ import ( "github.com/gomarkdown/markdown" "github.com/microcosm-cc/bluemonday" - "codeberg.org/Odyssium/gothub/utils" + "codeberg.org/gothub/gothub/utils" "context" "regexp" diff --git a/views/error.html b/views/error.html index 97f422b..75a7eed 100644 --- a/views/error.html +++ b/views/error.html @@ -4,6 +4,6 @@

    Error

    Someone pushed to production. Just kidding, that's probably not what happened. Here's the error:

    {{.error}}
    -

    Think this is a bug? Create an issue on Codeberg.

    +

    Think this is a bug? Create an issue on Codeberg.

    - \ No newline at end of file + diff --git a/views/header.html b/views/header.html index 0537e07..1b63662 100644 --- a/views/header.html +++ b/views/header.html @@ -12,9 +12,9 @@ GotHubGotHub (alpha) - \ No newline at end of file + From b4fef19992005ee3f08b71d185ad0b23166cae87 Mon Sep 17 00:00:00 2001 From: Midou36O Date: Wed, 30 Nov 2022 18:49:43 +0100 Subject: [PATCH 9/9] Change the repo module depends (2) --- go.mod | 2 +- main.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index e027f5f..4577f6f 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module codeberg.org/Odyssium/gothub +module codeberg.org/gothub/gothub go 1.19 diff --git a/main.go b/main.go index 157a8d3..9967c86 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,7 @@ import ( "github.com/gofiber/template/html" - "codeberg.org/Odyssium/gothub/pages" + "codeberg.org/gothub/gothub/pages" "github.com/gofiber/fiber/v2/middleware/cache"