Skip to content

Commit

Permalink
Add pre-commit hook
Browse files Browse the repository at this point in the history
Also cleanup lint issues and upgrade Go to 1.21.6
  • Loading branch information
lazyguru committed Jan 28, 2024
1 parent ba76425 commit 29ee2b1
Show file tree
Hide file tree
Showing 20 changed files with 99 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .codesandbox/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
"golang"
],
"published": true
}
}
2 changes: 1 addition & 1 deletion .github/workflows/go-dependency-submission.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@
go.work

# jetbrains editor
.idea
.idea
27 changes: 27 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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, ./... ]
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ COPY federation /app/

EXPOSE 8080

ENTRYPOINT [ "/app/federation" ]
ENTRYPOINT [ "/app/federation" ]
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions cmd/federation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"os"
"sublinks/sublinks-federation/internal/db"
"sublinks/sublinks-federation/internal/http"
"sublinks/sublinks-federation/internal/log"
Expand Down Expand Up @@ -30,5 +29,5 @@ func main() {
s := http.NewServer(logger)
s.RunServer()

os.Exit(0)
// NOTE: os.Exit(0) will not run defer statements
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
)
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand All @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions internal/activitypub/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
4 changes: 2 additions & 2 deletions internal/activitypub/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
14 changes: 7 additions & 7 deletions internal/activitypub/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
9 changes: 5 additions & 4 deletions internal/http/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 8 additions & 2 deletions internal/http/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ 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) {
server.Logger.Request("405 Method Not Allowed", r)
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)
}
}
8 changes: 6 additions & 2 deletions internal/http/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
}
}
16 changes: 10 additions & 6 deletions internal/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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",
Expand All @@ -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.
Expand All @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions internal/http/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
}
}
6 changes: 3 additions & 3 deletions internal/lemmy/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}
6 changes: 3 additions & 3 deletions internal/lemmy/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 29ee2b1

Please sign in to comment.