Skip to content

Commit 2728c6d

Browse files
committed
server.go: Update graphql enpoints, update readme.
1 parent 75152fd commit 2728c6d

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
### Export notes as html using the Kindle App
2+
![Export Notes From Kindle App](../screenshots/export-notes-from-kindle-app.png)

README.md

+36-5
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,8 @@ This tool supports both **HTML highlights** exported via the Kindle App and the
3232
./blueNote convert -i kindle-my-clippings -o json --json.pretty examples/My\ Clippings.txt
3333
```
3434

35-
### Export notes as html using the Kindle App
36-
![Export Notes From Kindle App](screenshots/export-notes-from-kindle-app.png)
37-
38-
39-
### Convert notes to JSON and display them in the console
35+
### Convert `Kindle HTML` notes to JSON and display them in the console
36+
(click [here](Documents/HOW_TO_EXPORT_KINDLE_HTML_HIGHLIGHTS.md) to see how to export the kindle HTML notes via Kindle App)
4037
```
4138
./blueNote convert -i kindle-html -o json --json.pretty examples/kindle_html_single_book_example.html
4239
```
@@ -46,16 +43,49 @@ This tool supports both **HTML highlights** exported via the Kindle App and the
4643
./blueNote convert -i kindle-html -o mongodb examples/kindle_html_single_book_example.html
4744
```
4845

46+
<!-- deprecated
4947
### Convert notes to org-roam files and save to the current dir
5048
```
5149
./blueNote convert -i kindle-html -o org-roam examples/kindle_html_single_book_example.html ./
5250
```
51+
-->
5352

5453
### Add `-s` if the book is a collection of multiple books
5554
```
5655
./blueNote convert -i kindle-html -o json --json.pretty -s examples/kindle_html_collection_example.html
5756
```
5857

58+
### Run as an http server that serves data from the MongoDB via GraphQL enpoints
59+
60+
```
61+
./blueNote server
62+
```
63+
64+
### Query the highlights using the GraphQL API
65+
66+
```
67+
curl -X POST \
68+
-H "Content-Type: application/json" \
69+
-d '{"query": "query { marks(author: \"Maugham\") { type title author data note tags createdAt lastModifiedAt } }"}' \
70+
http://localhost:11212/graphql 2>/dev/null | jq .
71+
{
72+
"data": {
73+
"marks": [
74+
{
75+
"author": "Maugham, W. Somerset",
76+
"createdAt": 1733038917438,
77+
"data": "trouble, much resented the churchwarden's managing ways. He really seemed to look upon himself as the most important person in the parish. Mr. Carey constantly told his wife that if Josiah Graves did not take care he would give him a good rap over the knuckles one day; but Mrs. Carey advised him to bear with Josiah Graves: he meant well, and it was not",
78+
"lastModifiedAt": 1733038917438,
79+
"note": "",
80+
"tags": [],
81+
"title": "Of Human Bondage",
82+
"type": "HIGHLIGHT"
83+
}
84+
]
85+
}
86+
}
87+
```
88+
5989

6090
<!--### Browse and edit the notes with tags in Emacs Org
6191
![View and Edit Notes in Emacs Org-roam](screenshots/view-notes-with-emacs-org-roam.png)
@@ -103,6 +133,7 @@ Remember to run `M-x org-roam-db-sync` to sync the org-roam database.
103133
- [x] `My Clippings.txt` parser.
104134
- [ ] Diff the previous processed `My Clippings.txt`
105135
- [ ] Support multiple authors.
136+
- [ ] Add progress indicator
106137

107138
### Server Backend
108139
- [x] Database storage.

pkg/server/server.go

+26-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"encoding/json"
1010
"errors"
1111
"fmt"
12+
"io"
1213
"net/http"
1314

1415
"github.com/graphql-go/graphql"
@@ -30,14 +31,37 @@ func NewServer(config *config.ServerConfig, store storage.Storage) Server {
3031

3132
func (s *server) Run() {
3233
schema = s.graphqlSchema()
33-
http.HandleFunc("/marks", handleGraphqlMarks)
34+
http.HandleFunc("/graphql", handleGraphqlMarks)
3435
util.Logf("Server is running on %v\n", s.config.ListenAddr)
3536
http.ListenAndServe(s.config.ListenAddr, nil)
3637

3738
}
3839

3940
func handleGraphqlMarks(w http.ResponseWriter, r *http.Request) {
40-
result := executeQuery(r.Context(), r.URL.Query().Get("query"))
41+
// Ensure the request method is POST
42+
if r.Method != http.MethodPost {
43+
http.Error(w, "Only POST requests are allowed", http.StatusMethodNotAllowed)
44+
return
45+
}
46+
47+
// Read the POST body
48+
body, err := io.ReadAll(r.Body)
49+
if err != nil {
50+
http.Error(w, "Failed to read request body", http.StatusInternalServerError)
51+
return
52+
}
53+
defer r.Body.Close()
54+
55+
// Parse the body as JSON
56+
var requestData map[string]interface{}
57+
if err := json.Unmarshal(body, &requestData); err != nil {
58+
http.Error(w, "Invalid JSON in request body", http.StatusBadRequest)
59+
return
60+
}
61+
62+
util.Logf("Received request from %v, request is %v\n", r.Host, requestData)
63+
64+
result := executeQuery(r.Context(), requestData["query"].(string))
4165
json.NewEncoder(w).Encode(result)
4266
}
4367

0 commit comments

Comments
 (0)