From 29ee2b17cba04968bee9cf7a17c3099d4fc62a2b Mon Sep 17 00:00:00 2001 From: Joe Constant Date: Sun, 28 Jan 2024 01:26:55 -0700 Subject: [PATCH] Add pre-commit hook Also cleanup lint issues and upgrade Go to 1.21.6 --- .codesandbox/template.json | 2 +- .../workflows/go-dependency-submission.yml | 2 +- .github/workflows/go.yml | 2 +- .gitignore | 2 +- .pre-commit-config.yaml | 27 +++++++++++++++++++ Dockerfile | 2 +- README.md | 8 +++++- cmd/federation.go | 3 +-- go.mod | 6 ++--- go.sum | 7 ++--- internal/activitypub/common.go | 4 +-- internal/activitypub/post.go | 4 +-- internal/activitypub/user.go | 14 +++++----- internal/http/activity.go | 9 ++++--- internal/http/middleware.go | 10 +++++-- internal/http/post.go | 8 ++++-- internal/http/server.go | 16 ++++++----- internal/http/user.go | 8 ++++-- internal/lemmy/client_test.go | 6 ++--- internal/lemmy/user.go | 6 ++--- 20 files changed, 99 insertions(+), 47 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.codesandbox/template.json b/.codesandbox/template.json index c031af3..70793fb 100644 --- a/.codesandbox/template.json +++ b/.codesandbox/template.json @@ -7,4 +7,4 @@ "golang" ], "published": true -} \ No newline at end of file +} diff --git a/.github/workflows/go-dependency-submission.yml b/.github/workflows/go-dependency-submission.yml index 428c7b1..b82b1de 100644 --- a/.github/workflows/go-dependency-submission.yml +++ b/.github/workflows/go-dependency-submission.yml @@ -17,7 +17,7 @@ jobs: - uses: actions/setup-go@v5 with: - go-version: "1.21.5" + go-version: "1.21.6" - name: Run snapshot action uses: actions/go-dependency-submission@v2 diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index df52b9f..cfe8a9a 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -34,7 +34,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v5 with: - go-version: '1.21.5' + go-version: '1.21.6' - id: govulncheck uses: golang/govulncheck-action@v1 diff --git a/.gitignore b/.gitignore index ae0914e..92fe114 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,4 @@ go.work # jetbrains editor -.idea \ No newline at end of file +.idea diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..b582b8c --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,27 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files +- repo: https://github.com/zricethezav/gitleaks + rev: v8.18.1 + hooks: + - id: gitleaks +- repo: https://github.com/tekwizely/pre-commit-golang + rev: v1.0.0-rc.1 + hooks: + - id: go-mod-tidy-repo + - id: go-fmt + - id: go-critic + - id: golangci-lint-repo-mod + - id: go-test-repo-mod + always_run: true + verbose: true + - id: my-cmd-repo + name: go-vuln-repo + args: [ govulncheck, ./... ] diff --git a/Dockerfile b/Dockerfile index f2e945b..d84a15f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,4 +4,4 @@ COPY federation /app/ EXPOSE 8080 -ENTRYPOINT [ "/app/federation" ] \ No newline at end of file +ENTRYPOINT [ "/app/federation" ] diff --git a/README.md b/README.md index 459a760..886eab4 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,13 @@ Please post any feature requests or bug reports in the repository's [Issues sect ## Local Dev -TBD +### Install pre-requisites: + +- `go install golang.org/x/vuln/cmd/govulncheck@latest` +- `go install -v github.com/go-critic/go-critic/cmd/gocritic@latest` +- Install [golangci-lint](https://golangci-lint.run/usage/install/#local-installation) +- Install [pre-commit](https://pre-commit.com/#installation) +- `pre-commit install` ### Run diff --git a/cmd/federation.go b/cmd/federation.go index 1911d15..f9611f1 100644 --- a/cmd/federation.go +++ b/cmd/federation.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "os" "sublinks/sublinks-federation/internal/db" "sublinks/sublinks-federation/internal/http" "sublinks/sublinks-federation/internal/log" @@ -30,5 +29,5 @@ func main() { s := http.NewServer(logger) s.RunServer() - os.Exit(0) + // NOTE: os.Exit(0) will not run defer statements } diff --git a/go.mod b/go.mod index b5f8f91..78d9d17 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module sublinks/sublinks-federation -go 1.21.5 +go 1.21.6 require ( github.com/go-sql-driver/mysql v1.7.1 @@ -15,7 +15,7 @@ require ( github.com/hashicorp/errwrap v1.1.0 // indirect github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.19 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect go.uber.org/atomic v1.11.0 // indirect - golang.org/x/sys v0.15.0 // indirect + golang.org/x/sys v0.16.0 // indirect ) diff --git a/go.sum b/go.sum index 957bb51..f05d1a0 100644 --- a/go.sum +++ b/go.sum @@ -36,8 +36,9 @@ github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0= github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y= github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A= @@ -64,8 +65,8 @@ golang.org/x/net v0.18.0/go.mod h1:/czyP5RqHAH4odGYxBJ1qz0+CE5WZ+2j1YgoEo8F2jQ= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= -golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= +golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.10.0 h1:tvDr/iQoUqNdohiYm0LmmKcBk+q86lb9EprIUFhHHGg= diff --git a/internal/activitypub/common.go b/internal/activitypub/common.go index ec4d486..9bedf75 100644 --- a/internal/activitypub/common.go +++ b/internal/activitypub/common.go @@ -6,6 +6,6 @@ type Source struct { } type Link struct { - Type string `json:"type"` //"Link" | "Image" - Href string `json:"href"` //"https://enterprise.lemmy.ml/pictrs/image/eOtYb9iEiB.png" + Type string `json:"type"` // "Link" | "Image" + Href string `json:"href"` // "https://enterprise.lemmy.ml/pictrs/image/eOtYb9iEiB.png" } diff --git a/internal/activitypub/post.go b/internal/activitypub/post.go index 07eb581..04f845e 100644 --- a/internal/activitypub/post.go +++ b/internal/activitypub/post.go @@ -46,8 +46,8 @@ func NewPost(postUrl string, fromUser string, communityUrl string, postTitle str Content: fmt.Sprintf("This is a post in the %s community", communityUrl), MediaType: "text/markdown", }, - //Attachment - //Image + // Attachment + // Image Sensitive: nsfw, CommentsEnabled: true, Language: Language{ diff --git a/internal/activitypub/user.go b/internal/activitypub/user.go index bd74be7..0b96036 100644 --- a/internal/activitypub/user.go +++ b/internal/activitypub/user.go @@ -38,25 +38,25 @@ func NewUser( matrixUserId string, bio string, publickey string, - Hostname string, + hostname string, ) User { user := User{} user.Context = GetContext() - user.Id = fmt.Sprintf("https://%s/u/%s", Hostname, name) + user.Id = fmt.Sprintf("https://%s/u/%s", hostname, name) user.PreferredUsername = name - user.Inbox = fmt.Sprintf("https://%s/u/%s/inbox", Hostname, name) - user.Outbox = fmt.Sprintf("https://%s/u/%s/outbox", Hostname, name) + user.Inbox = fmt.Sprintf("https://%s/u/%s/inbox", hostname, name) + user.Outbox = fmt.Sprintf("https://%s/u/%s/outbox", hostname, name) user.Type = "Person" user.Summary = bio user.MatrixUserId = matrixUserId - owner := fmt.Sprintf("https://%s/u/%s", Hostname, name) + owner := fmt.Sprintf("https://%s/u/%s", hostname, name) user.Publickey = PublicKey{ - Keyid: fmt.Sprintf("https://%s/u/%s#main-key", Hostname, name), + Keyid: fmt.Sprintf("https://%s/u/%s#main-key", hostname, name), Owner: owner, PublicKeyPem: publickey, } user.Published = time.Now() - user.Endpoints.SharedInbox = fmt.Sprintf("https://%s/inbox", Hostname) + user.Endpoints.SharedInbox = fmt.Sprintf("https://%s/inbox", hostname) return user } diff --git a/internal/http/activity.go b/internal/http/activity.go index 996db35..3844b88 100644 --- a/internal/http/activity.go +++ b/internal/http/activity.go @@ -39,17 +39,18 @@ func (server *Server) getActivityHandler(w http.ResponseWriter, r *http.Request) obj.Audience, obj, ), "", " ") - - break default: - error.Error(fmt.Errorf("action %s not found", vars["action"])) + server.Logger.Error(fmt.Sprintf("action %s not found", vars["action"]), nil) w.WriteHeader(http.StatusNotFound) return } w.WriteHeader(http.StatusOK) w.Header().Add("content-type", "application/activity+json") - w.Write(content) + _, err := w.Write(content) + if err != nil { + server.Logger.Error("Error writing response", err) + } } func (server *Server) GetPostActivityObject(id string) (*activitypub.Post, error) { diff --git a/internal/http/middleware.go b/internal/http/middleware.go index 9bb8e53..63bd1d0 100644 --- a/internal/http/middleware.go +++ b/internal/http/middleware.go @@ -21,7 +21,10 @@ func (server *Server) notFound(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) w.Header().Add("content-type", "application/activity+json") content, _ := json.Marshal(RequestError{Msg: "not found"}) - w.Write(content) + _, err := w.Write(content) + if err != nil { + server.Logger.Error("Error writing response", err) + } } func (server *Server) notAllowedMethod(w http.ResponseWriter, r *http.Request) { @@ -29,5 +32,8 @@ func (server *Server) notAllowedMethod(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNotFound) w.Header().Add("content-type", "application/activity+json") content, _ := json.Marshal(RequestError{Msg: "method not allowed"}) - w.Write(content) + _, err := w.Write(content) + if err != nil { + server.Logger.Error("Error writing response", err) + } } diff --git a/internal/http/post.go b/internal/http/post.go index 859b871..a564756 100644 --- a/internal/http/post.go +++ b/internal/http/post.go @@ -3,10 +3,11 @@ package http import ( "context" "encoding/json" - "github.com/gorilla/mux" "net/http" "sublinks/sublinks-federation/internal/activitypub" "sublinks/sublinks-federation/internal/lemmy" + + "github.com/gorilla/mux" ) func (server *Server) SetupPostRoutes() { @@ -27,5 +28,8 @@ func (server *Server) getPostHandler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Header().Add("content-type", "application/activity+json") content, _ := json.MarshalIndent(postLd, "", " ") - w.Write(content) + _, err = w.Write(content) + if err != nil { + server.Logger.Error("Error writing response", err) + } } diff --git a/internal/http/server.go b/internal/http/server.go index 086239d..d7cced6 100644 --- a/internal/http/server.go +++ b/internal/http/server.go @@ -3,12 +3,13 @@ package http import ( "context" "flag" - "github.com/gorilla/mux" "net/http" "os" "os/signal" "sublinks/sublinks-federation/internal/log" "time" + + "github.com/gorilla/mux" ) type Server struct { @@ -34,9 +35,9 @@ func (server *Server) RunServer() { server.SetupPostRoutes() server.SetupApubRoutes() server.SetupActivityRoutes() - server.NotFoundHandler = http.HandlerFunc(server.notFound) - server.MethodNotAllowedHandler = http.HandlerFunc(server.notAllowedMethod) - server.Use(server.logMiddleware) + server.Router.NotFoundHandler = http.HandlerFunc(server.notFound) + server.Router.MethodNotAllowedHandler = http.HandlerFunc(server.notAllowedMethod) + server.Router.Use(server.logMiddleware) srv := &http.Server{ Addr: "0.0.0.0:8080", @@ -45,7 +46,7 @@ func (server *Server) RunServer() { ReadTimeout: time.Second * 15, IdleTimeout: time.Second * 60, // pass embed of Server for *mux - Handler: server, + Handler: server.Router, } // Run our server in a goroutine so that it doesn't block. @@ -69,7 +70,10 @@ func (server *Server) RunServer() { defer cancel() // Doesn't block if no connections, but will otherwise wait // until the timeout deadline. - srv.Shutdown(ctx) + err := srv.Shutdown(ctx) + if err != nil { + server.Logger.Error("Error shutting down server", err) + } // Optionally, you could run srv.Shutdown in a goroutine and block on // <-ctx.Done() if your application should wait for other services // to finalize based on context cancellation. diff --git a/internal/http/user.go b/internal/http/user.go index 5224bc1..be36380 100644 --- a/internal/http/user.go +++ b/internal/http/user.go @@ -4,10 +4,11 @@ import ( "context" "encoding/json" "fmt" - "github.com/gorilla/mux" "net/http" "sublinks/sublinks-federation/internal/activitypub" "sublinks/sublinks-federation/internal/lemmy" + + "github.com/gorilla/mux" ) func (server *Server) SetupUserRoutes() { @@ -29,5 +30,8 @@ func (server *Server) getUserInfoHandler(w http.ResponseWriter, r *http.Request) w.WriteHeader(http.StatusOK) w.Header().Add("content-type", "application/activity+json") content, _ := json.MarshalIndent(userLd, "", " ") - w.Write(content) + _, err = w.Write(content) + if err != nil { + server.Logger.Error("Error writing response", err) + } } diff --git a/internal/lemmy/client_test.go b/internal/lemmy/client_test.go index c6bf834..aedd24e 100644 --- a/internal/lemmy/client_test.go +++ b/internal/lemmy/client_test.go @@ -3,8 +3,8 @@ package lemmy import "testing" func TestNewClient(t *testing.T) { - client := NewClient("url", "user", "pass") - if client.User != "user" { + client := NewClient("url", "user", "pass") + if client.User != "user" { t.Error("user not set") - } + } } diff --git a/internal/lemmy/user.go b/internal/lemmy/user.go index ebe4bbe..e6a3fe5 100644 --- a/internal/lemmy/user.go +++ b/internal/lemmy/user.go @@ -12,9 +12,9 @@ type UserResponse struct { type PersonView struct { Person Person `json:"person"` PersonCounts PersonCounts `json:"counts"` - Comments []Comment `json:"comments"` - Posts []Post `json:"posts"` - Moderates []Community `json:"moderates"` + Comments []Comment `json:"comments"` + Posts []Post `json:"posts"` + Moderates []Community `json:"moderates"` } type PersonCounts struct {