diff options
author | tjpcc <tjp@ctrl-c.club> | 2023-08-30 10:31:28 -0600 |
---|---|---|
committer | tjpcc <tjp@ctrl-c.club> | 2023-08-30 10:31:28 -0600 |
commit | 985651bbabce5f9d4b8f0e8103610b67db4147ea (patch) | |
tree | 38dbca57abc66e2ac52a59493bead59919b1d2cb /slog/backend.go | |
parent | 77ac83e700415cdbd4635eae91e79f99312ea240 (diff) |
[slog backend] re-ordering for readability
Diffstat (limited to 'slog/backend.go')
-rw-r--r-- | slog/backend.go | 96 |
1 files changed, 48 insertions, 48 deletions
diff --git a/slog/backend.go b/slog/backend.go index 613601c..b68bf90 100644 --- a/slog/backend.go +++ b/slog/backend.go @@ -39,7 +39,7 @@ func NewBackend(logger log.Logger, waitTime time.Duration) (nntpserver.Backend, waitTime = DefaultWaitTime } - b := &backend{logger: logger, waitTime: waitTime, index: make([]indexEntry, 0)} + b := &backend{logger: logger, waitTime: waitTime, index: []indexEntry{}} if err := b.refreshIndex(); err != nil { return nil, err } @@ -53,11 +53,6 @@ type backend struct { index []indexEntry } -func (b backend) debug(keyvals ...any) error { return level.Debug(b.logger).Log(keyvals...) } -func (b backend) info(keyvals ...any) error { return level.Info(b.logger).Log(keyvals...) } -func (b backend) warn(keyvals ...any) error { return level.Warn(b.logger).Log(keyvals...) } -func (b backend) err(keyvals ...any) error { return level.Error(b.logger).Log(keyvals...) } - func (b backend) ListGroups(max int) ([]*nntp.Group, error) { return []*nntp.Group{group}, nil } @@ -158,48 +153,10 @@ func (b backend) Authorized() bool { return func (b backend) AllowPost() bool { return true } func (b backend) Authenticate(_, _ string) (nntpserver.Backend, error) { return nil, nil } -type indexEntry struct { - id string - ts time.Time - title string - user string - author string -} - -const indexTimeFmt = "2006-01-02 15:04:05.999999" - -func (ie *indexEntry) UnmarshalJSON(b []byte) error { - var tgt struct { - Timestamp string - Id string - Title string - } - if err := json.Unmarshal(b, &tgt); err != nil { - return err - } - - ts, err := time.Parse(indexTimeFmt, tgt.Timestamp) - if err != nil { - return err - } - - ie.id = tgt.Id - ie.ts = ts - ie.title = tgt.Title - return nil -} - -func (ie *indexEntry) MarshalJSON() ([]byte, error) { - return json.Marshal(map[string]any{ - "id": ie.id, - "timestamp": ie.ts.Format(indexTimeFmt), - "title": ie.title, - }) -} - -func (ie indexEntry) messageID() string { - return fmt.Sprintf("<%s.%s>", ie.id, ie.author) -} +func (b backend) debug(keyvals ...any) error { return level.Debug(b.logger).Log(keyvals...) } +func (b backend) info(keyvals ...any) error { return level.Info(b.logger).Log(keyvals...) } +func (b backend) warn(keyvals ...any) error { return level.Warn(b.logger).Log(keyvals...) } +func (b backend) err(keyvals ...any) error { return level.Error(b.logger).Log(keyvals...) } func (b *backend) refreshIndex() error { now := time.Now() @@ -255,6 +212,49 @@ func (b *backend) refreshIndex() error { return nil } +type indexEntry struct { + id string + ts time.Time + title string + user string + author string +} + +const indexTimeFmt = "2006-01-02 15:04:05.999999" + +func (ie *indexEntry) UnmarshalJSON(b []byte) error { + var tgt struct { + Timestamp string + Id string + Title string + } + if err := json.Unmarshal(b, &tgt); err != nil { + return err + } + + ts, err := time.Parse(indexTimeFmt, tgt.Timestamp) + if err != nil { + return err + } + + ie.id = tgt.Id + ie.ts = ts + ie.title = tgt.Title + return nil +} + +func (ie *indexEntry) MarshalJSON() ([]byte, error) { + return json.Marshal(map[string]any{ + "id": ie.id, + "timestamp": ie.ts.Format(indexTimeFmt), + "title": ie.title, + }) +} + +func (ie indexEntry) messageID() string { + return fmt.Sprintf("<%s.%s>", ie.id, ie.author) +} + func myIndexPath() string { return path.Join(os.Getenv("HOME"), ".slog", "index") } |