Skip to content

Commit

Permalink
feat: optionally run it in system tray
Browse files Browse the repository at this point in the history
  • Loading branch information
fedragon committed Jul 10, 2024
1 parent b7b1a76 commit dbc6a7d
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 5 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ The code can run either as a standalone HTTP server or as a [Vercel Function](ht

### Run as HTTP server

#### Option 1: Headless

Build the binary with

```bash
Expand All @@ -26,6 +28,20 @@ The default server address is `http://localhost:3333`, and can be configured via

The endpoint will be available at `<your_url>/api/bookmarks`.

### Option 2: Show in system tray

Build the binary with

```bash
go build -o bin/tray cmd/tray/main.go
```

and then run it:

```
./bin/tray
```

### Run as Vercel Function

Deploy it to your Vercel account. The endpoint will be available at `<vercel_url>/api/bookmarks`. No code changes are required.
Expand Down
7 changes: 2 additions & 5 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,11 @@ import (
"github.com/kelseyhightower/envconfig"

"github.com/fedragon/bookmarkd/api"
"github.com/fedragon/bookmarkd/internal"
)

type Config struct {
HttpAddress string `envconfig:"BOOKMARKD_HTTP_ADDRESS" default:"0.0.0.0:3333"`
}

func main() {
config := Config{}
config := internal.Config{}
if err := envconfig.Process("", &config); err != nil {
log.Fatal(err)
}
Expand Down
123 changes: 123 additions & 0 deletions cmd/tray/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package main

import (
"context"
"errors"
"fmt"
"log"
"net/http"
"time"

"github.com/energye/systray"
"github.com/energye/systray/icon"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/kelseyhightower/envconfig"
"golang.org/x/sync/errgroup"

"github.com/fedragon/bookmarkd/api"
"github.com/fedragon/bookmarkd/internal"
)

func main() {
systray.Run(onReady, onExit)
}

func onReady() {
ctx, cancel := context.WithCancel(context.Background())
group, gctx := errgroup.WithContext(ctx)
group.Go(func() error {
return run(gctx)
})

systray.SetIcon(icon.Data)
systray.SetTitle("bookmarkd")
systray.SetTooltip("Store bookmarks in Obsidian")
systray.SetOnClick(func(menu systray.IMenu) {
if err := menu.ShowMenu(); err != nil {
fmt.Println(err)
}
})
systray.SetOnDClick(func(_ systray.IMenu) {})
systray.SetOnRClick(func(menu systray.IMenu) {
if err := menu.ShowMenu(); err != nil {
fmt.Println(err)
}
})

mStatus := systray.AddMenuItem("Status: 🍏", "Status")
mStatus.SetIcon(icon.Data)
mStatus.Disable()

group.Go(func() error {
checkStatus(gctx, mStatus)
return nil
})

mQuit := systray.AddMenuItem("Quit", "Quit the whole app")
mQuit.SetIcon(icon.Data)
mQuit.Enable()
mQuit.Click(func() {
fmt.Println("quitting (1)")
cancel()
if err := group.Wait(); err != nil {
fmt.Println(err)
}
fmt.Println("quitting (3)")
systray.Quit()
})
}

func onExit() {}

func checkStatus(ctx context.Context, mStatus *systray.MenuItem) {
client := &http.Client{Timeout: time.Second}
for {
select {
case <-ctx.Done():
fmt.Println("quitting (2)")
return
case <-time.Tick(time.Second * 5):
fmt.Println("updating status...")
res, err := client.Get("http://localhost:3333/api/status")
if err != nil {
mStatus.SetTitle("Status: ❌")
fmt.Println(err)
} else if res.StatusCode != http.StatusOK {
mStatus.SetTitle("Status: 🍎")
} else {
mStatus.SetTitle("Status: 🍏")
}
}
}
}

func run(ctx context.Context) error {
config := internal.Config{}
if err := envconfig.Process("", &config); err != nil {
return err
}

router := chi.NewRouter()
router.Use(middleware.Logger)
router.Get("/api/bookmarks", api.Handle)
router.Get("/api/status", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})

server := &http.Server{Addr: config.HttpAddress, Handler: router}

go func() {
fmt.Println("starting server...")
if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
}()

<-ctx.Done()
if err := server.Shutdown(ctx); err != nil {
return err
}

return nil
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ go 1.22

require (
github.com/JohannesKaufmann/html-to-markdown v1.6.0
github.com/energye/systray v1.0.2
github.com/go-chi/chi/v5 v5.1.0
github.com/go-shiori/go-readability v0.0.0-20240701094332-1070de7e32ef
github.com/kelseyhightower/envconfig v1.4.0
github.com/microcosm-cc/bluemonday v1.0.26
golang.org/x/sync v0.7.0
)

require (
Expand All @@ -16,8 +18,11 @@ require (
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/go-shiori/dom v0.0.0-20230515143342-73569d674e1c // indirect
github.com/godbus/dbus/v5 v5.0.4 // indirect
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/tevino/abool v0.0.0-20220530134649-2bfc934cb23c // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
)
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@ github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd3
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/energye/systray v1.0.2 h1:63R4prQkANtpM2CIA4UrDCuwZFt+FiygG77JYCsNmXc=
github.com/energye/systray v1.0.2/go.mod h1:sp7Q/q/I4/w5ebvpSuJVep71s9Bg7L9ZVp69gBASehM=
github.com/go-chi/chi/v5 v5.1.0 h1:acVI1TYaD+hhedDJ3r54HyA6sExp3HfXq7QWEEY/xMw=
github.com/go-chi/chi/v5 v5.1.0/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
github.com/go-shiori/dom v0.0.0-20230515143342-73569d674e1c h1:wpkoddUomPfHiOziHZixGO5ZBS73cKqVzZipfrLmO1w=
github.com/go-shiori/dom v0.0.0-20230515143342-73569d674e1c/go.mod h1:oVDCh3qjJMLVUSILBRwrm+Bc6RNXGZYtoh9xdvf1ffM=
github.com/go-shiori/go-readability v0.0.0-20240701094332-1070de7e32ef h1:6y2GmHDeuF2xwC5L7fLMTlgnOjm5Jy8RYDI1YYpcOKU=
github.com/go-shiori/go-readability v0.0.0-20240701094332-1070de7e32ef/go.mod h1:jH+l/xV/8x8utphLx72GLIuw9wGhGzrZS5i7arOk8zc=
github.com/godbus/dbus/v5 v5.0.4 h1:9349emZab16e7zQvpmsbtjc18ykshndd8y2PG3sgJbA=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f h1:3BSP1Tbs2djlpprl7wCLuiqMaUh5SJkkzI2gDs+FgLs=
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f/go.mod h1:Pcatq5tYkCW2Q6yrR2VRHlbHpZ/R4/7qyL1TCF7vl14=
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
Expand Down Expand Up @@ -44,6 +48,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tevino/abool v0.0.0-20220530134649-2bfc934cb23c h1:coVla7zpsycc+kA9NXpcvv2E4I7+ii6L5hZO2S6C3kw=
github.com/tevino/abool v0.0.0-20220530134649-2bfc934cb23c/go.mod h1:qc66Pna1RiIsPa7O4Egxxs9OqkuxDX55zznh9K07Tzg=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yuin/goldmark v1.7.1 h1:3bajkSilaCbjdKVsKdZjZCLBNPL9pYzrCakKaf4U49U=
github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
Expand All @@ -68,7 +74,10 @@ golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
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.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.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-20200515095857-1151b9dac4a9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
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=
Expand All @@ -79,6 +88,8 @@ golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.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=
Expand Down
5 changes: 5 additions & 0 deletions internal/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package internal

type Config struct {
HttpAddress string `envconfig:"BOOKMARKD_HTTP_ADDRESS" default:"0.0.0.0:3333"`
}

0 comments on commit dbc6a7d

Please sign in to comment.