diff --git a/api/reddit/check_subreddit.go b/api/reddit/check_subreddit.go index db611fa..cbbd941 100644 --- a/api/reddit/check_subreddit.go +++ b/api/reddit/check_subreddit.go @@ -7,6 +7,7 @@ import ( "net/http" "time" + "github.com/tigorlazuardi/redmage/pkg/caller" "github.com/tigorlazuardi/redmage/pkg/errs" ) @@ -22,6 +23,8 @@ func (reddit *Reddit) CheckSubreddit(ctx context.Context, params CheckSubredditP ctx, span := tracer.Start(ctx, "*Reddit.CheckSubreddit") defer span.End() + ctx = caller.WithContext(ctx, caller.New(2)) + url := fmt.Sprintf("https://reddit.com/%s/%s.json?limit=1", params.SubredditType, params.Subreddit) req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, http.NoBody) if err != nil { diff --git a/api/reddit/client.go b/api/reddit/client.go index dd421f0..f8a8ab6 100644 --- a/api/reddit/client.go +++ b/api/reddit/client.go @@ -4,6 +4,7 @@ import ( "net/http" "github.com/tigorlazuardi/redmage/config" + "github.com/tigorlazuardi/redmage/pkg/log" ) type Client interface { @@ -25,6 +26,22 @@ func (ro roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) func createRoundTripper(cfg *config.Config) roundTripperFunc { return func(r *http.Request) (*http.Response, error) { r.Header.Set("User-Agent", cfg.String("download.useragent")) - return http.DefaultTransport.RoundTrip(r) + resp, err := http.DefaultTransport.RoundTrip(r) + if err != nil { + log.New(r.Context()). + Err(err). + Errorf("reddit: %s %s", r.Method, r.URL) + return resp, err + } + if resp.StatusCode >= 400 { + log.New(r.Context()). + Errorf("reddit: %s %s %d", r.Method, r.URL, resp.StatusCode) + return resp, err + } + if resp.StatusCode >= 200 && resp.StatusCode < 300 { + log.New(r.Context()). + Infof("reddit: %s %s %d", r.Method, r.URL, resp.StatusCode) + } + return resp, err } } diff --git a/pkg/caller/caller.go b/pkg/caller/caller.go index b7ded1a..80b8113 100644 --- a/pkg/caller/caller.go +++ b/pkg/caller/caller.go @@ -1,6 +1,7 @@ package caller import ( + "context" "log/slog" "os" "runtime" @@ -70,3 +71,14 @@ func From(pc uintptr) Caller { c.Frame, _ = runtime.CallersFrames([]uintptr{pc}).Next() return c } + +type contextKey struct{} + +func WithContext(ctx context.Context, caller Caller) context.Context { + return context.WithValue(ctx, contextKey{}, caller) +} + +func FromContext(ctx context.Context) (Caller, bool) { + call, ok := ctx.Value(contextKey{}).(Caller) + return call, ok +} diff --git a/pkg/log/log.go b/pkg/log/log.go index b121077..7e1cdf3 100644 --- a/pkg/log/log.go +++ b/pkg/log/log.go @@ -273,6 +273,9 @@ func (entry *Entry) getCaller() caller.Caller { if entry.caller.PC != 0 { return entry.caller } + if call, ok := caller.FromContext(entry.ctx); ok { + return call + } return caller.New(4) } diff --git a/rest/subreddits/create.http b/rest/subreddits/create.http index 470dc87..4ff3d4a 100644 --- a/rest/subreddits/create.http +++ b/rest/subreddits/create.http @@ -2,7 +2,7 @@ POST http://localhost:8080/api/v1/subreddits HTTP/1.1 Host: localhost:8080 { - "name": "CultureImpact", + "name": "AnimeLandscapes", "enable_schedule": 1, "schedule": "@daily", "countback": 300 diff --git a/rest/subreddits/start.http b/rest/subreddits/start.http index 7644c21..36d7049 100644 --- a/rest/subreddits/start.http +++ b/rest/subreddits/start.http @@ -3,5 +3,5 @@ Host: localhost:8080 Content-Type: application/json { - "subreddit": "CultureImpact" + "subreddit": "AnimeLandscapes" } diff --git a/views/homeview/homeview.templ b/views/homeview/homeview.templ index bf0ca73..b5db451 100644 --- a/views/homeview/homeview.templ +++ b/views/homeview/homeview.templ @@ -24,11 +24,11 @@ templ home(_ *views.Context, data Data) {