From 6f3fbba48afa1d80ddebc3e8582925d74b68c730 Mon Sep 17 00:00:00 2001 From: Federico Ragona Date: Wed, 10 Jul 2024 18:23:22 +0200 Subject: [PATCH] chore: change default HTTP port and show it in system tray --- .gitignore | 2 +- README.md | 22 ++++++++++++---------- bookmarklet/bookmarklet.js | 2 +- bookmarklet/src.js | 2 +- cmd/tray/main.go | 29 +++++++++++++++-------------- internal/config.go | 2 +- dist/package.sh => macos/bundle.sh | 2 -- pocket-importer/importer.ts | 2 +- 8 files changed, 32 insertions(+), 31 deletions(-) rename dist/package.sh => macos/bundle.sh (93%) diff --git a/.gitignore b/.gitignore index 12635a0..1555126 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ bin/ -dist/bookmarkd.app/ +macos/bookmarkd.app/ diff --git a/README.md b/README.md index 25ac736..41f6336 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# bookmarkd +# bookmark'd Converts HTML pages to Markdown files and stores them in a local [Obsidian](https://obsidian.md) vault, in line with Steph Ango's [File over app](https://stephango.com/file-over-app) philosophy. @@ -6,20 +6,20 @@ Whenever you'd like to bookmark a page, click on the provided bookmarklet, and y ## Usage -The code can run either as a standalone HTTP server or as a [Vercel Function](https://vercel.com/docs/functions/runtimes/go). In either case, after the deployment you have to change the URL in `bookmarklet/src.js` to point to your deployed code and then follow the instructions in [Install bookmarklet](README.md#install-bookmarklet). +The code can run either locally or as a [Vercel Function](https://vercel.com/docs/functions/runtimes/go). In either case, after the deployment you have to change the URL in `bookmarklet/src.js` to point to your deployed code and then follow the instructions in [Install bookmarklet](README.md#install-bookmarklet). -### Run as HTTP server +### Run locally -### Option 1: as macOS app +#### Option 1: as macOS app Running ```bash -cd dist -./package.sh +cd macos +./bundle.sh ``` -will create a macOS app in `dist/bookmarkd.app` which will keep the server running and show its status in the system tray. +will create a macOS app in `macos/bookmarkd.app`. Open the app, and it will keep the server running, showing its status in the system tray. #### Option 2: Headless @@ -35,7 +35,7 @@ and then run it (on your local machine, or anywhere you'd like): ./bin/server ``` -The default server address is `http://localhost:3333`, and can be configured via the `BOOKMD_HTTP_ADDRESS` environment variable. +The default server address is `http://localhost:20918`, and can be configured via the `BOOKMD_HTTP_ADDRESS` environment variable. The endpoint will be available at `/api/bookmarks`. @@ -60,8 +60,10 @@ To install the bookmarklet in your browser: ## Bonus: Import Pocket saves -The repository contains an optional importer for [Pocket](https://getpocket.com/) saves in the `pocket-importer` directory. See its README for details. +The repository contains an optional importer for [Pocket](https://getpocket.com/) saves in the `pocket-importer` directory. See its own README for details. ## Credits -Inspired by [downmark](https://github.com/alessandro-fazzi/downmark). +Initial design inspired by [downmark](https://github.com/alessandro-fazzi/downmark). +Packaging script adapted from [xeoncross/macappshell](https://github.com/xeoncross/macappshell). +Tray icon courtesy of [ionicons](https://ionic.io/ionicons/usage#bookmarks-outline). diff --git a/bookmarklet/bookmarklet.js b/bookmarklet/bookmarklet.js index 92e4828..9acaa12 100644 --- a/bookmarklet/bookmarklet.js +++ b/bookmarklet/bookmarklet.js @@ -1 +1 @@ -javascript:(function()%7Bconst%20tags%20%3D%20prompt(%22tags%3A%20%22%2C%20%22%22)%0A%20%20.split(%22%2C%22)%0A%20%20.map(tag%20%3D%3E%20'tags%3D'%20%2B%20encodeURIComponent(tag.trim()))%0A%20%20.join(%22%26%22)%3B%0A%0Aconst%20addr%20%3D%20'http%3A%2F%2Flocalhost%3A3333%2Fapi%2Fbookmarks'%3B%0Aconst%20vault%20%3D%20'my-vault'%3B%0Aconst%20folder%20%3D%20'Clippings'%3B%0A%0Alet%20url%20%3D%20addr%20%2B%20'%3F'%20%2B%0A%20%20'vault%3D'%20%2B%20encodeURIComponent(vault)%20%2B%0A%20%20'%26folder%3D'%20%2B%20encodeURIComponent(folder)%20%2B%0A%20%20'%26url%3D'%20%2B%20encodeURIComponent(document.location)%3B%0A%0Aif%20(tags)%20%7B%0A%20%20url%20%2B%3D%20'%26'%20%2B%20tags%0A%7D%0A%0Adocument.location.href%20%3D%20url%3B%7D)()%3B \ No newline at end of file +javascript:(function()%7Bconst%20tags%20%3D%20prompt(%22tags%3A%20%22%2C%20%22%22)%0A%20%20.split(%22%2C%22)%0A%20%20.map(tag%20%3D%3E%20'tag%3D'%20%2B%20encodeURIComponent(tag.trim()))%0A%20%20.join(%22%26%22)%3B%0A%0Aconst%20addr%20%3D%20'http%3A%2F%2Flocalhost%3A20918%2Fapi%2Fbookmarks'%3B%0Aconst%20vault%20%3D%20'my-vault'%3B%0Aconst%20folder%20%3D%20'Clippings'%3B%0A%0Alet%20url%20%3D%20addr%20%2B%20'%3F'%20%2B%0A%20%20'vault%3D'%20%2B%20encodeURIComponent(vault)%20%2B%0A%20%20'%26folder%3D'%20%2B%20encodeURIComponent(folder)%20%2B%0A%20%20'%26url%3D'%20%2B%20encodeURIComponent(document.location)%3B%0A%0Aif%20(tags)%20%7B%0A%20%20url%20%2B%3D%20'%26'%20%2B%20tags%0A%7D%0A%0Adocument.location.href%20%3D%20url%3B%7D)()%3B \ No newline at end of file diff --git a/bookmarklet/src.js b/bookmarklet/src.js index ff697dc..87a1b3f 100644 --- a/bookmarklet/src.js +++ b/bookmarklet/src.js @@ -3,7 +3,7 @@ const tags = prompt("tags: ", "") .map(tag => 'tag=' + encodeURIComponent(tag.trim())) .join("&"); -const addr = 'http://localhost:3333/api/bookmarks'; +const addr = 'http://localhost:20918/api/bookmarks'; const vault = 'my-vault'; const folder = 'Clippings'; diff --git a/cmd/tray/main.go b/cmd/tray/main.go index 957c204..2541c12 100644 --- a/cmd/tray/main.go +++ b/cmd/tray/main.go @@ -27,27 +27,33 @@ func main() { } func onReady() { + cfg := internal.Config{} + if err := envconfig.Process("", &cfg); err != nil { + log.Fatal(err) + } + ctx, cancel := context.WithCancel(context.Background()) group, gctx := errgroup.WithContext(ctx) group.Go(func() error { - return run(gctx) + return run(gctx, &cfg) }) systray.SetIcon(icon) - systray.SetTooltip("Store bookmarks in Obsidian") + systray.SetTooltip("bookmark'd: store bookmarks in Obsidian") systray.SetOnClick(func(menu systray.IMenu) { if err := menu.ShowMenu(); err != nil { fmt.Println(err) } }) - systray.AddMenuItem("bookmarkd", "").Disable() + systray.AddMenuItem("bookmark'd", "Name").Disable() + systray.AddMenuItem(fmt.Sprintf("Address: %s", cfg.HttpAddress), "Address").Disable() mStatus := systray.AddMenuItem("Status: 🍏", "Status") mStatus.Disable() group.Go(func() error { - checkStatus(gctx, mStatus) + checkStatus(gctx, cfg.HttpAddress, mStatus) return nil }) @@ -66,16 +72,16 @@ func onReady() { func onExit() {} -func checkStatus(ctx context.Context, mStatus *systray.MenuItem) { +func checkStatus(ctx context.Context, httpAddr string, 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): + case <-time.Tick(time.Second * 10): fmt.Println("updating status...") - res, err := client.Get("http://localhost:3333/api/status") + res, err := client.Get(fmt.Sprintf("http://%s/api/status", httpAddr)) if err != nil { mStatus.SetTitle("Status: ❌") fmt.Println(err) @@ -88,12 +94,7 @@ func checkStatus(ctx context.Context, mStatus *systray.MenuItem) { } } -func run(ctx context.Context) error { - config := internal.Config{} - if err := envconfig.Process("", &config); err != nil { - return err - } - +func run(ctx context.Context, cfg *internal.Config) error { router := chi.NewRouter() router.Use(middleware.Logger) router.Get("/api/bookmarks", api.Handle) @@ -101,7 +102,7 @@ func run(ctx context.Context) error { w.WriteHeader(http.StatusOK) }) - server := &http.Server{Addr: config.HttpAddress, Handler: router} + server := &http.Server{Addr: cfg.HttpAddress, Handler: router} go func() { fmt.Println("starting server...") diff --git a/internal/config.go b/internal/config.go index 4884e32..59e45e4 100644 --- a/internal/config.go +++ b/internal/config.go @@ -1,5 +1,5 @@ package internal type Config struct { - HttpAddress string `envconfig:"BOOKMARKD_HTTP_ADDRESS" default:"0.0.0.0:3333"` + HttpAddress string `envconfig:"BOOKMARKD_HTTP_ADDRESS" default:"0.0.0.0:20918"` } diff --git a/dist/package.sh b/macos/bundle.sh similarity index 93% rename from dist/package.sh rename to macos/bundle.sh index 119c9d9..4c2b68d 100755 --- a/dist/package.sh +++ b/macos/bundle.sh @@ -3,8 +3,6 @@ set -eu # Mac OSX .app builder -# Copied from https://github.com/xeoncross/macappshell/blob/master/setup.sh, all credits (and many thanks!) to its author. -# Note: slightly adjusted to feet my project's needs. function die { echo "ERROR: $1" > /dev/null 1>&2 diff --git a/pocket-importer/importer.ts b/pocket-importer/importer.ts index 14e60a5..4ec0698 100644 --- a/pocket-importer/importer.ts +++ b/pocket-importer/importer.ts @@ -7,7 +7,7 @@ import * as fs from "node:fs"; const defaultContext = browser.contexts()[0]; const page = defaultContext.pages()[0]; - const serverAddress = 'http://localhost:3333'; + const serverAddress = 'http://localhost:20918'; const vault = 'my-vault'; const folder = 'Clippings'; const doc = fs.readFileSync('./pocket-export.html', 'utf8');