revising aliases c

This commit is contained in:
2025-02-08 13:51:56 -01:06
parent 43d67e9ee1
commit b2530e89f7
14 changed files with 105 additions and 207 deletions

View File

@@ -1,6 +1,7 @@
package app package app
import ( import (
"bytes"
"fmt" "fmt"
"net/http" "net/http"
"strconv" "strconv"
@@ -22,14 +23,14 @@ import (
"realy.lol/tag/atag" "realy.lol/tag/atag"
) )
type List map[st]struct{} type List map[string]struct{}
type Relay struct { type Relay struct {
sync.Mutex sync.Mutex
*config.C *config.C
Store store.I Store store.I
// Owners' pubkeys // Owners' pubkeys
Owners []by Owners [][]byte
// Followed are the pubkeys that are in the Owners' follow lists and have full // Followed are the pubkeys that are in the Owners' follow lists and have full
// access permission. // access permission.
Followed List Followed List
@@ -41,29 +42,29 @@ type Relay struct {
Muted List Muted List
// OwnersFollowLists are the event IDs of owners follow lists, which must not be // OwnersFollowLists are the event IDs of owners follow lists, which must not be
// deleted, only replaced. // deleted, only replaced.
OwnersFollowLists []by OwnersFollowLists [][]byte
// OwnersMuteLists are the event IDs of owners mute lists, which must not be // OwnersMuteLists are the event IDs of owners mute lists, which must not be
// deleted, only replaced. // deleted, only replaced.
OwnersMuteLists []by OwnersMuteLists [][]byte
} }
func (r *Relay) Name() st { return r.C.AppName } func (r *Relay) Name() string { return r.C.AppName }
func (r *Relay) Storage() store.I { return r.Store } func (r *Relay) Storage() store.I { return r.Store }
func (r *Relay) Init() (err er) { func (r *Relay) Init() (err error) {
for _, src := range r.C.Owners { for _, src := range r.C.Owners {
if len(src) < 1 { if len(src) < 1 {
continue continue
} }
dst := make(by, len(src)/2) dst := make([]byte, len(src)/2)
if _, err = hex.DecBytes(dst, by(src)); chk.E(err) { if _, err = hex.DecBytes(dst, []byte(src)); chk.E(err) {
continue continue
} }
r.Owners = append(r.Owners, dst) r.Owners = append(r.Owners, dst)
} }
if len(r.Owners) > 0 { if len(r.Owners) > 0 {
log.T.C(func() st { log.T.C(func() string {
ownerIds := make([]string, len(r.Owners)) ownerIds := make([]string, len(r.Owners))
for i, npub := range r.Owners { for i, npub := range r.Owners {
ownerIds[i] = hex.Enc(npub) ownerIds[i] = hex.Enc(npub)
@@ -77,23 +78,23 @@ func (r *Relay) Init() (err er) {
return nil return nil
} }
func (r *Relay) NoLimiter(pubKey by) (ok bo) { func (r *Relay) NoLimiter(pubKey []byte) (ok bool) {
r.Lock() r.Lock()
defer r.Unlock() defer r.Unlock()
_, ok = r.Followed[st(pubKey)] _, ok = r.Followed[string(pubKey)]
return return
} }
func (r *Relay) ZeroLists() { func (r *Relay) ZeroLists() {
r.Followed = make(map[st]struct{}) r.Followed = make(map[string]struct{})
r.OwnersFollowed = make(map[st]struct{}) r.OwnersFollowed = make(map[string]struct{})
r.OwnersFollowLists = r.OwnersFollowLists[:0] r.OwnersFollowLists = r.OwnersFollowLists[:0]
r.Muted = make(map[st]struct{}) r.Muted = make(map[string]struct{})
r.OwnersMuteLists = r.OwnersMuteLists[:0] r.OwnersMuteLists = r.OwnersMuteLists[:0]
} }
func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st, func (r *Relay) AcceptEvent(c context.T, evt *event.T, hr *http.Request, origin string,
authedPubkey by) (accept bo, notice st, afterSave func()) { authedPubkey []byte) (accept bool, notice string, afterSave func()) {
// if the authenticator is enabled we require auth to accept events // if the authenticator is enabled we require auth to accept events
if !r.AuthEnabled() { if !r.AuthEnabled() {
return true, "", nil return true, "", nil
@@ -115,7 +116,7 @@ func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
// followed can access the relay and upload DM events and such for owner // followed can access the relay and upload DM events and such for owner
// followed users. // followed users.
for o := range r.OwnersFollowed { for o := range r.OwnersFollowed {
if equals(by(o), evt.PubKey) { if bytes.Equal([]byte(o), evt.PubKey) {
return true, "", func() { return true, "", func() {
r.ZeroLists() r.ZeroLists()
r.CheckOwnerLists(context.Bg()) r.CheckOwnerLists(context.Bg())
@@ -126,7 +127,7 @@ func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
if evt.Kind.Equal(kind.MuteList) { if evt.Kind.Equal(kind.MuteList) {
// only owners control the mute list // only owners control the mute list
for _, o := range r.Owners { for _, o := range r.Owners {
if equals(o, evt.PubKey) { if bytes.Equal(o, evt.PubKey) {
return true, "", func() { return true, "", func() {
r.ZeroLists() r.ZeroLists()
r.CheckOwnerLists(context.Bg()) r.CheckOwnerLists(context.Bg())
@@ -136,7 +137,7 @@ func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
} }
for _, o := range r.Owners { for _, o := range r.Owners {
log.T.F("%0x,%0x", o, evt.PubKey) log.T.F("%0x,%0x", o, evt.PubKey)
if equals(o, evt.PubKey) { if bytes.Equal(o, evt.PubKey) {
// prevent owners from deleting their own mute/follow lists in case of bad // prevent owners from deleting their own mute/follow lists in case of bad
// client implementation // client implementation
if evt.Kind.Equal(kind.Deletion) { if evt.Kind.Equal(kind.Deletion) {
@@ -144,8 +145,8 @@ func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
aTags := evt.Tags.GetAll(tag.New("a")) aTags := evt.Tags.GetAll(tag.New("a"))
for _, at := range aTags.F() { for _, at := range aTags.F() {
a := &atag.T{} a := &atag.T{}
var rem by var rem []byte
var err er var err error
if rem, err = a.Unmarshal(at.Value()); chk.E(err) { if rem, err = a.Unmarshal(at.Value()); chk.E(err) {
continue continue
} }
@@ -166,7 +167,7 @@ func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
// don't allow owners to delete their mute or follow lists because // don't allow owners to delete their mute or follow lists because
// they should not want to, can simply replace it, and malicious // they should not want to, can simply replace it, and malicious
// clients may do this specifically to attack the owner's relay (s) // clients may do this specifically to attack the owner's relay (s)
if equals(own, a.PubKey) || if bytes.Equal(own, a.PubKey) ||
a.Kind.Equal(kind.MuteList) || a.Kind.Equal(kind.MuteList) ||
a.Kind.Equal(kind.FollowList) { a.Kind.Equal(kind.FollowList) {
return false, "owners may not delete their own " + return false, "owners may not delete their own " +
@@ -181,7 +182,7 @@ func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
// check the mute list, and reject events authored by muted pubkeys, even if // check the mute list, and reject events authored by muted pubkeys, even if
// they come from a pubkey that is on the follow list. // they come from a pubkey that is on the follow list.
for pk := range r.Muted { for pk := range r.Muted {
if equals(evt.PubKey, by(pk)) { if bytes.Equal(evt.PubKey, []byte(pk)) {
return false, "rejecting event with pubkey " + hex.Enc(evt.PubKey) + return false, "rejecting event with pubkey " + hex.Enc(evt.PubKey) +
" because on owner mute list", nil " because on owner mute list", nil
} }
@@ -189,9 +190,9 @@ func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
// for all else, check the authed pubkey is in the follow list // for all else, check the authed pubkey is in the follow list
for pk := range r.Followed { for pk := range r.Followed {
// allow all events from follows of owners // allow all events from follows of owners
if equals(authedPubkey, by(pk)) { if bytes.Equal(authedPubkey, []byte(pk)) {
log.I.F("accepting event %0x because %0x on owner follow list", log.I.F("accepting event %0x because %0x on owner follow list",
evt.ID, by(pk)) evt.ID, []byte(pk))
return true, "", nil return true, "", nil
} }
} }
@@ -206,8 +207,8 @@ func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
return return
} }
func (r *Relay) AcceptReq(c cx, hr *http.Request, id by, ff *filters.T, func (r *Relay) AcceptReq(c context.T, hr *http.Request, id []byte, ff *filters.T,
authedPubkey by) (allowed *filters.T, ok bo) { authedPubkey []byte) (allowed *filters.T, ok bool) {
if !r.AuthEnabled() || r.PublicReadable && len(authedPubkey) > 0 { if !r.AuthEnabled() || r.PublicReadable && len(authedPubkey) > 0 {
allowed = &filters.T{} allowed = &filters.T{}
// non-authed users may not make filters that have too broad criteria, resource // non-authed users may not make filters that have too broad criteria, resource
@@ -270,7 +271,7 @@ func (r *Relay) AcceptReq(c cx, hr *http.Request, id by, ff *filters.T,
defer r.Unlock() defer r.Unlock()
if len(r.Owners) > 0 { if len(r.Owners) > 0 {
for pk := range r.Followed { for pk := range r.Followed {
if equals(authedPubkey, by(pk)) { if bytes.Equal(authedPubkey, []byte(pk)) {
ok = true ok = true
return return
} }
@@ -287,17 +288,17 @@ func (r *Relay) AcceptReq(c cx, hr *http.Request, id by, ff *filters.T,
// CheckOwnerLists regenerates the owner follow and mute lists if they are empty. // CheckOwnerLists regenerates the owner follow and mute lists if they are empty.
// //
// It also adds the followed npubs of the follows. // It also adds the followed npubs of the follows.
func (r *Relay) CheckOwnerLists(c cx) { func (r *Relay) CheckOwnerLists(c context.T) {
if len(r.Owners) > 0 { if len(r.Owners) > 0 {
r.Lock() r.Lock()
defer r.Unlock() defer r.Unlock()
var err er var err error
var evs []*event.T var evs []*event.T
// need to search DB for moderator npub follow lists, followed npubs are allowed access. // need to search DB for moderator npub follow lists, followed npubs are allowed access.
if len(r.Followed) < 1 { if len(r.Followed) < 1 {
// add the owners themselves of course // add the owners themselves of course
for i := range r.Owners { for i := range r.Owners {
r.Followed[st(r.Owners[i])] = struct{}{} r.Followed[string(r.Owners[i])] = struct{}{}
} }
log.D.Ln("regenerating owners follow lists") log.D.Ln("regenerating owners follow lists")
if evs, err = r.Store.QueryEvents(c, if evs, err = r.Store.QueryEvents(c,
@@ -307,20 +308,20 @@ func (r *Relay) CheckOwnerLists(c cx) {
for _, ev := range evs { for _, ev := range evs {
r.OwnersFollowLists = append(r.OwnersFollowLists, ev.ID) r.OwnersFollowLists = append(r.OwnersFollowLists, ev.ID)
for _, t := range ev.Tags.F() { for _, t := range ev.Tags.F() {
if equals(t.Key(), by("p")) { if bytes.Equal(t.Key(), []byte("p")) {
var p by var p []byte
if p, err = hex.Dec(st(t.Value())); chk.E(err) { if p, err = hex.Dec(string(t.Value())); chk.E(err) {
continue continue
} }
r.Followed[st(p)] = struct{}{} r.Followed[string(p)] = struct{}{}
r.OwnersFollowed[st(p)] = struct{}{} r.OwnersFollowed[string(p)] = struct{}{}
} }
} }
} }
evs = evs[:0] evs = evs[:0]
// next, search for the follow lists of all on the follow list // next, search for the follow lists of all on the follow list
log.D.Ln("searching for owners follows follow lists") log.D.Ln("searching for owners follows follow lists")
var followed []st var followed []string
for f := range r.Followed { for f := range r.Followed {
followed = append(followed, f) followed = append(followed, f)
} }
@@ -333,12 +334,12 @@ func (r *Relay) CheckOwnerLists(c cx) {
// deleted, only replaced. // deleted, only replaced.
r.OwnersFollowLists = append(r.OwnersFollowLists, ev.ID) r.OwnersFollowLists = append(r.OwnersFollowLists, ev.ID)
for _, t := range ev.Tags.F() { for _, t := range ev.Tags.F() {
if equals(t.Key(), by("p")) { if bytes.Equal(t.Key(), []byte("p")) {
var p by var p []byte
if p, err = hex.Dec(st(t.Value())); err != nil { if p, err = hex.Dec(string(t.Value())); err != nil {
continue continue
} }
r.Followed[st(p)] = struct{}{} r.Followed[string(p)] = struct{}{}
} }
} }
} }
@@ -346,7 +347,7 @@ func (r *Relay) CheckOwnerLists(c cx) {
} }
if len(r.Muted) < 1 { if len(r.Muted) < 1 {
log.D.Ln("regenerating owners mute lists") log.D.Ln("regenerating owners mute lists")
r.Muted = make(map[st]struct{}) r.Muted = make(map[string]struct{})
if evs, err = r.Store.QueryEvents(c, if evs, err = r.Store.QueryEvents(c,
&filter.T{Authors: tag.New(r.Owners...), &filter.T{Authors: tag.New(r.Owners...),
Kinds: kinds.New(kind.MuteList)}); chk.E(err) { Kinds: kinds.New(kind.MuteList)}); chk.E(err) {
@@ -354,12 +355,12 @@ func (r *Relay) CheckOwnerLists(c cx) {
for _, ev := range evs { for _, ev := range evs {
r.OwnersMuteLists = append(r.OwnersMuteLists, ev.ID) r.OwnersMuteLists = append(r.OwnersMuteLists, ev.ID)
for _, t := range ev.Tags.F() { for _, t := range ev.Tags.F() {
if equals(t.Key(), by("p")) { if bytes.Equal(t.Key(), []byte("p")) {
var p by var p []byte
if p, err = hex.Dec(st(t.Value())); chk.E(err) { if p, err = hex.Dec(string(t.Value())); chk.E(err) {
continue continue
} }
r.Muted[st(p)] = struct{}{} r.Muted[string(p)] = struct{}{}
} }
} }
} }
@@ -369,11 +370,11 @@ func (r *Relay) CheckOwnerLists(c cx) {
} }
} }
func (r *Relay) AuthEnabled() bo { return r.AuthRequired || len(r.Owners) > 0 } func (r *Relay) AuthEnabled() bool { return r.AuthRequired || len(r.Owners) > 0 }
// ServiceUrl returns the address of the relay to send back in auth responses. // ServiceUrl returns the address of the relay to send back in auth responses.
// If auth is disabled this returns an empty string. // If auth is disabled this returns an empty string.
func (r *Relay) ServiceUrl(req *http.Request) (s st) { func (r *Relay) ServiceUrl(req *http.Request) (s string) {
if !r.AuthEnabled() { if !r.AuthEnabled() {
return return
} }

View File

@@ -4,9 +4,11 @@ import (
"os" "os"
"runtime" "runtime"
"time" "time"
"realy.lol/context"
) )
func MonitorResources(c cx) { func MonitorResources(c context.T) {
tick := time.NewTicker(time.Minute) tick := time.NewTicker(time.Minute)
log.I.Ln("running process", os.Args[0], os.Getpid()) log.I.Ln("running process", os.Args[0], os.Getpid())
// memStats := &runtime.MemStats{} // memStats := &runtime.MemStats{}

View File

@@ -1,22 +1,9 @@
package app package app
import ( import (
"bytes"
"realy.lol/context"
"realy.lol/lol" "realy.lol/lol"
) )
type (
bo = bool
by = []byte
st = string
er = error
no = int
cx = context.T
)
var ( var (
log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf
equals = bytes.Equal
) )

View File

@@ -6,28 +6,28 @@ import (
_ "net/http/pprof" _ "net/http/pprof"
"os" "os"
"runtime/debug" "runtime/debug"
"strings"
"sync" "sync"
"time" "time"
"github.com/pkg/profile" "github.com/pkg/profile"
"realy.lol/bech32encoding"
"realy.lol/cmd/realy/app" "realy.lol/cmd/realy/app"
"realy.lol/context" "realy.lol/context"
"realy.lol/interrupt" "realy.lol/interrupt"
"realy.lol/lol" "realy.lol/lol"
"realy.lol/p256k"
"realy.lol/ratel" "realy.lol/ratel"
"realy.lol/realy" "realy.lol/realy"
"realy.lol/realy/config" "realy.lol/realy/config"
"realy.lol/units"
"realy.lol/realy/options" "realy.lol/realy/options"
"strings"
"realy.lol/bech32encoding"
"realy.lol/p256k"
"realy.lol/signer" "realy.lol/signer"
"realy.lol/units"
) )
func main() { func main() {
var err er var err error
var cfg *config.C var cfg *config.C
if cfg, err = config.New(); chk.T(err) { if cfg, err = config.New(); chk.T(err) {
if err != nil { if err != nil {
@@ -64,11 +64,11 @@ func main() {
MaxLimit: ratel.DefaultMaxLimit, MaxLimit: ratel.DefaultMaxLimit,
UseCompact: cfg.UseCompact, UseCompact: cfg.UseCompact,
Compression: cfg.Compression, Compression: cfg.Compression,
Extra: []no{ Extra: []int{
cfg.DBSizeLimit, cfg.DBSizeLimit,
cfg.DBLowWater, cfg.DBLowWater,
cfg.DBHighWater, cfg.DBHighWater,
cfg.GCFrequency * no(time.Second), cfg.GCFrequency * int(time.Second),
}, },
}, },
) )
@@ -81,8 +81,8 @@ func main() {
if len(admin) < 1 { if len(admin) < 1 {
continue continue
} }
var pk by var pk []byte
if pk, err = bech32encoding.NpubToBytes(by(admin)); chk.E(err) { if pk, err = bech32encoding.NpubToBytes([]byte(admin)); chk.E(err) {
return return
} }
log.I.S(pk) log.I.S(pk)

View File

@@ -1,22 +1,9 @@
package main package main
import ( import (
"bytes"
"realy.lol/context"
"realy.lol/lol" "realy.lol/lol"
) )
type (
bo = bool
by = []byte
st = string
er = error
no = int
cx = context.T
)
var ( var (
log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf
equals = bytes.Equal
) )

View File

@@ -31,14 +31,14 @@ const (
type Result struct { type Result struct {
sec *secp256k1.SecretKey sec *secp256k1.SecretKey
npub by npub []byte
pub *secp256k1.PublicKey pub *secp256k1.PublicKey
} }
var args struct { var args struct {
String st `arg:"positional" help:"the string you want to appear in the npub"` String string `arg:"positional" help:"the string you want to appear in the npub"`
Position st `arg:"positional" default:"end" help:"[begin|contain|end] default: end"` Position string `arg:"positional" default:"end" help:"[begin|contain|end] default: end"`
Threads no `help:"number of threads to mine with - defaults to using all CPU threads available"` Threads int `help:"number of threads to mine with - defaults to using all CPU threads available"`
} }
func main() { func main() {
@@ -56,7 +56,7 @@ Options:
--help, -h display this help and exit`) --help, -h display this help and exit`)
os.Exit(0) os.Exit(0)
} }
var where no var where int
canonical := strings.ToLower(args.Position) canonical := strings.ToLower(args.Position)
switch { switch {
case strings.HasPrefix(canonical, "begin"): case strings.HasPrefix(canonical, "begin"):
@@ -74,7 +74,7 @@ Options:
} }
} }
func Vanity(str string, where no, threads no) (e er) { func Vanity(str string, where int, threads int) (e error) {
// check the string has valid bech32 ciphers // check the string has valid bech32 ciphers
for i := range str { for i := range str {
@@ -146,12 +146,12 @@ out:
return return
} }
func mine(str st, where no, quit qu.C, resC chan Result, wg *sync.WaitGroup, func mine(str string, where int, quit qu.C, resC chan Result, wg *sync.WaitGroup,
counter *atomic.Int64) { counter *atomic.Int64) {
wg.Add(1) wg.Add(1)
var r Result var r Result
var e er var e error
found := false found := false
out: out:
for { for {
@@ -182,17 +182,17 @@ out:
} }
switch where { switch where {
case PositionBeginning: case PositionBeginning:
if bytes.HasPrefix(r.npub, append(prefix, by(str)...)) { if bytes.HasPrefix(r.npub, append(prefix, []byte(str)...)) {
found = true found = true
quit.Q() quit.Q()
} }
case PositionEnding: case PositionEnding:
if bytes.HasSuffix(r.npub, by(str)) { if bytes.HasSuffix(r.npub, []byte(str)) {
found = true found = true
quit.Q() quit.Q()
} }
case PositionContains: case PositionContains:
if bytes.Contains(r.npub, by(str)) { if bytes.Contains(r.npub, []byte(str)) {
found = true found = true
quit.Q() quit.Q()
} }
@@ -203,7 +203,7 @@ out:
// GenKeyPair creates a fresh new key pair using the entropy source used by // GenKeyPair creates a fresh new key pair using the entropy source used by
// crypto/rand (ie, /dev/random on posix systems). // crypto/rand (ie, /dev/random on posix systems).
func GenKeyPair() (sec *secp256k1.SecretKey, func GenKeyPair() (sec *secp256k1.SecretKey,
pub *secp256k1.PublicKey, err er) { pub *secp256k1.PublicKey, err error) {
sec, err = secp256k1.GenerateSecretKey() sec, err = secp256k1.GenerateSecretKey()
if err != nil { if err != nil {

View File

@@ -1,22 +1,9 @@
package main package main
import ( import (
"bytes"
"realy.lol/context"
"realy.lol/lol" "realy.lol/lol"
) )
type (
bo = bool
by = []byte
st = string
er = error
no = int
cx = context.T
)
var ( var (
log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf
equals = bytes.Equal
) )

View File

@@ -5,25 +5,25 @@ import (
) )
type Envelope interface { type Envelope interface {
Label() st Label() string
Write(w io.Writer) (err er) Write(w io.Writer) (err error)
JSON JSON
} }
type JSON interface { type JSON interface {
// Marshal converts the data of the type into JSON, appending it to the provided // Marshal converts the data of the type into JSON, appending it to the provided
// slice and returning the extended slice. // slice and returning the extended slice.
Marshal(dst by) (b by) Marshal(dst []byte) (b []byte)
// Unmarshal decodes a JSON form of a type back into the runtime form, and // Unmarshal decodes a JSON form of a type back into the runtime form, and
// returns whatever remains after the type has been decoded out. // returns whatever remains after the type has been decoded out.
Unmarshal(b by) (r by, err er) Unmarshal(b []byte) (r []byte, err error)
} }
type Binary interface { type Binary interface {
// MarshalBinary converts the data of the type into binary form, appending it to // MarshalBinary converts the data of the type into binary form, appending it to
// the provided slice. // the provided slice.
MarshalBinary(dst by) (b by) MarshalBinary(dst []byte) (b []byte)
// UnmarshalBinary decodes a binary form of a type back into the runtime form, // UnmarshalBinary decodes a binary form of a type back into the runtime form,
// and returns whatever remains after the type has been decoded out. // and returns whatever remains after the type has been decoded out.
UnmarshalBinary(b by) (r by, err er) UnmarshalBinary(b []byte) (r []byte, err error)
} }

View File

@@ -1,22 +1,9 @@
package codec package codec
import ( import (
"bytes"
"realy.lol/context"
"realy.lol/lol" "realy.lol/lol"
) )
type (
bo = bool
by = []byte
st = string
er = error
no = int
cx = context.T
)
var ( var (
log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf
equals = bytes.Equal
) )

View File

@@ -5,19 +5,19 @@ import (
"strings" "strings"
) )
type Env map[st]st type Env map[string]string
// GetEnv reads a file expected to represent a collection of KEY=value in // GetEnv reads a file expected to represent a collection of KEY=value in
// standard shell environment variable format - ie, key usually in all upper // standard shell environment variable format - ie, key usually in all upper
// case no spaces and words separated by underscore, value can have any separator, but usually // case no spaces and words separated by underscore, value can have any separator, but usually
// comma, for an array of values. // comma, for an array of values.
func GetEnv(path st) (env Env, err er) { func GetEnv(path string) (env Env, err error) {
var s by var s []byte
env = make(Env) env = make(Env)
if s, err = os.ReadFile(path); chk.T(err) { if s, err = os.ReadFile(path); chk.T(err) {
return return
} }
lines := strings.Split(st(s), "\n") lines := strings.Split(string(s), "\n")
for _, line := range lines { for _, line := range lines {
if len(line) == 0 { if len(line) == 0 {
continue continue
@@ -31,4 +31,4 @@ func GetEnv(path st) (env Env, err er) {
// LookupEnv returns the raw string value associated with a provided key name, used as a custom // LookupEnv returns the raw string value associated with a provided key name, used as a custom
// environment variable loader for go-simpler.org/env to enable .env file loading. // environment variable loader for go-simpler.org/env to enable .env file loading.
func (env Env) LookupEnv(key st) (value st, ok bo) { value, ok = env[key]; return } func (env Env) LookupEnv(key string) (value string, ok bool) { value, ok = env[key]; return }

View File

@@ -1,22 +1,9 @@
package config package config
import ( import (
"bytes"
"realy.lol/context"
"realy.lol/lol" "realy.lol/lol"
) )
type (
bo = bool
by = []byte
st = string
er = error
no = int
cx = context.T
)
var ( var (
log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf
equals = bytes.Equal
) )

View File

@@ -1,19 +0,0 @@
package context
import (
"bytes"
"context"
)
type (
bo = bool
by = []byte
st = string
er = error
no = int
cx = context.Context
)
var (
equals = bytes.Equal
)

View File

@@ -5,7 +5,6 @@ import (
"io" "io"
"os" "os"
"runtime" "runtime"
"strings"
"time" "time"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
@@ -40,7 +39,7 @@ type (
// Ln prints lists of interfaces with spaces in between // Ln prints lists of interfaces with spaces in between
Ln func(a ...interface{}) Ln func(a ...interface{})
// F prints like fmt.Println surrounded by log details // F prints like fmt.Println surrounded []byte log details
F func(format string, a ...interface{}) F func(format string, a ...interface{})
// S prints a spew.Sdump for an enveloper slice // S prints a spew.Sdump for an enveloper slice
S func(a ...interface{}) S func(a ...interface{})
@@ -48,10 +47,10 @@ type (
// not being viewed // not being viewed
C func(closure func() string) C func(closure func() string)
// Chk is a shortcut for printing if there is an error, or returning true // Chk is a shortcut for printing if there is an error, or returning true
Chk func(e er) bo Chk func(e error) bool
// Err is a pass-through function that uses fmt.Errorf to construct an error // Err is a pass-through function that uses fmt.Errorf to construct an error
// and returns the error after printing it to the log // and returns the error after printing it to the log
Err func(format string, a ...interface{}) er Err func(format string, a ...any) error
LevelPrinter struct { LevelPrinter struct {
Ln Ln
F F
@@ -61,9 +60,9 @@ type (
Err Err
} }
LevelSpec struct { LevelSpec struct {
ID no ID int
Name string Name string
Colorizer func(a ...interface{}) string Colorizer func(a ...any) string
} }
// Entry is a log entry to be printed as json to the log file // Entry is a log entry to be printed as json to the log file
@@ -124,12 +123,12 @@ func init() {
SetLoggers(Info) SetLoggers(Info)
} }
func SetLoggers(level no) { func SetLoggers(level int) {
Main.Log.T.F("log level %s", LevelSpecs[level].Colorizer(LevelNames[level])) Main.Log.T.F("log level %s", LevelSpecs[level].Colorizer(LevelNames[level]))
Level.Store(int32(level)) Level.Store(int32(level))
} }
func GetLogLevel(level string) (i no) { func GetLogLevel(level string) (i int) {
for i = range LevelNames { for i = range LevelNames {
if level == LevelNames[i] { if level == LevelNames[i] {
return i return i
@@ -209,7 +208,7 @@ func GetPrinter(l int32, writer io.Writer) LevelPrinter {
msgCol(GetLoc(2)), msgCol(GetLoc(2)),
) )
}, },
Chk: func(e er) bo { Chk: func(e error) bool {
if Level.Load() < l { if Level.Load() < l {
return e != nil return e != nil
} }
@@ -225,7 +224,7 @@ func GetPrinter(l int32, writer io.Writer) LevelPrinter {
} }
return false return false
}, },
Err: func(format string, a ...interface{}) er { Err: func(format string, a ...interface{}) error {
if Level.Load() < l { if Level.Load() < l {
fmt.Fprintf(writer, fmt.Fprintf(writer,
"%s%s %s %s\n", "%s%s %s %s\n",
@@ -246,8 +245,8 @@ func GetNullPrinter() LevelPrinter {
F: func(format string, a ...interface{}) {}, F: func(format string, a ...interface{}) {},
S: func(a ...interface{}) {}, S: func(a ...interface{}) {},
C: func(closure func() string) {}, C: func(closure func() string) {},
Chk: func(e er) bo { return e != nil }, Chk: func(e error) bool { return e != nil },
Err: func(format string, a ...interface{}) er { return fmt.Errorf(format, a...) }, Err: func(format string, a ...interface{}) error { return fmt.Errorf(format, a...) },
} }
} }
@@ -287,7 +286,7 @@ func Timestamper() (s string) {
timeText := fmt.Sprint(time.Now().UnixNano()) timeText := fmt.Sprint(time.Now().UnixNano())
lt := len(timeText) lt := len(timeText)
lb := lt + 1 lb := lt + 1
var timeBytes = make(by, lb) var timeBytes = make([]byte, lb)
copy(timeBytes[lb-9:lb], timeText[lt-9:lt]) copy(timeBytes[lb-9:lb], timeText[lt-9:lt])
timeBytes[lb-10] = '.' timeBytes[lb-10] = '.'
lb -= 10 lb -= 10
@@ -296,22 +295,22 @@ func Timestamper() (s string) {
return fmt.Sprint(string(timeBytes), " ") return fmt.Sprint(string(timeBytes), " ")
} }
var wd, _ = os.Getwd() // var wd, _ = os.Getwd()
func GetNLoc(n no) (output string) { func GetNLoc(n int) (output string) {
for ; n > 1; n-- { for ; n > 1; n-- {
output += fmt.Sprintf("%s\n", GetLoc(n)) output += fmt.Sprintf("%s\n", GetLoc(n))
} }
return return
} }
func GetLoc(skip no) (output string) { func GetLoc(skip int) (output string) {
_, file, line, _ := runtime.Caller(skip) _, file, line, _ := runtime.Caller(skip)
split := strings.Split(file, wd+string(os.PathSeparator)) // split := strings.Split(file, wd+string(os.PathSeparator))
if len(split) < 2 { // if len(split) < 2 {
output = fmt.Sprintf("%s:%d", file, line) output = fmt.Sprintf("%s:%d", file, line)
} else { // } else {
output = fmt.Sprintf("%s:%d", split[1], line) // output = fmt.Sprintf("%s:%d", split[1], line)
} // }
return return
} }

View File

@@ -1,20 +0,0 @@
package lol
import (
"bytes"
"realy.lol/context"
)
type (
bo = bool
by = []byte
st = string
er = error
no = int
cx = context.T
)
var (
equals = bytes.Equal
)