diff --git a/go.mod b/go.mod index 6279319..877e3e8 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,10 @@ module git.minhas.io/asara/well-goknown go 1.19 -require github.com/go-redis/redis v6.15.9+incompatible +require ( + github.com/davecgh/go-spew v1.1.1 + github.com/go-redis/redis v6.15.9+incompatible +) require ( github.com/onsi/ginkgo v1.16.5 // indirect diff --git a/go.sum b/go.sum index 9e49489..dacac62 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,5 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= diff --git a/lnd/lnd.go b/lnd/lnd.go new file mode 100644 index 0000000..11893d7 --- /dev/null +++ b/lnd/lnd.go @@ -0,0 +1,8 @@ +package lnd + +import "fmt" + +func main() { + // https://bitcoin.stackexchange.com/questions/73869/rpc-call-to-lnd + fmt.Println("vim-go") +} diff --git a/main.go b/main.go index a1c0eb5..17da593 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,21 @@ import ( ) func main() { + /* + nostrTest := nostr.NostrRequest{ + Name: "asara", + Key: "abcdefg", + Hostname: "devvul.com", + } + nostr.AddNostrAddr(nostrTest) + */ + // matrix endpoints http.HandleFunc("/.well-known/matrix/server", matrix.Matrix) + + // nostr endpoints http.HandleFunc("/.well-known/nostr.json", nostr.GetNostrAddr) + http.HandleFunc("/request/nostr", nostr.RequestNostrAddr) + + // start server http.ListenAndServe(":8090", nil) } diff --git a/nostr/nostr.go b/nostr/nostr.go index 9512ddb..a98cb87 100644 --- a/nostr/nostr.go +++ b/nostr/nostr.go @@ -1,11 +1,12 @@ package nostr import ( + "encoding/json" "fmt" "net/http" "strings" - "github.com/go-redis/redis" + "git.minhas.io/asara/well-goknown/redis" ) type nostrWellKnown struct { @@ -17,46 +18,51 @@ type NostrRequest struct { Name string `json:"name"` Key string `json:"key"` Hostname string - Relays []string `json:"relays"` + Relays []string `json:"relays,omitempty"` } -/* func AddNostrAddr(n NostrRequest) { // transform request to well known struct + relays := make(map[string][]string) + user := nostrWellKnown{} names := map[string]string{n.Name: n.Key} - relays := map[string][]string{n.Key: n.Relays} - user := nostrWellKnown{Names: names, Relays: relays} + if n.Relays != nil { + relays = map[string][]string{n.Key: n.Relays} + user = nostrWellKnown{Names: names, Relays: relays} + } else { + user = nostrWellKnown{Names: names} + } jsonUser, _ := json.Marshal(user) - client := redis.NewClient(&redis.Options{ - Addr: "localhost:6379", - Password: "", - DB: 0, - }) + r, err := redis.New("localhost:6379", "", 0) + if err != nil { + fmt.Println("FAILED") + } nameKey := fmt.Sprintf("%s@%s", n.Name, n.Hostname) - err := client.Set(nameKey, jsonUser, 0).Err() + err = r.Client.Set(nameKey, jsonUser, 0).Err() if err != nil { fmt.Println("FAILED") } } -*/ + +func RequestNostrAddr(w http.ResponseWriter, req *http.Request) { +} func GetNostrAddr(w http.ResponseWriter, req *http.Request) { // get query string for username req.ParseForm() // connect to redis - client := redis.NewClient(&redis.Options{ - Addr: "localhost:6379", - Password: "", - DB: 0, - }) + r, err := redis.New("localhost:6379", "", 0) + if err != nil { + w.WriteHeader(http.StatusNotFound) + return + } requestedName := req.FormValue("name") - hostname := getHostname(req.Host) // search for user@domain - search := fmt.Sprintf("%s@%s", requestedName, hostname) - addr, err := client.Get(search).Result() + search := fmt.Sprintf("%s@%s", requestedName, getHostname(req.Host)) + addr, err := r.Client.Get(search).Result() if err != nil { w.WriteHeader(http.StatusNotFound) return diff --git a/redis/redis.go b/redis/redis.go new file mode 100644 index 0000000..0f27fc6 --- /dev/null +++ b/redis/redis.go @@ -0,0 +1,21 @@ +package redis + +import "github.com/go-redis/redis" + +type Redis struct { + Client *redis.Client +} + +func New(address string, password string, database int) (*Redis, error) { + client := redis.NewClient(&redis.Options{ + Addr: address, + Password: password, + DB: database, + }) + if err := client.Ping().Err(); err != nil { + return nil, err + } + return &Redis{ + Client: client, + }, nil +}