Skip to content

Commit

Permalink
clean up realy of unused junk
Browse files Browse the repository at this point in the history
  • Loading branch information
mleku committed Nov 29, 2024
1 parent 5106d76 commit ff007ac
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 84 deletions.
11 changes: 6 additions & 5 deletions cmd/realy/app/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ import (
"realy.lol/ints"
"realy.lol/kind"
"realy.lol/kinds"
"realy.lol/realy/config"
"realy.lol/store"
"realy.lol/tag"
)

type Relay struct {
sync.Mutex
*Config
*config.C
Store store.I
// Owners' pubkeys
Owners []B
Expand All @@ -36,12 +37,12 @@ type Relay struct {
OwnersMuteLists []B
}

func (r *Relay) Name() S { return r.Config.AppName }
func (r *Relay) Name() S { return r.C.AppName }

func (r *Relay) Storage(c context.T) store.I { return r.Store }

func (r *Relay) Init() (err E) {
for _, src := range r.Config.Owners {
for _, src := range r.C.Owners {
if len(src) < 1 {
continue
}
Expand Down Expand Up @@ -349,12 +350,12 @@ func (r *Relay) CheckOwnerLists(c context.T) {
}
}

func (r *Relay) AuthEnabled() bool { return r.Config.AuthRequired }
func (r *Relay) AuthEnabled() bool { return r.C.AuthRequired }

// ServiceUrl returns the address of the relay to send back in auth responses.
// If auth is disabled this returns an empty string.
func (r *Relay) ServiceUrl(req *http.Request) (s S) {
if !r.Config.AuthRequired {
if !r.C.AuthRequired {
return
}
host := req.Header.Get("X-Forwarded-Host")
Expand Down
16 changes: 9 additions & 7 deletions cmd/realy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,37 @@ import (
"time"

"github.com/pkg/profile"

"realy.lol/cmd/realy/app"
"realy.lol/context"
"realy.lol/interrupt"
"realy.lol/lol"
"realy.lol/ratel"
"realy.lol/realy"
"realy.lol/realy/config"
"realy.lol/units"
)

func main() {
var err E
var cfg *app.Config
if cfg, err = app.NewConfig(); chk.T(err) || app.HelpRequested() {
var cfg *config.C
if cfg, err = config.New(); chk.T(err) || config.HelpRequested() {
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %s\n\n", err)
}
app.PrintHelp(cfg, os.Stderr)
config.PrintHelp(cfg, os.Stderr)
os.Exit(0)
}
if app.GetEnv() {
app.PrintEnv(cfg, os.Stdout)
if config.GetEnv() {
config.PrintEnv(cfg, os.Stdout)
os.Exit(0)
}
log.I.Ln("log level", cfg.LogLevel)
lol.SetLogLevel(cfg.LogLevel)
if cfg.Pprof {
defer profile.Start(profile.MemProfile).Stop()
go func() {
http.ListenAndServe("127.0.0.1:6060", nil)
chk.E(http.ListenAndServe("127.0.0.1:6060", nil))
}()
}
debug.SetMemoryLimit(int64(cfg.MemLimit))
Expand All @@ -59,7 +61,7 @@ func main() {
},
},
)
r := &app.Relay{Config: cfg, Store: storage}
r := &app.Relay{C: cfg, Store: storage}
go app.MonitorResources(c)
var server *realy.Server
if server, err = realy.NewServer(realy.ServerParams{
Expand Down
7 changes: 0 additions & 7 deletions realy/broadcasting.go

This file was deleted.

12 changes: 6 additions & 6 deletions cmd/realy/app/config.go → realy/config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package app
package config

import (
"fmt"
Expand All @@ -16,7 +16,7 @@ import (
"realy.lol/sha256"
)

type Config struct {
type C struct {
AppName S `env:"APP_NAME" default:"realy"`
Profile S `env:"PROFILE" usage:"root path for all other path configurations (based on APP_NAME and OS specific location)"`
Listen S `env:"LISTEN" default:"0.0.0.0" usage:"network listen address"`
Expand All @@ -36,8 +36,8 @@ type Config struct {
NWC S `env:"NWC" usage:"NWC connection string for relay to interact with an NWC enabled wallet"`
}

func NewConfig() (cfg *Config, err E) {
cfg = &Config{}
func New() (cfg *C, err E) {
cfg = &C{}
if err = env.Load(cfg, nil); chk.T(err) {
return
}
Expand Down Expand Up @@ -87,7 +87,7 @@ func GetEnv() (requested bool) {
return
}

func PrintEnv(cfg *Config, printer io.Writer) {
func PrintEnv(cfg *C, printer io.Writer) {
t := reflect.TypeOf(*cfg)

for i := 0; i < t.NumField(); i++ {
Expand All @@ -111,7 +111,7 @@ func PrintEnv(cfg *Config, printer io.Writer) {

// PrintHelp outputs a help text listing the configuration options and default
// values to a provided io.Writer (usually os.Stderr or os.Stdout).
func PrintHelp(cfg *Config, printer io.Writer) {
func PrintHelp(cfg *C, printer io.Writer) {
_, _ = fmt.Fprintf(printer,
"Environment variables that configure %s:\n\n", cfg.AppName)
env.Usage(cfg, printer, &env.Options{SliceSep: ","})
Expand Down
21 changes: 21 additions & 0 deletions realy/config/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package config

import (
"bytes"

"realy.lol/context"
"realy.lol/lol"
)

type (
B = []byte
S = string
E = error
N = int
Ctx = context.T
)

var (
log, chk, errorf = lol.Main.Log, lol.Main.Check, lol.Main.Errorf
equals = bytes.Equal
)
11 changes: 0 additions & 11 deletions realy/extra.go

This file was deleted.

2 changes: 1 addition & 1 deletion realy/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const (
pingPeriod = pongWait / 2

// Maximum message size allowed from peer.
maxMessageSize = 500 * units.Mb
maxMessageSize = 1 * units.Mb
)

// TODO: consider moving these to Server as config params
Expand Down
32 changes: 0 additions & 32 deletions realy/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"realy.lol/envelopes/eventenvelope"
"realy.lol/event"
"realy.lol/filter"
"realy.lol/filters"
"realy.lol/tag"
"realy.lol/web"
Expand All @@ -20,37 +19,6 @@ var (
listenersMutex sync.Mutex
)

func GetListeningFilters() *filters.T {
respfilters := &filters.T{F: make([]*filter.T, 0, len(listeners)*2)}

listenersMutex.Lock()
defer listenersMutex.Unlock()

// here we go through all the existing listeners
for _, connlisteners := range listeners {
for _, listener := range connlisteners {
for _, listenerfilter := range listener.filters.F {
for _, respfilter := range respfilters.F {
// check if this filter specifically is already added to respfilters
if filter.Equal(listenerfilter, respfilter) {
goto nextconn
}
}

// field not yet present on respfilters, add it
respfilters.F = append(respfilters.F, listenerfilter)

// continue to the next filter
nextconn:
continue
}
}
}

// respfilters will be a slice with all the distinct filter we currently have active
return respfilters
}

func setListener(id S, ws *web.Socket, ff *filters.T) {
listenersMutex.Lock()
defer listenersMutex.Unlock()
Expand Down
6 changes: 0 additions & 6 deletions realy/notice.go

This file was deleted.

2 changes: 1 addition & 1 deletion realy/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
// [Informationer], [CustomWebSocketHandler], [ShutdownAware] and AdvancedXxx types.
// See their respective doc comments.
//
// The basic usage is to call Start or StartConf, which starts serving immediately.
// The basic usage is to call Start, which starts serving immediately.
// For a more fine-grained control, use NewServer.
type Server struct {
Ctx
Expand Down
10 changes: 7 additions & 3 deletions realy/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,16 @@ func TestServerStartShutdown(t *testing.T) {
init: func() E { storeInited = true; return nil },
},
}
srv, _ := NewServer(c, cancel, rl, "", ratel.DefaultMaxLimit)
srv, _ := NewServer(ServerParams{
Ctx: c,
Cancel: cancel,
Rl: rl,
MaxLimit: ratel.DefaultMaxLimit,
})
ready := make(chan bool)
done := make(chan E)
go func() {
done <- srv.Start("127.0.0.1", 0,
"127.0.0.1", 9999, ready)
done <- srv.Start("127.0.0.1", 0, ready)
close(done)
}()
<-ready
Expand Down
12 changes: 7 additions & 5 deletions realy/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import (

func startTestRelay(c context.T, t *testing.T, tr *testRelay) *Server {
t.Helper()
srv, _ := NewServer(c, func() {}, tr, "", 500*units.Kb)
srv, _ := NewServer(ServerParams{
c, func() {}, tr, "", 500 * units.Kb, "", "",
})
started := make(chan bool)
go srv.Start("127.0.0.1", 0, "127.0.0.1", 0, started)
go srv.Start("127.0.0.1", 0, started)
<-started
return srv
}
Expand Down Expand Up @@ -52,11 +54,11 @@ func (tr *testRelay) OnShutdown(ctx context.T) {
}

func (tr *testRelay) AcceptEvent(c context.T, evt *event.T, hr *http.Request, origin S,
authedPubkey B) (ok bool, notice S) {
authedPubkey B) (ok bool, notice S, after func()) {
if fn := tr.acceptEvent; fn != nil {
return fn(evt), ""
return fn(evt), "", nil
}
return true, ""
return true, "", nil
}

type testStorage struct {
Expand Down

0 comments on commit ff007ac

Please sign in to comment.