From ebf23cfe36d14e8a14988b6cf4ac84074555ff59 Mon Sep 17 00:00:00 2001 From: Odyssey Date: Sat, 17 Dec 2022 20:09:19 +0100 Subject: [PATCH 01/12] It's not necessary to build commit info to ldflags now. Signed-off-by: Odyssey --- Dockerfile | 2 +- pages/index.go | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 9df86ed..841f7b9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,7 @@ RUN apk --no-cache add git RUN git clone https://codeberg.org/gothub/gothub . RUN go mod download -RUN GOOS=linux GOARCH=$TARGETARCH go build -ldflags "-X codeberg.org/gothub/gothub/pages.Version=$(git rev-parse --short HEAD)" -o /src/gothub +RUN GOOS=linux GOARCH=$TARGETARCH go build -o /src/gothub FROM scratch as bin diff --git a/pages/index.go b/pages/index.go index df0bc08..2a04102 100644 --- a/pages/index.go +++ b/pages/index.go @@ -1,8 +1,22 @@ package pages -import "github.com/gofiber/fiber/v2" +import ( + "runtime/debug" -var Version = "unknown" + "github.com/gofiber/fiber/v2" +) + +// taken from https://git.vern.cc/pjals/bestofbot. +var Version = func() string { + if info, ok := debug.ReadBuildInfo(); ok { + for _, setting := range info.Settings { + if setting.Key == "vcs.revision" { + return setting.Value[:8] + } + } + } + return "AGPLVIOLATION" +}() func HandleIndex(c *fiber.Ctx) error { return c.Render("index", fiber.Map{ From 05398f38313410adf8f9fede951fc5f98759dc10 Mon Sep 17 00:00:00 2001 From: Odyssey Date: Sat, 17 Dec 2022 20:27:19 +0100 Subject: [PATCH 02/12] Finish the readme. Signed-off-by: Odyssey --- README.md | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 931f015..193dd75 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,35 @@ -# gothub +# GotHub -Alternative front-end for GitHub written with Go. +Alternative front-end for GitHub written with Go. (WIP) +[Matrix](https://matrix.to/#/%23gothub%3Atrygve.me?via=projectsegfau.lt&via=vern.cc&via=matrix.org&via=trygve.me) -## [Matrix](https://matrix.to/#/%23gothub%3Atrygve.me?via=projectsegfau.lt&via=vern.cc&via=matrix.org&via=trygve.me) +## Features +- Lightweight - for both you and the instance host +- No JavaScript - pure HTML and CSS goodness +- No requests made to Micro$oft - they won't even know you made a request! +- Open source - for peer review & trustworthiness +- Save bandwidth - no JavaScript/tracking bloat loaded! +## Instances +The instances list is in the [wiki](https://codeberg.org/gothub/gothub/wiki/Instances). -### This project is still in development. ⚠️ \ No newline at end of file +## Setup +### Docker +We recommend Docker as it is easy to update GotHub that way and it is easy to setup. +[Packages page](https://codeberg.org/gothub/-/packages/container/gothub/latest) +``` +docker run -d --name gothub codeberg.org/gothub/gothub +``` + +If you are using Portainer or Podman, you know how to create the GotHub container. + +### Manual +You will need an installation of Go 1.19+ installed on your system. +```bash +git clone https://codeberg.org/gothub/gothub.git +cd gothub +go build -o gothub +./gothub serve +``` + +If you don't want to use port 3000 for GotHub, set the GOTHUB_PORT environment variable to the port you want to use. \ No newline at end of file From 9457ccaa6f8407adde9eb18ece64d11835c9985d Mon Sep 17 00:00:00 2001 From: Odyssey Date: Sat, 17 Dec 2022 20:48:01 +0100 Subject: [PATCH 03/12] Just say unknown instead. Signed-off-by: Odyssey --- pages/index.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pages/index.go b/pages/index.go index 2a04102..608600d 100644 --- a/pages/index.go +++ b/pages/index.go @@ -15,7 +15,7 @@ var Version = func() string { } } } - return "AGPLVIOLATION" + return "unknown" }() func HandleIndex(c *fiber.Ctx) error { From de99762b07e12ef4bcb991a011f187bb5e58b9f5 Mon Sep 17 00:00:00 2001 From: Midou36O Date: Sat, 17 Dec 2022 20:48:50 +0100 Subject: [PATCH 04/12] Expose the port in docker --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 193dd75..cf9791e 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ The instances list is in the [wiki](https://codeberg.org/gothub/gothub/wiki/Inst We recommend Docker as it is easy to update GotHub that way and it is easy to setup. [Packages page](https://codeberg.org/gothub/-/packages/container/gothub/latest) ``` -docker run -d --name gothub codeberg.org/gothub/gothub +docker run -d -p 3000:3000 --name gothub codeberg.org/gothub/gothub ``` If you are using Portainer or Podman, you know how to create the GotHub container. @@ -32,4 +32,4 @@ go build -o gothub ./gothub serve ``` -If you don't want to use port 3000 for GotHub, set the GOTHUB_PORT environment variable to the port you want to use. \ No newline at end of file +If you don't want to use port 3000 for GotHub, set the GOTHUB_PORT environment variable to the port you want to use. From f772a4c2aec6a46804043c281cc73515177de0ea Mon Sep 17 00:00:00 2001 From: Shiny Nematoda Date: Sat, 24 Dec 2022 19:08:07 +0000 Subject: [PATCH 05/12] Added syntax highlight --- go.mod | 4 +++- go.sum | 7 +++++++ public/css/global.css | 8 ++++---- serve/serve.go | 41 ++++++++++++++++++++++++++++++++++++++++- views/file.html | 5 +++-- 5 files changed, 57 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 533234c..b4e2e54 100644 --- a/go.mod +++ b/go.mod @@ -3,17 +3,20 @@ module codeberg.org/gothub/gothub go 1.19 require ( + github.com/alecthomas/chroma/v2 v2.4.0 github.com/carlmjohnson/requests v0.22.3 github.com/gofiber/fiber/v2 v2.40.1 github.com/gofiber/template v1.7.3 github.com/gomarkdown/markdown v0.0.0-20221013030248-663e2500819c github.com/microcosm-cc/bluemonday v1.0.21 + github.com/spf13/cobra v1.6.1 github.com/tidwall/gjson v1.14.4 ) require ( github.com/andybalholm/brotli v1.0.4 // indirect github.com/aymerick/douceur v0.2.0 // indirect + github.com/dlclark/regexp2 v1.4.0 // indirect github.com/gorilla/css v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/klauspost/compress v1.15.12 // indirect @@ -21,7 +24,6 @@ require ( github.com/mattn/go-isatty v0.0.16 // indirect github.com/mattn/go-runewidth v0.0.14 // indirect github.com/rivo/uniseg v0.4.3 // indirect - github.com/spf13/cobra v1.6.1 // indirect github.com/spf13/pflag v1.0.5 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect diff --git a/go.sum b/go.sum index e9c9623..22c5908 100644 --- a/go.sum +++ b/go.sum @@ -54,6 +54,10 @@ github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3 github.com/Joker/hpp v1.0.0/go.mod h1:8x5n+M1Hp5hC0g8okX3sR3vFQwynaX/UgSOM9MeBKzY= 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/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= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= @@ -104,6 +108,8 @@ 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 h1:F1rxgk7p4uKjwIQxBs9oAXe5CqrXlCduYEJvrF4u93E= +github.com/dlclark/regexp2 v1.4.0/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc= github.com/eknkc/amber v0.0.0-20171010120322-cdade1c07385/go.mod h1:0vRUJqYpeSZifjYj7uP3BG/gKcuzL9xWVV/Y+cK33KM= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= @@ -243,6 +249,7 @@ github.com/hashicorp/memberlist v0.2.2/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOn github.com/hashicorp/memberlist v0.3.0/go.mod h1:MS2lj3INKhZjWNqd3N0m3J+Jxf3DAOnAH9VT3Sh9MUE= github.com/hashicorp/serf v0.9.5/go.mod h1:UWDWwZeL5cuWDJdl0C6wrvrUwEqtQ4ZKBKKENpqIUyk= github.com/hashicorp/serf v0.9.6/go.mod h1:TXZNMjZQijwlDvp+r0b63xZ45H7JmCmgg4gpTwn9UV4= +github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM= github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= diff --git a/public/css/global.css b/public/css/global.css index 359c04a..8f18658 100644 --- a/public/css/global.css +++ b/public/css/global.css @@ -187,12 +187,12 @@ a:hover { } /* URI: /file/:user/:repo/:file */ -.filePre { +.userReadme pre { background-color: var(--background-darker); - color: var(--text); padding: 8px; border-radius: 8px; - white-space: pre-wrap; + /*white-space: pre-wrap;*/ + overflow-x: auto; } @@ -251,4 +251,4 @@ a:hover { margin-bottom: 8px; margin-right: 0; } -} \ No newline at end of file +} diff --git a/serve/serve.go b/serve/serve.go index bbed3d7..b3394fb 100644 --- a/serve/serve.go +++ b/serve/serve.go @@ -6,9 +6,16 @@ import ( "log" "os" "time" + "bytes" + "bufio" "codeberg.org/gothub/gothub/pages" "github.com/carlmjohnson/requests" + + "github.com/alecthomas/chroma/v2/lexers" + htmlfmt "github.com/alecthomas/chroma/v2/formatters/html" + "github.com/alecthomas/chroma/v2/styles" + "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/cache" "github.com/gofiber/fiber/v2/middleware/compress" @@ -105,8 +112,40 @@ func Serve() { "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": file, + "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"), diff --git a/views/file.html b/views/file.html index 5f1ff7b..d1bd108 100644 --- a/views/file.html +++ b/views/file.html @@ -2,16 +2,17 @@
{{ if .file }} +

{{.name}}

-
{{ .file}}
+ {{ .file}}
{{end}} -
\ No newline at end of file + From 3ce44d4c3c0942cdb63969cfe769b3cdd6249e6b Mon Sep 17 00:00:00 2001 From: Shiny Nematoda Date: Sat, 24 Dec 2022 19:10:31 +0000 Subject: [PATCH 06/12] Code formatting --- serve/serve.go | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/serve/serve.go b/serve/serve.go index b3394fb..2c3d61a 100644 --- a/serve/serve.go +++ b/serve/serve.go @@ -1,19 +1,19 @@ package serve import ( + "bufio" + "bytes" "context" "html/template" "log" "os" "time" - "bytes" - "bufio" "codeberg.org/gothub/gothub/pages" "github.com/carlmjohnson/requests" - - "github.com/alecthomas/chroma/v2/lexers" + 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" @@ -112,22 +112,22 @@ func Serve() { "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{ @@ -135,17 +135,17 @@ func Serve() { }) } 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()), + "css": template.CSS(cssbuf.String()), "fullname": c.Params("user") + "/" + c.Params("repo"), "name": c.Params("file"), "branch": c.Params("branch"), From 68b48ca672293c8901ba647118bc07bba76e71d3 Mon Sep 17 00:00:00 2001 From: Shiny Nematoda Date: Mon, 26 Dec 2022 04:20:57 +0000 Subject: [PATCH 07/12] Update 'views/file.html' --- views/file.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/file.html b/views/file.html index d1bd108..92a53e8 100644 --- a/views/file.html +++ b/views/file.html @@ -12,7 +12,7 @@ {{ .file}} {{end}} From bf13a8095ebea81a674f27972d45e2bc4bf1210e Mon Sep 17 00:00:00 2001 From: Shiny Nematoda Date: Mon, 26 Dec 2022 04:21:33 +0000 Subject: [PATCH 08/12] Update 'views/file.html' --- views/file.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/file.html b/views/file.html index 92a53e8..d949669 100644 --- a/views/file.html +++ b/views/file.html @@ -4,7 +4,7 @@ {{ if .file }}
From 286fe9c7a0c5494a1b38ac135c4a7686c3a82c50 Mon Sep 17 00:00:00 2001 From: Odyssey Date: Tue, 27 Dec 2022 15:19:26 +0100 Subject: [PATCH 09/12] Add a legal notice (closes #23) Signed-off-by: Odyssey --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index cf9791e..b5262d0 100644 --- a/README.md +++ b/README.md @@ -33,3 +33,10 @@ go build -o gothub ``` If you don't want to use port 3000 for GotHub, set the GOTHUB_PORT environment variable to the port you want to use. + + +## DMCA/Legal notice + +All content shown on a GotHub instance is from GitHub. Any issues with content shown on a GotHub instance needs to be reported to GitHub, not the instance host's internet or domain provider. + +GitHub is a registered trademark of GitHub, Inc. GotHub is not affiliated with GitHub, Inc. \ No newline at end of file From de9ab67e8cf9f01c5aa10a9b17c6ca25f97f3e87 Mon Sep 17 00:00:00 2001 From: Odyssey Date: Tue, 27 Dec 2022 15:40:12 +0100 Subject: [PATCH 10/12] Add raw button to file view Signed-off-by: Odyssey --- public/css/global.css | 15 +++++++++++++++ views/file.html | 8 ++++---- views/user.html | 2 +- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/public/css/global.css b/public/css/global.css index 8f18658..9dc1c10 100644 --- a/public/css/global.css +++ b/public/css/global.css @@ -9,6 +9,7 @@ :root { --text: #fff; --background: #252525; + --secondary-background: #353535; --background-darker: #151515; --accent: #00b7c3; } @@ -117,6 +118,8 @@ a:hover { .userProfile h1 { color: #00b7c3; margin: 4px; + word-wrap: nowrap; + text-overflow: ellipsis; } .userProfile h2 { @@ -195,11 +198,23 @@ a:hover { overflow-x: auto; } +.userContainer { + display: flex; + flex-direction: row; + justify-content: space-between; + align-items: center; +} + + +.secondary { + background-color: var(--secondary-background); +} @media screen and (prefers-color-scheme: light) { :root { --text: #000; --background: #f1f1f1; + --secondary-background: #e1e1e1; --background-darker: #fff; } body { diff --git a/views/file.html b/views/file.html index d949669..7f50c4d 100644 --- a/views/file.html +++ b/views/file.html @@ -8,11 +8,11 @@
-

{{.name}}

+
+

{{.name}}

+ Raw +
{{ .file}}
-
- Download -
{{end}} diff --git a/views/user.html b/views/user.html index a071c11..127751f 100644 --- a/views/user.html +++ b/views/user.html @@ -9,7 +9,7 @@
{{.Login}}'s avatar {{ if .Name }} -

{{.Name}}

+

{{.Name}}

{{ end }}

{{.Login}}

{{ if eq .Type "User" }} From 40dcdcf68cbd59f1f15714023495856d56bd70fd Mon Sep 17 00:00:00 2001 From: Odyssey Date: Sun, 1 Jan 2023 00:27:10 +0100 Subject: [PATCH 11/12] switch to alpine for docker image Signed-off-by: Odyssey --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 841f7b9..399cd0c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,7 +9,7 @@ RUN git clone https://codeberg.org/gothub/gothub . RUN go mod download RUN GOOS=linux GOARCH=$TARGETARCH go build -o /src/gothub -FROM scratch as bin +FROM alpine:3.16 as bin WORKDIR /app COPY --from=build /src/gothub . From 7eb43d9c97aab20c17b9e37e728ba0f85007c777 Mon Sep 17 00:00:00 2001 From: Odyssey Date: Sun, 1 Jan 2023 01:10:16 +0100 Subject: [PATCH 12/12] Syntax Highlighting CSS fix. Signed-off-by: Odyssey --- public/css/global.css | 1 - 1 file changed, 1 deletion(-) diff --git a/public/css/global.css b/public/css/global.css index 9dc1c10..ef3249b 100644 --- a/public/css/global.css +++ b/public/css/global.css @@ -191,7 +191,6 @@ a:hover { /* URI: /file/:user/:repo/:file */ .userReadme pre { - background-color: var(--background-darker); padding: 8px; border-radius: 8px; /*white-space: pre-wrap;*/