Skip to content

Commit

Permalink
bump to Go 1.21 and drop logrus
Browse files Browse the repository at this point in the history
  • Loading branch information
dimkr committed Aug 9, 2023
1 parent 5204e45 commit 129dbdb
Show file tree
Hide file tree
Showing 34 changed files with 246 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: '>=1.20.0'
go-version: '>=1.21.0'
- run: |
go generate ./migrations
go vet ./...
Expand Down
4 changes: 2 additions & 2 deletions cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package cfg

import (
"flag"
log "github.com/sirupsen/logrus"
"log/slog"
)

const MaxPostsLength = 200
Expand All @@ -30,5 +30,5 @@ var (

func init() {
flag.StringVar(&Domain, "domain", "localhost", "Domain name")
flag.IntVar(&LogLevel, "loglevel", int(log.InfoLevel), "Logging verbosity")
flag.IntVar(&LogLevel, "loglevel", int(slog.LevelInfo), "Logging verbosity")
}
12 changes: 5 additions & 7 deletions cmd/tootik/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ import (
"flag"

"github.com/dimkr/tootik/data"
"github.com/dimkr/tootik/migrations"
"github.com/dimkr/tootik/fed"
"github.com/dimkr/tootik/front/finger"
"github.com/dimkr/tootik/front/gemini"
"github.com/dimkr/tootik/front/gopher"
"github.com/dimkr/tootik/logger"
"github.com/dimkr/tootik/migrations"
log "github.com/dimkr/tootik/slogru"
"github.com/dimkr/tootik/user"
_ "github.com/mattn/go-sqlite3"
"os"
Expand All @@ -52,8 +52,6 @@ var (
func main() {
flag.Parse()

log := logger.New(nil)

db, err := sql.Open("sqlite3", *dbPath+"?_journal_mode=WAL")
if err != nil {
log.Fatal(err)
Expand All @@ -79,7 +77,7 @@ func main() {
}
}()

if err := migrations.Run(ctx, db, log); err != nil {
if err := migrations.Run(ctx, db, log.Default()); err != nil {
log.Fatal(err)
}

Expand Down Expand Up @@ -129,13 +127,13 @@ func main() {

wg.Add(1)
go func() {
fed.DeliverPosts(ctx, db, log)
fed.DeliverPosts(ctx, db, log.Default())
wg.Done()
}()

wg.Add(1)
go func() {
if err := fed.ProcessActivities(ctx, db, log); err != nil {
if err := fed.ProcessActivities(ctx, db, log.Default()); err != nil {
log.WithError(err).Error("Failed to process activities")
}
cancel()
Expand Down
2 changes: 1 addition & 1 deletion fed/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
"github.com/dimkr/tootik/cfg"
"github.com/dimkr/tootik/data"
"github.com/dimkr/tootik/note"
log "github.com/dimkr/tootik/slogru"
_ "github.com/mattn/go-sqlite3"
log "github.com/sirupsen/logrus"
"strings"
"time"
)
Expand Down
2 changes: 1 addition & 1 deletion fed/deliver.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/dimkr/tootik/cfg"
"github.com/dimkr/tootik/data"
"github.com/dimkr/tootik/note"
log "github.com/sirupsen/logrus"
log "github.com/dimkr/tootik/slogru"
"strings"
"time"
)
Expand Down
2 changes: 1 addition & 1 deletion fed/icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"fmt"
"github.com/dimkr/tootik/cfg"
"github.com/dimkr/tootik/icon"
log "github.com/dimkr/tootik/slogru"
_ "github.com/mattn/go-sqlite3"
log "github.com/sirupsen/logrus"
"net/http"
"path/filepath"
"strings"
Expand Down
2 changes: 1 addition & 1 deletion fed/inbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"errors"
"fmt"
"github.com/dimkr/tootik/cfg"
log "github.com/dimkr/tootik/slogru"
_ "github.com/mattn/go-sqlite3"
log "github.com/sirupsen/logrus"
"io"
"io/ioutil"
"net/http"
Expand Down
12 changes: 6 additions & 6 deletions fed/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
"errors"
"fmt"
"github.com/dimkr/tootik/cfg"
"github.com/dimkr/tootik/logger"
logger "github.com/dimkr/tootik/slogru"
_ "github.com/mattn/go-sqlite3"
log "github.com/sirupsen/logrus"
"log/slog"
"net"
"net/http"
"time"
Expand All @@ -46,19 +46,19 @@ func ListenAndServe(ctx context.Context, db *sql.DB, addr, cert, key string) err
mux := http.NewServeMux()
mux.HandleFunc("/robots.txt", robots)
mux.HandleFunc("/.well-known/webfinger", func(w http.ResponseWriter, r *http.Request) {
handler := webFingerHandler{logger.New(log.Fields{"query": r.URL.RawQuery}), db}
handler := webFingerHandler{logger.With("query", r.URL.RawQuery), db}
handler.Handle(w, r)
})
mux.HandleFunc("/user/", func(w http.ResponseWriter, r *http.Request) {
handler := userHandler{logger.New(log.Fields{"path": r.URL.Path}), db}
handler := userHandler{logger.With(slog.String("path", r.URL.Path)), db}
handler.Handle(w, r)
})
mux.HandleFunc("/icon/", func(w http.ResponseWriter, r *http.Request) {
handler := iconHandler{logger.New(log.Fields{"path": r.URL.Path}), db}
handler := iconHandler{logger.With(slog.String("path", r.URL.Path)), db}
handler.Handle(w, r)
})
mux.HandleFunc("/inbox/", func(w http.ResponseWriter, r *http.Request) {
handler := inboxHandler{logger.New(log.Fields{"path": r.URL.Path}), db}
handler := inboxHandler{logger.With(slog.String("path", r.URL.Path)), db}
handler.Handle(w, r)
})
mux.HandleFunc("/", root)
Expand Down
2 changes: 1 addition & 1 deletion fed/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"fmt"
"github.com/dimkr/tootik/ap"
"github.com/dimkr/tootik/cfg"
log "github.com/sirupsen/logrus"
log "github.com/dimkr/tootik/slogru"
"io"
"io/ioutil"
"net/http"
Expand Down
4 changes: 2 additions & 2 deletions fed/resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package fed

import (
"context"
"github.com/dimkr/tootik/logger"
log "github.com/dimkr/tootik/slogru"
"golang.org/x/sync/semaphore"
"sync"
)
Expand All @@ -33,7 +33,7 @@ type ResolverPool struct {
var Resolvers = ResolverPool{
Pool: sync.Pool{
New: func() any {
return &Resolver{Log: logger.New(nil)}
return &Resolver{Log: log.Default()}
},
},
Weighted: semaphore.NewWeighted(poolSize),
Expand Down
2 changes: 1 addition & 1 deletion fed/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
"fmt"
"github.com/dimkr/tootik/ap"
"github.com/dimkr/tootik/cfg"
log "github.com/dimkr/tootik/slogru"
"github.com/go-fed/httpsig"
log "github.com/sirupsen/logrus"
"io"
"io/ioutil"
"net/http"
Expand Down
2 changes: 1 addition & 1 deletion fed/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"errors"
"fmt"
"github.com/dimkr/tootik/cfg"
log "github.com/dimkr/tootik/slogru"
_ "github.com/mattn/go-sqlite3"
log "github.com/sirupsen/logrus"
"net/http"
"path/filepath"
"strings"
Expand Down
2 changes: 1 addition & 1 deletion fed/webfinger.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"encoding/json"
"fmt"
"github.com/dimkr/tootik/cfg"
log "github.com/dimkr/tootik/slogru"
_ "github.com/mattn/go-sqlite3"
log "github.com/sirupsen/logrus"
"net/http"
"strings"
)
Expand Down
6 changes: 3 additions & 3 deletions front/finger/finger.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import (
"github.com/dimkr/tootik/ap"
"github.com/dimkr/tootik/cfg"
"github.com/dimkr/tootik/data"
"github.com/dimkr/tootik/logger"
log "github.com/dimkr/tootik/slogru"
"github.com/dimkr/tootik/text/plain"
log "github.com/sirupsen/logrus"
"log/slog"
)

const reqTimeout = time.Second * 30
Expand Down Expand Up @@ -68,7 +68,7 @@ func handle(ctx context.Context, conn net.Conn, db *sql.DB, wg *sync.WaitGroup)
}

user := string(req[:total-2])
log := logger.New(log.Fields{"user": user})
log := log.With(slog.String("user", user))

if user == "" {
log.Warn("Invalid username specified")
Expand Down
2 changes: 1 addition & 1 deletion front/gemini/gemini.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ import (
"github.com/dimkr/tootik/ap"
"github.com/dimkr/tootik/cfg"
"github.com/dimkr/tootik/front"
log "github.com/dimkr/tootik/slogru"
"github.com/dimkr/tootik/text/gmi"
log "github.com/sirupsen/logrus"
"net"
"net/url"
"sync"
Expand Down
2 changes: 1 addition & 1 deletion front/gopher/gopher.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"context"
"database/sql"
"github.com/dimkr/tootik/front"
log "github.com/dimkr/tootik/slogru"
"github.com/dimkr/tootik/text/gmap"
log "github.com/sirupsen/logrus"
"net"
"net/url"
"sync"
Expand Down
13 changes: 7 additions & 6 deletions front/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ import (
"database/sql"
"errors"
"github.com/dimkr/tootik/ap"
"github.com/dimkr/tootik/logger"
log "github.com/dimkr/tootik/slogru"
"github.com/dimkr/tootik/text"
log "github.com/sirupsen/logrus"
"net/url"
"regexp"
"sync"
Expand All @@ -37,9 +36,11 @@ var (
func Handle(ctx context.Context, w text.Writer, reqUrl *url.URL, user *ap.Actor, db *sql.DB, wg *sync.WaitGroup) {
for re, handler := range handlers {
if re.MatchString(reqUrl.Path) {
logFields := log.Fields{"path": reqUrl.Path}
if user != nil {
logFields["user"] = user.ID
var l *log.Logger
if user == nil {
l = log.With("path", reqUrl.Path)
} else {
l = log.With("path", reqUrl.Path, "user", user.ID)
}

handler(w, &request{
Expand All @@ -48,7 +49,7 @@ func Handle(ctx context.Context, w text.Writer, reqUrl *url.URL, user *ap.Actor,
User: user,
DB: db,
WaitGroup: wg,
Log: logger.New(logFields),
Log: l,
})
return
}
Expand Down
2 changes: 1 addition & 1 deletion front/inbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func dailyPosts(w text.Writer, r *request, day time.Time) {

offset, err := getOffset(r.URL)
if err != nil {
r.Log.WithField("url", r.URL.String()).WithError(err).Info("Failed to parse query")
r.Log.WithField("url", r.URL).WithError(err).Info("Failed to parse query")
w.Status(40, "Invalid query")
return
}
Expand Down
4 changes: 2 additions & 2 deletions front/outbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import (
"fmt"
"github.com/dimkr/tootik/ap"
"github.com/dimkr/tootik/data"
log "github.com/dimkr/tootik/slogru"
"github.com/dimkr/tootik/text"
log "github.com/sirupsen/logrus"
"path/filepath"
"regexp"
)
Expand Down Expand Up @@ -58,7 +58,7 @@ func outbox(w text.Writer, r *request) {

offset, err := getOffset(r.URL)
if err != nil {
r.Log.WithField("url", r.URL.String()).WithError(err).Info("Failed to parse query")
r.Log.WithField("url", r.URL).WithError(err).Info("Failed to parse query")
w.Status(40, "Invalid query")
return
}
Expand Down
2 changes: 1 addition & 1 deletion front/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
"github.com/dimkr/tootik/ap"
"github.com/dimkr/tootik/cfg"
"github.com/dimkr/tootik/fed"
log "github.com/dimkr/tootik/slogru"
"github.com/dimkr/tootik/text"
"github.com/dimkr/tootik/text/plain"
log "github.com/sirupsen/logrus"
"net/url"
"regexp"
"time"
Expand Down
2 changes: 1 addition & 1 deletion front/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
"github.com/dimkr/tootik/ap"
"github.com/dimkr/tootik/cfg"
"github.com/dimkr/tootik/data"
log "github.com/dimkr/tootik/slogru"
"github.com/dimkr/tootik/text"
"github.com/dimkr/tootik/text/plain"
log "github.com/sirupsen/logrus"
"net/url"
"regexp"
"strings"
Expand Down
4 changes: 2 additions & 2 deletions front/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func init() {
func local(w text.Writer, r *request) {
offset, err := getOffset(r.URL)
if err != nil {
r.Log.WithField("url", r.URL.String()).WithError(err).Info("Failed to parse query")
r.Log.WithField("url", r.URL).WithError(err).Info("Failed to parse query")
w.Status(40, "Invalid query")
return
}
Expand Down Expand Up @@ -109,7 +109,7 @@ func local(w text.Writer, r *request) {
func federated(w text.Writer, r *request) {
offset, err := getOffset(r.URL)
if err != nil {
r.Log.WithField("url", r.URL.String()).WithError(err).Info("Failed to parse query")
r.Log.WithField("url", r.URL).WithError(err).Info("Failed to parse query")
w.Status(40, "Invalid query")
return
}
Expand Down
2 changes: 1 addition & 1 deletion front/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"fmt"
"github.com/dimkr/tootik/ap"
"github.com/dimkr/tootik/fed"
log "github.com/sirupsen/logrus"
log "github.com/dimkr/tootik/slogru"
"net/url"
"sync"
)
Expand Down
2 changes: 1 addition & 1 deletion front/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func resolve(w text.Writer, r *request) {

query, err := url.QueryUnescape(r.URL.RawQuery)
if err != nil {
r.Log.WithField("url", r.URL.String()).WithError(err).Info("Failed to decode user name")
r.Log.WithField("url", r.URL).WithError(err).Info("Failed to decode user name")
w.Status(40, "Bad input")
return
}
Expand Down
2 changes: 1 addition & 1 deletion front/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func search(w text.Writer, r *request) {

hashtag, err := url.QueryUnescape(r.URL.RawQuery)
if err != nil {
r.Log.WithField("url", r.URL.String()).WithError(err).Info("Failed to decode user name")
r.Log.WithField("url", r.URL).WithError(err).Info("Failed to decode user name")
w.Status(40, "Bad input")
return
}
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
module github.com/dimkr/tootik

go 1.20
go 1.21

require (
github.com/go-fed/httpsig v1.1.0
github.com/mattn/go-sqlite3 v1.14.17
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.8.4
golang.org/x/sync v0.3.0
)
Expand Down
Loading

0 comments on commit 129dbdb

Please sign in to comment.