Updated the README
Added new security layers
This commit is contained in:
@@ -6,17 +6,14 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Entry is the API/JSON shape the server returns.
|
||||
// StoredAt is RFC3339/RFC3339Nano in UTC.
|
||||
type Entry struct {
|
||||
Hash string `json:"hash"`
|
||||
Bytes int64 `json:"bytes"`
|
||||
StoredAt string `json:"stored_at"` // RFC3339( Nano ) string
|
||||
StoredAt string `json:"stored_at"`
|
||||
Private bool `json:"private"`
|
||||
CreatorTZ string `json:"creator_tz,omitempty"` // IANA TZ like "America/New_York"
|
||||
CreatorTZ string `json:"creator_tz,omitempty"`
|
||||
}
|
||||
|
||||
// internal record with real time.Time for sorting/comparison.
|
||||
type rec struct {
|
||||
Hash string
|
||||
Bytes int64
|
||||
@@ -25,30 +22,20 @@ type rec struct {
|
||||
CreatorTZ string
|
||||
}
|
||||
|
||||
// Index is an in-memory index keyed by hash.
|
||||
type Index struct {
|
||||
mu sync.RWMutex
|
||||
hash map[string]rec
|
||||
}
|
||||
|
||||
// New creates an empty Index.
|
||||
func New() *Index {
|
||||
return &Index{
|
||||
hash: make(map[string]rec),
|
||||
}
|
||||
}
|
||||
func New() *Index { return &Index{hash: make(map[string]rec)} }
|
||||
|
||||
// Put inserts or replaces an entry.
|
||||
// e.StoredAt may be RFC3339( Nano ); if empty/invalid we use time.Now().UTC().
|
||||
func (ix *Index) Put(e Entry) error {
|
||||
ix.mu.Lock()
|
||||
defer ix.mu.Unlock()
|
||||
|
||||
t := parseWhen(e.StoredAt)
|
||||
if t.IsZero() {
|
||||
t = time.Now().UTC()
|
||||
}
|
||||
|
||||
ix.hash[e.Hash] = rec{
|
||||
Hash: e.Hash,
|
||||
Bytes: e.Bytes,
|
||||
@@ -59,7 +46,6 @@ func (ix *Index) Put(e Entry) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete removes an entry by hash (no error if absent).
|
||||
func (ix *Index) Delete(hash string) error {
|
||||
ix.mu.Lock()
|
||||
defer ix.mu.Unlock()
|
||||
@@ -67,19 +53,14 @@ func (ix *Index) Delete(hash string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// List returns entries sorted by StoredAt descending.
|
||||
func (ix *Index) List() ([]Entry, error) {
|
||||
ix.mu.RLock()
|
||||
defer ix.mu.RUnlock()
|
||||
|
||||
tmp := make([]rec, 0, len(ix.hash))
|
||||
for _, r := range ix.hash {
|
||||
tmp = append(tmp, r)
|
||||
}
|
||||
sort.Slice(tmp, func(i, j int) bool {
|
||||
return tmp[i].StoredAt.After(tmp[j].StoredAt)
|
||||
})
|
||||
|
||||
sort.Slice(tmp, func(i, j int) bool { return tmp[i].StoredAt.After(tmp[j].StoredAt) })
|
||||
out := make([]Entry, len(tmp))
|
||||
for i, r := range tmp {
|
||||
out[i] = Entry{
|
||||
@@ -93,7 +74,6 @@ func (ix *Index) List() ([]Entry, error) {
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// parseWhen tries RFC3339Nano then RFC3339; returns zero time on failure.
|
||||
func parseWhen(s string) time.Time {
|
||||
if s == "" {
|
||||
return time.Time{}
|
||||
|
Reference in New Issue
Block a user