Skip to content

Commit

Permalink
chore: change default HTTP port
Browse files Browse the repository at this point in the history
and show it in system tray
  • Loading branch information
fedragon committed Jul 10, 2024
1 parent 9e6292a commit 6f3fbba
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
bin/
dist/bookmarkd.app/
macos/bookmarkd.app/
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# 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.

Whenever you'd like to bookmark a page, click on the provided bookmarklet, and you will be prompted to add it to your Obsidian vault.

## 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

Expand All @@ -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 `<your_url>/api/bookmarks`.

Expand All @@ -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).
2 changes: 1 addition & 1 deletion bookmarklet/bookmarklet.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bookmarklet/src.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
29 changes: 15 additions & 14 deletions cmd/tray/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})

Expand All @@ -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)
Expand All @@ -88,20 +94,15 @@ 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)
router.Get("/api/status", func(w http.ResponseWriter, r *http.Request) {
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...")
Expand Down
2 changes: 1 addition & 1 deletion internal/config.go
Original file line number Diff line number Diff line change
@@ -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"`
}
2 changes: 0 additions & 2 deletions dist/package.sh → macos/bundle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pocket-importer/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 6f3fbba

Please sign in to comment.