Skip to content
This repository has been archived by the owner on Dec 17, 2023. It is now read-only.

Commit

Permalink
feat: make /api/feed endpoint idempotent
Browse files Browse the repository at this point in the history
  • Loading branch information
piraces committed Jan 21, 2023
1 parent 0cfa139 commit 04967ed
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 68 deletions.
29 changes: 7 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
`rsslay` exposes an API to work with it programmatically, so you can automate feed creation and retrieval.
The following operations are available:

### Get a existing feed information

_**Note:** the feed must already be created, if not exists it will return `404 - Not found`_
### Get/Create a feed

- Path: `/api/feed`
- Method: `GET`
- Method: `GET` or `POST`
- Query params **(mandatory)**: `url`
- Example: `GET https://rsslay.nostr.moe/api/feed?url=https://www.engadget.com/rss.xml`
- Result: `application/json`
Expand All @@ -43,24 +41,11 @@ _**Note:** the feed must already be created, if not exists it will return `404 -
}
```

### Create a feed

- Path: `/api/feed`
- Method: `POST`
- Query params **(mandatory)**: `url`
- Example: `POST https://rsslay.nostr.moe/api/feed?url=https://www.engadget.com/rss.xml`
- Result: `application/json`
- Example result:
```json
{
"PubKey": "1e630062dd55226058224a0a1e9b54e09ac121ed13dd5070758816a9c561aeab",
"NPubKey": "npub1re3sqcka253xqkpzfg9pax65uzdvzg0dz0w4qur43qt2n3tp464sswsn92",
"Url": "https://www.engadget.com/rss.xml",
"Error": false,
"ErrorMessage": "",
"ErrorCode": 0
}
```
Example with cURL:
```shell
curl --location --request GET \
'https://rsslay.nostr.moe/api/feed?url=https://nitter.moomoo.me/suhail/rss'
```

## Running the relay from the source

Expand Down
47 changes: 1 addition & 46 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"html/template"
"log"
"net/http"
"net/url"
)

//go:embed templates/*
Expand Down Expand Up @@ -83,9 +82,7 @@ func handleFavicon(w http.ResponseWriter, _ *http.Request) {
}

func handleApiFeed(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet {
handleGetFeedEntry(w, r)
} else if r.Method == http.MethodPost {
if r.Method == http.MethodGet || r.Method == http.MethodPost {
handleCreateFeedEntry(w, r)
} else {
http.Error(w, "Method not supported", http.StatusMethodNotAllowed)
Expand All @@ -107,48 +104,6 @@ func handleCreateFeedEntry(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(response)
}

func handleGetFeedEntry(w http.ResponseWriter, r *http.Request) {
feedUrl := r.URL.Query().Get("url")
if feedUrl == "" {
http.Error(w, "The query parameter 'url' is mandatory", http.StatusBadRequest)
return
}
_, err := url.Parse(feedUrl)
if err != nil {
http.Error(w, "The query parameter 'url' is not a valid URL", http.StatusBadRequest)
return
}
sk := privateKeyFromFeed(feedUrl)
publicKey, err := nostr.GetPublicKey(sk)
if err != nil {
http.Error(w, "Unable to process query for url: "+feedUrl, http.StatusInternalServerError)
return
}

entry, i, err := relay.db.Get([]byte(publicKey))
defer i.Close()
if err != nil {
http.Error(w, "Unable to get entry for url: "+feedUrl, http.StatusInternalServerError)
return
}
var entity Entity
if err := json.Unmarshal(entry, &entity); err != nil {
log.Printf("got invalid json from db at key %s: %v", publicKey, err)
http.Error(w, "Unable to get entry for url: "+feedUrl, http.StatusInternalServerError)
return
}
encodedPublicKey, _ := nip19.EncodePublicKey(publicKey)
response, _ := json.Marshal(Entry{
PubKey: publicKey,
NPubKey: encodedPublicKey,
Url: entity.URL,
})
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(response)
return
}

func createFeedEntry(url string) *Entry {
entry := Entry{
Error: false,
Expand Down

0 comments on commit 04967ed

Please sign in to comment.