Skip to content

Commit

Permalink
update logger and server
Browse files Browse the repository at this point in the history
  • Loading branch information
devanbenz committed Jan 23, 2024
1 parent 5b2259a commit 64bee7b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 37 deletions.
2 changes: 1 addition & 1 deletion cmd/federation.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ func main() {
}

conn, err := db.Connect()
defer conn.Close()
if err != nil {
logger.Fatal("failed connecting to db", err)
}
db.RunMigrations(conn)

defer conn.Close()
s := http.NewServer(logger)
s.RunServer()

Expand Down
6 changes: 3 additions & 3 deletions internal/http/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func (s Server) SetupActivityRoutes() {
s.HandleFunc("/activities/{action}/{id}", s.getActivityHandler).Methods("GET")
s.Router.HandleFunc("/activities/{action}/{id}", s.getActivityHandler).Methods("GET")
}

func (s Server) getActivityHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -25,7 +25,7 @@ func (s Server) getActivityHandler(w http.ResponseWriter, r *http.Request) {
case "create":
obj, err := s.GetPostActivityObject(vars["id"])
if err != nil {
s.Error("Error reading object", err)
s.Logger.Error("Error reading object", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
Expand Down Expand Up @@ -57,7 +57,7 @@ func (s Server) GetPostActivityObject(id string) (*activitypub.Post, error) {
c := lemmy.GetLemmyClient(ctx)
post, err := c.GetPost(ctx, id)
if err != nil {
s.Error("Error reading post", err)
s.Logger.Error("Error reading post", err)
return nil, err
}
return activitypub.ConvertPostToApub(post), nil
Expand Down
16 changes: 8 additions & 8 deletions internal/http/apub.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ import (
)

func (s Server) SetupApubRoutes() {
s.HandleFunc("/users/{user}/inbox", getInboxHandler).Methods("GET")
s.HandleFunc("/users/{user}/inbox", postInboxHandler).Methods("POST")
s.HandleFunc("/users/{user}/outbox", getOutboxHandler).Methods("GET")
s.HandleFunc("/users/{user}/outbox", postOutboxHandler).Methods("POST")
s.Router.HandleFunc("/users/{user}/inbox", s.getInboxHandler).Methods("GET")
s.Router.HandleFunc("/users/{user}/inbox", s.postInboxHandler).Methods("POST")
s.Router.HandleFunc("/users/{user}/outbox", s.getOutboxHandler).Methods("GET")
s.Router.HandleFunc("/users/{user}/outbox", s.postOutboxHandler).Methods("POST")
}

func getInboxHandler(w http.ResponseWriter, r *http.Request) {
func (s Server) getInboxHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Add("content-type", "application/activity+json")
}

func postInboxHandler(w http.ResponseWriter, r *http.Request) {
func (s Server) postInboxHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Add("content-type", "application/activity+json")
}

func getOutboxHandler(w http.ResponseWriter, r *http.Request) {
func (s Server) getOutboxHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Add("content-type", "application/activity+json")
}

func postOutboxHandler(w http.ResponseWriter, r *http.Request) {
func (s Server) postOutboxHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Add("content-type", "application/activity+json")
}
4 changes: 2 additions & 2 deletions internal/http/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func (s Server) SetupPostRoutes() {
s.HandleFunc("/post/{postId}", s.getPostHandler).Methods("GET")
s.Router.HandleFunc("/post/{postId}", s.getPostHandler).Methods("GET")
}

func (s Server) getPostHandler(w http.ResponseWriter, r *http.Request) {
Expand All @@ -19,7 +19,7 @@ func (s Server) getPostHandler(w http.ResponseWriter, r *http.Request) {
c := lemmy.GetLemmyClient(ctx)
post, err := c.GetPost(ctx, vars["postId"])
if err != nil {
s.Error("Error reading post", err)
s.Logger.Error("Error reading post", err)
return
}
postLd := activitypub.ConvertPostToApub(post)
Expand Down
6 changes: 3 additions & 3 deletions internal/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ func (s Server) RunServer() {

// Run our server in a goroutine so that it doesn't block.
go func() {
s.Info("Starting server")
s.Logger.Info("Starting server")
if err := srv.ListenAndServe(); err != nil {
s.Error("Error starting server", err)
s.Logger.Error("Error starting server", err)
}
}()

Expand All @@ -73,5 +73,5 @@ func (s Server) RunServer() {
// 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.
s.Info("shutting down")
s.Logger.Info("shutting down")
}
6 changes: 3 additions & 3 deletions internal/http/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import (
)

func (s Server) SetupUserRoutes() {
s.HandleFunc("/u/{user}", s.getUserInfoHandler).Methods("GET")
s.Router.HandleFunc("/u/{user}", s.getUserInfoHandler).Methods("GET")
}

func (s Server) getUserInfoHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
ctx := context.Background()
c := lemmy.GetLemmyClient(ctx)
s.Info(fmt.Sprintf("Looking up user %s", vars["user"]))
s.Logger.Info(fmt.Sprintf("Looking up user %s", vars["user"]))
user, err := c.GetUser(ctx, vars["user"])
if err != nil {
s.Error("Error reading user", err)
s.Logger.Error("Error reading user", err)
return
}

Expand Down
37 changes: 20 additions & 17 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package log

import (
"encoding/json"
"github.com/rs/zerolog"
"io"
"net/http"

"github.com/rs/zerolog/log"
"os"
)

type Logger interface {
Expand All @@ -17,34 +17,37 @@ type Logger interface {
Request(msg string, r *http.Request)
}

type Log struct{}
type Log struct {
*zerolog.Logger
}

func NewLogger() *Log {
log.Debug().Msg("Logger started")
return &Log{}
logger := zerolog.New(os.Stdout)
logger.Debug().Msg("Logger started")
return &Log{&logger}
}

func (l Log) Info(msg string) {
log.Info().Msg(msg)
func (l *Log) Info(msg string) {
l.Logger.Info().Msg(msg)
}

func (l Log) Debug(msg string) {
log.Debug().Msg(msg)
func (l *Log) Debug(msg string) {
l.Logger.Debug().Msg(msg)
}

func (l Log) Error(msg string, err error) {
log.Error().Err(err).Msg(msg)
func (l *Log) Error(msg string, err error) {
l.Logger.Error().Err(err).Msg(msg)
}

func (l Log) Fatal(msg string, err error) {
log.Fatal().Err(err).Msg(msg)
func (l *Log) Fatal(msg string, err error) {
l.Logger.Fatal().Err(err).Msg(msg)
}

func (l Log) Warn(msg string) {
log.Warn().Msg(msg)
func (l *Log) Warn(msg string) {
l.Logger.Warn().Msg(msg)
}

func (l Log) Request(msg string, r *http.Request) {
func (l *Log) Request(msg string, r *http.Request) {
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
Expand All @@ -66,7 +69,7 @@ func (l Log) Request(msg string, r *http.Request) {
} else {
body = rawbody
}
log.Debug().
l.Logger.Debug().
Str("method", r.Method).
Str("url", r.URL.String()).
Str("user-agent", r.UserAgent()).
Expand Down

0 comments on commit 64bee7b

Please sign in to comment.