### Commit Message
Update .gitignore and refactor main.go, pkg/app/main.go ### Modified Files - **.gitignore** - Added `.idea/material_theme_project_new.xml` and `.idea/orly.iml` to be ignored. - **main.go** - Moved `os` import to the top. - Adjusted the order of imports for better readability. - Commented out the unused `profile` import. - **pkg/app/main.go** - Added detailed comments for types and methods in the `Relay` struct. - Reorganized method descriptions for clarity. - Ensured consistent documentation style throughout the file.
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -100,3 +100,5 @@ pkg/database/testrealy
|
|||||||
/.idea/orly.dev.iml
|
/.idea/orly.dev.iml
|
||||||
/.idea/vcs.xml
|
/.idea/vcs.xml
|
||||||
/.idea/codeStyles/codeStyleConfig.xml
|
/.idea/codeStyles/codeStyleConfig.xml
|
||||||
|
/.idea/material_theme_project_new.xml
|
||||||
|
/.idea/orly.iml
|
||||||
|
|||||||
5
main.go
5
main.go
@@ -5,9 +5,11 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/pkg/profile"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
_ "net/http/pprof"
|
_ "net/http/pprof"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/pkg/profile"
|
||||||
app2 "orly.dev/pkg/app"
|
app2 "orly.dev/pkg/app"
|
||||||
"orly.dev/pkg/app/config"
|
"orly.dev/pkg/app/config"
|
||||||
"orly.dev/pkg/app/relay"
|
"orly.dev/pkg/app/relay"
|
||||||
@@ -19,7 +21,6 @@ import (
|
|||||||
"orly.dev/pkg/utils/log"
|
"orly.dev/pkg/utils/log"
|
||||||
"orly.dev/pkg/utils/lol"
|
"orly.dev/pkg/utils/lol"
|
||||||
"orly.dev/pkg/version"
|
"orly.dev/pkg/version"
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
113
pkg/app/main.go
113
pkg/app/main.go
@@ -3,55 +3,83 @@ package app
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"orly.dev/pkg/app/config"
|
"orly.dev/pkg/app/config"
|
||||||
"orly.dev/pkg/encoders/event"
|
"orly.dev/pkg/encoders/event"
|
||||||
"orly.dev/pkg/encoders/filter"
|
"orly.dev/pkg/encoders/filter"
|
||||||
"orly.dev/pkg/encoders/filters"
|
"orly.dev/pkg/encoders/filters"
|
||||||
"orly.dev/pkg/interfaces/store"
|
"orly.dev/pkg/interfaces/store"
|
||||||
"orly.dev/pkg/utils/context"
|
"orly.dev/pkg/utils/context"
|
||||||
"sync"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// List represents a set-like structure using a map with empty struct values.
|
||||||
type List map[string]struct{}
|
type List map[string]struct{}
|
||||||
|
|
||||||
|
// Relay is a struct that represents a relay for Nostr events. It contains a
|
||||||
|
// configuration and a persistence layer for storing the events. The Relay
|
||||||
|
// type implements various methods to handle event acceptance, filtering,
|
||||||
|
// and storage.
|
||||||
type Relay struct {
|
type Relay struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
*config.C
|
*config.C
|
||||||
Store store.I
|
Store store.I
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Name returns the name of the application represented by this relay.
|
||||||
|
//
|
||||||
|
// Return Values:
|
||||||
|
//
|
||||||
|
// - string: the name of the application.
|
||||||
|
//
|
||||||
|
// Expected behaviour:
|
||||||
|
//
|
||||||
|
// This function simply returns the AppName field from the configuration.
|
||||||
func (r *Relay) Name() string { return r.C.AppName }
|
func (r *Relay) Name() string { return r.C.AppName }
|
||||||
|
|
||||||
|
// Storage represents a persistence layer for Nostr events handled by a relay.
|
||||||
func (r *Relay) Storage() store.I { return r.Store }
|
func (r *Relay) Storage() store.I { return r.Store }
|
||||||
|
|
||||||
|
// Init initializes and sets up the relay for Nostr events.
|
||||||
|
//
|
||||||
|
// Return Values:
|
||||||
|
//
|
||||||
|
// - err: an error if any issues occurred during initialization.
|
||||||
|
//
|
||||||
|
// Expected behaviour:
|
||||||
|
//
|
||||||
|
// This function is responsible for setting up the relay, configuring it,
|
||||||
|
// and initializing the necessary components to handle Nostr events.
|
||||||
func (r *Relay) Init() (err error) {
|
func (r *Relay) Init() (err error) {
|
||||||
// for _, src := range r.C.Owners {
|
|
||||||
// if len(src) < 1 {
|
|
||||||
// continue
|
|
||||||
// }
|
|
||||||
// dst := make([]byte, len(src)/2)
|
|
||||||
// if _, err = hex.DecBytes(dst, []byte(src)); chk.E(err) {
|
|
||||||
// if dst, err = bech32encoding.NpubToBytes([]byte(src)); chk.E(err) {
|
|
||||||
// continue
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// r.owners = append(r.owners, dst)
|
|
||||||
// }
|
|
||||||
// if len(r.owners) > 0 {
|
|
||||||
// log.F.C(func() string {
|
|
||||||
// ownerIds := make([]string, len(r.owners))
|
|
||||||
// for i, npub := range r.owners {
|
|
||||||
// ownerIds[i] = hex.Enc(npub)
|
|
||||||
// }
|
|
||||||
// owners := strings.Join(ownerIds, ",")
|
|
||||||
// return fmt.Sprintf("owners %s", owners)
|
|
||||||
// })
|
|
||||||
// r.ZeroLists()
|
|
||||||
// r.CheckOwnerLists(context.Bg())
|
|
||||||
// }
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AcceptEvent checks an event and determines whether the event should be
|
||||||
|
// accepted and if the client has the authority to submit it.
|
||||||
|
//
|
||||||
|
// Parameters:
|
||||||
|
//
|
||||||
|
// - c - a context.T for signalling if the task has been canceled.
|
||||||
|
// - evt - an *event.E that is being evaluated.
|
||||||
|
// - hr - an *http.Request containing the information about the current
|
||||||
|
// connection.
|
||||||
|
// - origin - the address of the client.
|
||||||
|
// - authedPubkey - the public key, if authed, of the client for this
|
||||||
|
// connection.
|
||||||
|
//
|
||||||
|
// Return Values:
|
||||||
|
//
|
||||||
|
// - accept - returns true if the event is accepted.
|
||||||
|
// - notice - if it is not accepted,
|
||||||
|
// a message in the form of `machine-readable-prefix: reason for
|
||||||
|
// error/blocked/rate-limited/etc`
|
||||||
|
// - afterSave - a closure to run after the event has been stored.
|
||||||
|
//
|
||||||
|
// Expected behaviour:
|
||||||
|
//
|
||||||
|
// This function checks whether the client has permission to store the event,
|
||||||
|
// and if they don't, returns false and some kind of error message. If they do,
|
||||||
|
// the event is forwarded to the database to be stored and indexed.
|
||||||
func (r *Relay) AcceptEvent(
|
func (r *Relay) AcceptEvent(
|
||||||
c context.T, evt *event.E, hr *http.Request,
|
c context.T, evt *event.E, hr *http.Request,
|
||||||
origin string, authedPubkey []byte,
|
origin string, authedPubkey []byte,
|
||||||
@@ -60,6 +88,23 @@ func (r *Relay) AcceptEvent(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AcceptFilter checks the provided filter against a set of accepted filters and
|
||||||
|
// determines if the filter should be accepted or not.
|
||||||
|
//
|
||||||
|
// Parameters:
|
||||||
|
//
|
||||||
|
// - c - a context.T for signalling if the task has been canceled.
|
||||||
|
// - hr - an *http.Request containing the information about the current
|
||||||
|
// connection.
|
||||||
|
// - f - a *filter.S that needs to be checked.
|
||||||
|
// - authedPubkey - the public key, if authed, of the client for this
|
||||||
|
// connection.
|
||||||
|
//
|
||||||
|
// Return Values:
|
||||||
|
//
|
||||||
|
// - allowed - the filtered filter if it is accepted or nil otherwise
|
||||||
|
// - ok - true if the filter is accepted, false otherwise
|
||||||
|
// - modified - a boolean indicating whether the filter was modified
|
||||||
func (r *Relay) AcceptFilter(
|
func (r *Relay) AcceptFilter(
|
||||||
c context.T, hr *http.Request, f *filter.S,
|
c context.T, hr *http.Request, f *filter.S,
|
||||||
authedPubkey []byte,
|
authedPubkey []byte,
|
||||||
@@ -69,6 +114,24 @@ func (r *Relay) AcceptFilter(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AcceptReq checks an event and determines whether the event should be
|
||||||
|
// accepted and if the client has the authority to submit it.
|
||||||
|
//
|
||||||
|
// Parameters:
|
||||||
|
//
|
||||||
|
// - c - a context.T for signalling if the task has been canceled.
|
||||||
|
// - hr - an *http.Request containing the information about the current
|
||||||
|
// connection.
|
||||||
|
// - id - the ID of the request.
|
||||||
|
// - ff - a set of filters that have been applied to the event, represented
|
||||||
|
// as a filters.T object.
|
||||||
|
// - authedPubkey - the public key, if authed, of the client for this connection.
|
||||||
|
//
|
||||||
|
// Return Values:
|
||||||
|
//
|
||||||
|
// - allowed - a filters.T object representing the accepted filters.
|
||||||
|
// - ok - true if the event is accepted, false otherwise.
|
||||||
|
// - modified - a boolean indicating whether the filters were modified.
|
||||||
func (r *Relay) AcceptReq(
|
func (r *Relay) AcceptReq(
|
||||||
c context.T, hr *http.Request, id []byte,
|
c context.T, hr *http.Request, id []byte,
|
||||||
ff *filters.T, authedPubkey []byte,
|
ff *filters.T, authedPubkey []byte,
|
||||||
|
|||||||
Reference in New Issue
Block a user