Skip to content

Commit

Permalink
feat: add pinned notes using bbolt
Browse files Browse the repository at this point in the history
  • Loading branch information
JackWilli committed Dec 8, 2024
1 parent 8475296 commit 79d7b02
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
require (
github.com/BurntSushi/toml v1.4.0
github.com/oolong-sh/sync v0.0.0-20241128232107-4aff73e779a0
go.etcd.io/bbolt v1.3.11
)

require (
Expand Down
7 changes: 6 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
go.etcd.io/bbolt v1.3.11 h1:yGEzV1wPz2yVCLsD8ZAiGHhHVlczyC9d1rP43/VCRJ0=
go.etcd.io/bbolt v1.3.11/go.mod h1:dksAq7YMXoljX0xu6VF5DMZGbhYYoLUalEiSySYAS4I=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
Expand All @@ -41,6 +44,8 @@ golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
5 changes: 5 additions & 0 deletions internal/api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func SpawnServer() {
mux.HandleFunc("PUT /note", handleUpdateNote)
mux.HandleFunc("DELETE /note", handleDeleteNote)

// pinning endpoints
mux.HandleFunc("GET /pins", handleGetPinnedNotes)
mux.HandleFunc("POST /pins", handleAddPinnedNote)
mux.HandleFunc("DELETE /pins", handleDeletePinnedNote)

// start server
log.Println("Starting server on :11975...")
if err := http.ListenAndServe(":11975", mux); err != nil {
Expand Down
122 changes: 121 additions & 1 deletion internal/api/note_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"os"
"path/filepath"

"github.com/oolong-sh/oolong/internal/db"
"github.com/oolong-sh/oolong/internal/state"
"go.etcd.io/bbolt"
)

type createUpdateRequest struct {
Expand Down Expand Up @@ -142,7 +144,7 @@ func handleUpdateNote(w http.ResponseWriter, r *http.Request) {
}
}

// 'Delete /note?path=/path/to/note' endpoint handler deletess a note file based on query input
// 'Delete /note?path=/path/to/note' endpoint handler deletes a note file based on query input
func handleDeleteNote(w http.ResponseWriter, r *http.Request) {
log.Println("Request received:", r.Method, r.URL, r.Host)

Expand All @@ -169,3 +171,121 @@ func handleDeleteNote(w http.ResponseWriter, r *http.Request) {
// NOTE: this function may need to call the update function due to files no longer existing
// - check this case in state, this may require substantial logic missing there
}

func addPinnedNote(path string) error {
return db.Database.Update(func(tx *bbolt.Tx) error {
bucket := tx.Bucket([]byte(db.PinnedBucket))
if bucket == nil {
return fmt.Errorf("bucket %s not found", db.PinnedBucket)
}

if bucket.Get([]byte(path)) != nil {
return fmt.Errorf("note already pinned")
}

return bucket.Put([]byte(path), []byte{})
})
}

func getPinnedNotes() ([]string, error) {
notes := []string{}

err := db.Database.View(func(tx *bbolt.Tx) error {
bucket := tx.Bucket([]byte(db.PinnedBucket))
if bucket == nil {
return fmt.Errorf("bucket %s not found", db.PinnedBucket)
}

return bucket.ForEach(func(k, _ []byte) error {
notes = append(notes, string(k))
return nil
})
})

return notes, err
}

func deletePinnedNote(path string) error {
return db.Database.Update(func(tx *bbolt.Tx) error {
bucket := tx.Bucket([]byte(db.PinnedBucket))
if bucket == nil {
return fmt.Errorf("bucket %s not found", db.PinnedBucket)
}

return bucket.Delete([]byte(path))
})
}

func handleGetPinnedNotes(w http.ResponseWriter, r *http.Request) {
log.Println("Request received:", r.Method, r.URL, r.Host)

// CORS handling
if err := checkOrigin(w, r); err != nil {
log.Println(err)
http.Error(w, fmt.Sprintln(err), 500)
return
}

pinnedNotes, err := getPinnedNotes()
if err != nil {
log.Println(err)
http.Error(w, "Failed to fetch pinned notes", 500)
return
}

w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(pinnedNotes); err != nil {
log.Println(err)
http.Error(w, "Failed to encode response", 500)
}
}

func handleAddPinnedNote(w http.ResponseWriter, r *http.Request) {
log.Println("Request received:", r.Method, r.URL, r.Host)

// CORS handling
if err := checkOrigin(w, r); err != nil {
log.Println(err)
http.Error(w, fmt.Sprintln(err), 500)
return
}

path := r.URL.Query().Get("path")
if path == "" {
http.Error(w, "Path parameter not specified", http.StatusBadRequest)
return
}

if err := addPinnedNote(path); err != nil {
log.Println(err)
http.Error(w, err.Error(), 400)
return
}

w.WriteHeader(http.StatusCreated)
}

func handleDeletePinnedNote(w http.ResponseWriter, r *http.Request) {
log.Println("Request received:", r.Method, r.URL, r.Host)

// CORS handling
if err := checkOrigin(w, r); err != nil {
log.Println(err)
http.Error(w, fmt.Sprintln(err), 500)
return
}

path := r.URL.Query().Get("path")
if path == "" {
http.Error(w, "Path parameter not specified", http.StatusBadRequest)
return
}

if err := deletePinnedNote(path); err != nil {
log.Println(err)
http.Error(w, "Failed to delete pinned note", 400)
return
}

w.WriteHeader(http.StatusNoContent)
}
31 changes: 31 additions & 0 deletions internal/db/db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package db

import (
"go.etcd.io/bbolt"
)

var Database *bbolt.DB

const PinnedBucket = "PinnedNotes"
const db_path = "pinned_notes.db"

// Initialize the database and ensure the bucket exists
func InitializeDB() error {
var err error
Database, err = bbolt.Open(db_path, 0666, nil)
if err != nil {
return err
}

return Database.Update(func(tx *bbolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(PinnedBucket))
return err
})
}

// Close the database when the server shuts down
func CloseDB() {
if Database != nil {
Database.Close()
}
}
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/oolong-sh/oolong/internal/config"
"github.com/oolong-sh/oolong/internal/daemon"
"github.com/oolong-sh/oolong/internal/db"
"github.com/oolong-sh/oolong/internal/documents"
"github.com/oolong-sh/oolong/internal/linking/ngrams"
"github.com/oolong-sh/oolong/internal/state"
Expand All @@ -29,6 +30,11 @@ func main() {
panic(err)
}

if err := db.InitializeDB(); err != nil {
panic(err)
}
defer db.CloseDB()

// go plugins.InitPlugins()

// run daemon if --no-daemon flag is not passed
Expand Down

0 comments on commit 79d7b02

Please sign in to comment.