Nicer server setup
This commit is contained in:
parent
7b9abd159c
commit
0b6084d4ee
|
@ -9,10 +9,12 @@ possibly selected text, additional comments or tags and send it into your [Org M
|
||||||
* `pip3 install --user hug` for [Hug](http://www.hug.rest/) HTTP server.
|
* `pip3 install --user hug` for [Hug](http://www.hug.rest/) HTTP server.
|
||||||
|
|
||||||
# Running
|
# Running
|
||||||
1. Install server counterpart as systemd service: `server/setup`.
|
1. Install server counterpart as systemd service (to autostart it): `server/setup --path /path/to/your/capture.org [--port <custom port>]`.
|
||||||
|
|
||||||
|
Or alternatively, just run it directly if you don't want to autostart it: `server/grasp_server.py --path /path/to/your/capture.org [--port <custom_port>]`.
|
||||||
2. Install chrome extension and configure hotkeys
|
2. Install chrome extension and configure hotkeys
|
||||||
|
|
||||||
That's it! Currently port `12212` is hardcoded, but it will be configurable.
|
That's it! **Currently port `12212` is hardcoded** in the extension, but it will be configurable later.
|
||||||
|
|
||||||
# Motivation
|
# Motivation
|
||||||
Why use org-capture? Well, it's hard to explain, maybe some other time... However if you do know you want to use it instead/alongside your browser bookmarks, by default
|
Why use org-capture? Well, it's hard to explain, maybe some other time... However if you do know you want to use it instead/alongside your browser bookmarks, by default
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
CAPTURE_PATH = Path('/L/Dropbox/mobile/notes/links.org')
|
|
|
@ -1,8 +0,0 @@
|
||||||
#!/bin/bash
|
|
||||||
set -eux
|
|
||||||
|
|
||||||
cd "$(dirname "$0")"
|
|
||||||
|
|
||||||
PORT=12212
|
|
||||||
|
|
||||||
hug -p "$PORT" -f server.py
|
|
43
server/server.py
Normal file → Executable file
43
server/server.py
Normal file → Executable file
|
@ -1,13 +1,17 @@
|
||||||
"""First API, local access only"""
|
#!/usr/bin/python3
|
||||||
|
import argparse
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
import hug # type: ignore
|
import hug # type: ignore
|
||||||
import hug.types as T # type: ignore
|
import hug.types as T # type: ignore
|
||||||
|
|
||||||
from config import CAPTURE_PATH # TODO mm, maybe configure it during server setup?
|
|
||||||
from org_tools import append_org_entry
|
from org_tools import append_org_entry
|
||||||
|
|
||||||
|
CAPTURE_PATH_VAR = 'GRASP_CAPTURE_PATH'
|
||||||
|
|
||||||
def empty(s) -> bool:
|
def empty(s) -> bool:
|
||||||
return s is None or len(s.strip()) == 0
|
return s is None or len(s.strip()) == 0
|
||||||
|
|
||||||
|
@ -27,6 +31,8 @@ def capture(
|
||||||
):
|
):
|
||||||
log("capturing", url, title, selection, comment)
|
log("capturing", url, title, selection, comment)
|
||||||
|
|
||||||
|
capture_path = Path(os.environ[CAPTURE_PATH_VAR]).expanduser()
|
||||||
|
|
||||||
heading = url
|
heading = url
|
||||||
parts = []
|
parts = []
|
||||||
tags = []
|
tags = []
|
||||||
|
@ -51,13 +57,13 @@ def capture(
|
||||||
])
|
])
|
||||||
body = None if len(parts) == 0 else '\n'.join(parts)
|
body = None if len(parts) == 0 else '\n'.join(parts)
|
||||||
|
|
||||||
|
# TODO format response on extension site
|
||||||
response = {
|
response = {
|
||||||
'file': str(CAPTURE_PATH),
|
'file': str(capture_path),
|
||||||
}
|
}
|
||||||
try:
|
try:
|
||||||
append_org_entry(
|
append_org_entry(
|
||||||
CAPTURE_PATH,
|
capture_path,
|
||||||
heading=heading,
|
heading=heading,
|
||||||
body=body,
|
body=body,
|
||||||
tags=tags,
|
tags=tags,
|
||||||
|
@ -74,3 +80,30 @@ def capture(
|
||||||
})
|
})
|
||||||
|
|
||||||
return json.dumps(response).encode('utf8')
|
return json.dumps(response).encode('utf8')
|
||||||
|
|
||||||
|
def run(port: str, capture_path: str):
|
||||||
|
env = os.environ.copy()
|
||||||
|
# not sure if there is a simpler way to communicate with the server...
|
||||||
|
env[CAPTURE_PATH_VAR] = capture_path
|
||||||
|
os.execvpe(
|
||||||
|
'hug',
|
||||||
|
[
|
||||||
|
'grasp-server',
|
||||||
|
'-p', port,
|
||||||
|
'-f', __file__,
|
||||||
|
],
|
||||||
|
env,
|
||||||
|
)
|
||||||
|
|
||||||
|
def setup_parser(p):
|
||||||
|
p.add_argument('--port', type=str, default='12212', help='Port for communicating with extension')
|
||||||
|
p.add_argument('--path', type=str, default='~/capture.org', help='File to capture into')
|
||||||
|
|
||||||
|
def main():
|
||||||
|
p = argparse.ArgumentParser('grasp server setup', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
||||||
|
setup_parser(p)
|
||||||
|
args = p.parse_args()
|
||||||
|
run(args.port, args.path)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
54
server/setup
54
server/setup
|
@ -1,10 +1,13 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
import argparse
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from subprocess import check_call
|
from subprocess import check_call
|
||||||
|
|
||||||
run_script = Path(__file__).parent.joinpath('run').absolute()
|
# TODO rename to grasp_server.py
|
||||||
|
from server import setup_parser
|
||||||
|
|
||||||
CONFIG = f"""
|
SYSTEMD_CONFIG = """
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=Grasp extension server conterpart
|
Description=Grasp extension server conterpart
|
||||||
|
|
||||||
|
@ -12,29 +15,46 @@ Description=Grasp extension server conterpart
|
||||||
WantedBy=default.target
|
WantedBy=default.target
|
||||||
|
|
||||||
[Service]
|
[Service]
|
||||||
ExecStart={run_script}
|
ExecStart={server} {extra_args}
|
||||||
Type=simple
|
Type=simple
|
||||||
Restart=always
|
Restart=always
|
||||||
"""
|
"""
|
||||||
|
|
||||||
SNAME = 'grasp.service'
|
|
||||||
|
|
||||||
out = Path(f'~/.config/systemd/user/{SNAME}').expanduser()
|
|
||||||
|
|
||||||
print(f"Writing systemd config to {out}:")
|
|
||||||
with out.open('w') as fo:
|
|
||||||
fo.write(CONFIG)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def systemd(*args):
|
def systemd(*args):
|
||||||
check_call([
|
check_call([
|
||||||
'systemctl', '--user', *args,
|
'systemctl', '--user', *args,
|
||||||
])
|
])
|
||||||
|
|
||||||
systemd('daemon-reload')
|
def setup(args):
|
||||||
systemd('enable', SNAME)
|
unit_name = args.unit_name
|
||||||
systemd('start' , SNAME)
|
out = Path(f'~/.config/systemd/user/{unit_name}').expanduser()
|
||||||
systemd('status', SNAME)
|
print(f"Writing systemd config to {out}:")
|
||||||
|
|
||||||
# TODO journalctl --user -u grasp.service to debug
|
server_bin = Path(__file__).parent.joinpath('server.py').absolute()
|
||||||
|
extra_args = f'--port {args.port} --path {args.path}'
|
||||||
|
|
||||||
|
with out.open('w') as fo:
|
||||||
|
fo.write(SYSTEMD_CONFIG.format(
|
||||||
|
server=server_bin,
|
||||||
|
extra_args=extra_args,
|
||||||
|
))
|
||||||
|
|
||||||
|
try:
|
||||||
|
systemd('daemon-reload')
|
||||||
|
systemd('enable', unit_name)
|
||||||
|
systemd('start' , unit_name)
|
||||||
|
systemd('status', unit_name)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Something has gone wrong... you might want to use 'journalctl --user -u {unit_name}' to debug")
|
||||||
|
raise e
|
||||||
|
|
||||||
|
def main():
|
||||||
|
p = argparse.ArgumentParser('grasp server setup', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
||||||
|
p.add_argument('--unit-name', type=str, default='grasp.service', help='Systemd unit name')
|
||||||
|
setup_parser(p)
|
||||||
|
args = p.parse_args()
|
||||||
|
setup(args)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in a new issue