Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: basic server sync #8

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
module github.com/oolong-sh/oolong

go 1.22.7
go 1.23.2

require github.com/yuin/gopher-lua v1.1.1

require (
github.com/aaaton/golem/v4 v4.0.0
github.com/aaaton/golem/v4 v4.0.1
github.com/aaaton/golem/v4/dicts/en v1.0.1
)

require github.com/BurntSushi/toml v1.4.0
require (
github.com/BurntSushi/toml v1.4.0
github.com/oolong-sh/sync v0.0.0-20241128232107-4aff73e779a0
)

require (
github.com/kr/fs v0.1.0 // indirect
github.com/pkg/sftp v1.13.7 // indirect
golang.org/x/crypto v0.29.0 // indirect
)

require (
github.com/fsnotify/fsnotify v1.8.0
golang.org/x/sys v0.13.0 // indirect
golang.org/x/sys v0.27.0 // indirect
)
69 changes: 66 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,75 @@
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/aaaton/golem/v4 v4.0.0 h1:YHieBS+5Fqir298nJ7fk3EvMcKM/+T5gpMRt4TIAiZ8=
github.com/aaaton/golem/v4 v4.0.0/go.mod h1:OfK/S5v9Exsx1yO21WorREuIVV+Y5K2hygP0A9oJCCI=
github.com/aaaton/golem/v4 v4.0.1 h1:jvnnTmzdfZC8cUGIo6obIcnmB3stTaf5Uw64OMx3C84=
github.com/aaaton/golem/v4 v4.0.1/go.mod h1:OfK/S5v9Exsx1yO21WorREuIVV+Y5K2hygP0A9oJCCI=
github.com/aaaton/golem/v4/dicts/en v1.0.1 h1:/BsOsh8JTgTkuevwM9axPnAi9CD4rK7TWHNdW/6V3Uo=
github.com/aaaton/golem/v4/dicts/en v1.0.1/go.mod h1:1YKRrQNng+KbS+peA7sj3TIa8eqR6T2UqdJ+Tc9xeoA=
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.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/oolong-sh/sync v0.0.0-20241128232107-4aff73e779a0 h1:xQq7hGer2MRvHjLK4L+bLPFyCnwyFiiy/1F5Ap2LS30=
github.com/oolong-sh/sync v0.0.0-20241128232107-4aff73e779a0/go.mod h1:R+SR70YgYzJHvs81TcrFk6q2zcSFI9DoTo7d3yBPWHM=
github.com/pkg/sftp v1.13.7 h1:uv+I3nNJvlKZIQGSr8JVQLNHFU9YhhNpvC14Y6KgmSM=
github.com/pkg/sftp v1.13.7/go.mod h1:KMKI0t3T6hfA+lTR/ssZdunHo+uwq7ghoN09/FSu3DY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
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/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=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
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=
golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ=
golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
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/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=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0=
golang.org/x/term v0.26.0 h1:WEQa6V3Gja/BhNxg540hBip/kkaYtRg3cxg4oXSw4AU=
golang.org/x/term v0.26.0/go.mod h1:Si5m1o57C5nBNQo5z1iq+XDijt21BDBDp2bK0QI8e3E=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
14 changes: 6 additions & 8 deletions internal/api/api_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,25 @@ func checkOrigin(w http.ResponseWriter, r *http.Request) error {
origin := r.Header.Get("Origin")

// check if the origin is whitelisted
if !slices.Contains(allowedOrigins, origin) {
http.Error(w, "Request origin not in allow list", http.StatusForbidden)
return fmt.Errorf("Requesting client not in allow list. Origin: %s\n", origin)
if origin != "" {
if !slices.Contains(allowedOrigins, origin) {
http.Error(w, "Request origin not in allow list", http.StatusForbidden)
return fmt.Errorf("Requesting client not in allow list. Origin: %s\n", origin)
}
w.Header().Set("Access-Control-Allow-Origin", origin)
}

w.Header().Set("Access-Control-Allow-Origin", origin)

return nil
}

// exists returns whether the given file or directory exists
func exists(path string) (bool, error) {
_, err := os.Stat(path)

if err == nil {
return true, nil
}

if os.IsNotExist(err) {
return false, nil
}

return false, err
}
6 changes: 2 additions & 4 deletions internal/api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
func SpawnServer() {
mux := http.NewServeMux()

// TODO: add some sort of JWT system for better security

// graph endpoints
mux.HandleFunc("GET /graph", handleGetGraph)

Expand All @@ -19,10 +21,6 @@ func SpawnServer() {
mux.HandleFunc("PUT /note", handleUpdateNote)
mux.HandleFunc("DELETE /note", handleDeleteNote)

// keyword endpoints?

// plugin endpoints? (probably not these outside of official ones)

// start server
log.Println("Starting server on :11975...")
if err := http.ListenAndServe(":11975", mux); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/api/graph_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func handleGetGraph(w http.ResponseWriter, r *http.Request) {
if err := checkOrigin(w, r); err != nil {
log.Println(err)
http.Error(w, fmt.Sprintln(err), 500)
return
}

// get snapshot of current state
Expand All @@ -29,7 +30,6 @@ func handleGetGraph(w http.ResponseWriter, r *http.Request) {
notes := notes.DocumentsToNotes(s.Documents)

// serialize graph data
// TODO: pass in thresholds (with request? or with config?)
data, err := graph.SerializeGraph(kw, notes)
if err != nil {
http.Error(w, "Error serializing graph data", 500)
Expand Down
5 changes: 5 additions & 0 deletions internal/api/note_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func handleGetNotes(w http.ResponseWriter, r *http.Request) {
if err := checkOrigin(w, r); err != nil {
log.Println(err)
http.Error(w, fmt.Sprintln(err), 500)
return
}

s := state.State()
Expand All @@ -44,6 +45,7 @@ func handleGetNote(w http.ResponseWriter, r *http.Request) {
if err := checkOrigin(w, r); err != nil {
log.Println(err)
http.Error(w, fmt.Sprintln(err), 500)
return
}

path := r.URL.Query().Get("path")
Expand Down Expand Up @@ -77,6 +79,7 @@ func handleCreateNote(w http.ResponseWriter, r *http.Request) {
if err := checkOrigin(w, r); err != nil {
log.Println(err)
http.Error(w, fmt.Sprintln(err), 500)
return
}

// parse request body
Expand Down Expand Up @@ -121,6 +124,7 @@ func handleUpdateNote(w http.ResponseWriter, r *http.Request) {
if err := checkOrigin(w, r); err != nil {
log.Println(err)
http.Error(w, fmt.Sprintln(err), 500)
return
}

// parse request body
Expand All @@ -146,6 +150,7 @@ func handleDeleteNote(w http.ResponseWriter, r *http.Request) {
if err := checkOrigin(w, r); err != nil {
log.Println(err)
http.Error(w, fmt.Sprintln(err), 500)
return
}

path := r.URL.Query().Get("path")
Expand Down
24 changes: 7 additions & 17 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package config

import (
"os"
"os/user"
"path/filepath"

"github.com/BurntSushi/toml"
"github.com/oolong-sh/sync"
)

var cfg OolongConfig

type OolongSyncConfig sync.SyncConfig

type OolongConfig struct {
NotesDirPaths []string `toml:"note_directories"`
NGramRange []int `toml:"ngram_range"`
Expand All @@ -19,6 +20,7 @@ type OolongConfig struct {

PluginsConfig OolongPluginConfig `toml:"plugins"`
GraphConfig OolongGraphConfig `toml:"graph"`
SyncConfig OolongSyncConfig `toml:"sync"`
}

type OolongPluginConfig struct {
Expand All @@ -40,12 +42,12 @@ func PluginPaths() []string { return cfg.PluginsConfig.PluginPaths
func IgnoredDirectories() []string { return cfg.IgnoreDirectories }
func StopWords() []string { return cfg.StopWords }
func WeightThresholds() OolongGraphConfig { return cfg.GraphConfig }
func SyncConfig() OolongSyncConfig { return cfg.SyncConfig }

// TODO: file watcher for config file, reload on change

func Setup(configPath string) error {
var err error
configPath, err = expand(configPath)
func Setup() error {
configPath, err := findConfigPath()
if err != nil {
panic(err)
}
Expand All @@ -72,15 +74,3 @@ func Setup(configPath string) error {

return nil
}

func expand(path string) (string, error) {
if len(path) == 0 || path[0] != '~' {
return path, nil
}

usr, err := user.Current()
if err != nil {
return "", err
}
return filepath.Join(usr.HomeDir, path[1:]), nil
}
65 changes: 65 additions & 0 deletions internal/config/config_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package config

import (
"fmt"
"os"
"os/user"
"path/filepath"
)

func findConfigPath() (string, error) {
var err error

// read config file location from env var, fall back to ~/.config/oolong.toml
configPath := os.Getenv("OOLONG_CONFIG_PATH")
if configPath == "" {
configPath, err = checkDefaultLocations()
if err != nil {
return "", err
}
}
fmt.Println(configPath)

configPath, err = expand(configPath)
if err != nil {
return "", err
}

return configPath, nil
}

func checkDefaultLocations() (string, error) {
configPaths := []string{
"~/.config/oolong.toml",
"~/.config/oolong/oolong.toml",
"~/oolong.toml",
"~/.oolong.toml",
"/etc/oolong/oolong.toml",
"./oolong.toml",
}

for _, p := range configPaths {
configPath, err := expand(p)
if err != nil {
continue
}

if _, err := os.Stat(configPath); err == nil {
return configPath, nil
}
}

return "", fmt.Errorf("No fallback config file found.")
}

func expand(path string) (string, error) {
if len(path) == 0 || path[0] != '~' {
return path, nil
}

usr, err := user.Current()
if err != nil {
return "", err
}
return filepath.Join(usr.HomeDir, path[1:]), nil
}
31 changes: 31 additions & 0 deletions internal/daemon/sync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package daemon

import (
"log"

"github.com/oolong-sh/oolong/internal/config"
"github.com/oolong-sh/sync"
)

func Sync(files ...string) error {
syncConfig := config.SyncConfig()

// stop if synchronization is not enabled (no config/bad config)
if syncConfig.Host == "" {
log.Println("Synchronization not enabled.")
return nil
}

s, err := sync.NewClient(sync.SyncConfig(syncConfig))
if err != nil {
return err
}

if err := s.Update(files...); err != nil {
return err
}

log.Println("Done syncing.")

return nil
}
Loading